ChDir 陳述式

變更目前的目錄或資料夾。

語法

ChDir路徑

必要的 path 自變字串表達式,可識別哪些目錄或資料夾成為新的預設目錄或資料夾。 path 可能包含磁碟機。 如果沒有指定磁碟機,ChDir 會在目前的磁碟機上變更預設的目錄或資料夾。

備註

ChDir 陳述式會變更預設目錄或資料夾,但不會變更預設磁碟機。 另一個陳述式 ChDrive 會變更預設磁碟機。

ChDir "D:\TMP" ' Make "D:\TMP" the current folder. 

ChDrive "D"    ' Make "D" the current drive. 

在 Power Macintosh 上,預設磁碟機一律會變更為 path 中指定的磁碟機。 完整路徑規範的開頭為磁碟區名稱,而相對路徑的開頭為冒號 (:)。 ChDir 會解析路徑中指定的任何別名:

ChDir "MacDrive:Tmp" ' On the Macintosh. 

請注意,進行相對目錄變更時,在 Microsoft Windows 和 Macintosh 中會使用不同的符號:

ChDir ".." ' Moves up one directory in Microsoft Windows. 
ChDir "::" ' Moves up one directory on the Macintosh.

在 Macintosh 中,預設磁碟機名稱為 "HD",而路徑名稱的部分則是以分號隔開,而不是反斜線。 同樣地,您會指定 Macintosh 資料夾,而不是 Windows。 最後,萬用字元在 Macintosh 中不會有特殊意義,而只會視為字元。

範例

此範例使用 ChDir 陳述式變更目前的目錄或資料夾。 如果預設磁碟機為 C,即使預設資料夾變更為磁碟機 D: 上的資料夾,預設磁碟機仍為 C。

' Assume "C:" is the current drive. The following statement changes 
' the default directory on drive "D:". "C:" remains the current drive. 
ChDir "D:\WINDOWS\SYSTEM" 

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應