Pri*_*yam 0 recursion xcopy batch-file
有这个目录结构:
Dir1
--Dir2
--File1
--File2
--Dir3
--File3
--File4
--File5
Run Code Online (Sandbox Code Playgroud)
现在我想使用批处理文件将子目录(Dir2,Dir3)中的所有文件复制到父目录Dir1.我已经提出了下面的代码,但它并不完美.我得到以下输出 -
Directory2 --It has 4 files all together
Invalid number of parameters
Invalid number of parameters
Does E:\Directory1\Copy\File1.dat specify a file name -- And only this file gets copied
or directory name on the target
(F = file, D = directory)?
Run Code Online (Sandbox Code Playgroud)
代码 -
@echo off
call :treeProcess
Pause
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for /D %%d in (*) do (
echo %%d
cd %%d
for %%f in (*) do xcopy %%f E:\Movies\Copy\%%f
call :treeProcess
cd ..
)
exit /b
Run Code Online (Sandbox Code Playgroud)
不需要批处理文件.从Dir1文件夹执行以下命令:
for /r /d %F in (*) do @copy /y "%F\*"
Run Code Online (Sandbox Code Playgroud)
作为批处理文件
@echo off
for /r /d %%F in (*) do copy /y "%%F\*"
Run Code Online (Sandbox Code Playgroud)
但是 - 请注意,您可能在多个子文件夹中具有相同的文件名.只有一个人会在你的Dir1中存活下来.
编辑
以上假设您正在运行Dir1文件夹中的命令(或脚本).如果脚本扩充到包含Dir1的路径,它可以从任何地方运行.
for /r "pathToDir1" /d %F in (*) do @copy /y "pathToDir1\%F\*"
Run Code Online (Sandbox Code Playgroud)
或作为批处理文件
@echo off
set "root=pathToDir1"
for /r "%root%" /d %%F in (*) do copy /y "%root%\%%F\*"
Run Code Online (Sandbox Code Playgroud)
您可以将路径传递给Dir1作为批处理文件的参数..如果要使用当前文件夹,请作为路径传入.
@echo off
for /r %1 /d %%F in (*) do copy /y "%~1\%%F\*"
Run Code Online (Sandbox Code Playgroud)