好吧,如果你只想移動\test
\內的所有文件目錄,那麼這將首先執行文件,然後是批處理目錄。 for/d將複製目錄和子目錄和文件。
@echo off
move "d:\temp\test\*" "d:\temp\archive"
for /d %%a in ("D:\temp\test\*") do move "%%~fa" "d:\temp\archive\"
作爲一個附註,從cmd下面運行時你會得到一個錯誤。
move d:\temp\test\* d:\temp\archive
這是因爲它會移動所有文件,但不移動目錄。如果獲得The filename, directory name, or volume label syntax is incorrect.
,則沒有文件,只有您的移動命令無法看到的文件夾。
注意從批處理文件中,/Y
開關被禁用,並且文件夾將不會被替換(如果存在)。因此,如果您計劃經常覆蓋,或許更願意使用xcopy
並更新存檔,然後在文件成功複製後在d:\temp
中運行刪除。
最後,始終將您的路徑放在雙重"
中。在這種情況下,將工作沒有雙引號罰款,但如果你有類似move d:\program files\temp\* d:\temp\archives
會造成因爲程序和文件之間的空間的錯誤,因此它總是最好使用move "d:\program files\temp\*" "d:\temp\archive
編輯瞭解%%~
分配。在這些例子中,我們使用的%%I
代替%%a
%~I : expands %I removing any surrounding quotes (")
%~fI : expands %I to a fully qualified path name
%~dI : expands %I to a drive letter only
%~pI : expands %I to a path only
%~nI : expands %I to a file name only
%~xI : expands %I to a file extension only
%~sI : expanded path contains short names only
%~aI : expands %I to file attributes of file
%~tI : expands %I to date/time of file
%~zI : expands %I to size of file
%~$PATH:I : searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
if the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI : expands %I to a drive letter and path only
%~nxI : expands %I to a file name and extension only
%~fsI : expands %I to a full path name with short names only
%~dp$PATH:I : searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI : expands %I to a DIR like output line`
的感謝!你是否也請解釋「%%〜fa」是什麼意思?我只知道「%% a」代表「d:\ temp \ test \」中的每個文件夾。 – ioojimooi
當然。 %%〜fa將與完全限定的路徑有關。請記住,我們將%% a賦值給'D:\ temp \ test \ *',其中'*'是完整路徑,所以'%%〜fa'就是完整路徑。因此,如果你有'D:\ temp \ test \ folder1'' D:\ temp \ test \ folder2',每個人都會用'%%〜fa'來看看它是如何工作的,創建一個批處理並插入它。它只會迴應完整的路徑。 '@echo off for/d %% a in(「D:\ temp \ test \ *」)do echo %%〜fa' –
@ioojimooi這樣做也是一樣的,但不會回顯整個路徑,只是目的地本身。 (「D:\ temp \ test \ *」中)for/d %% a do echo %%〜na' –