有这样的目录结构:
Dir1
--Dir2
--File1
--File2
--Dir3
--File3
--File4
--File5
现在我想使用批处理文件将子目录(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)?
代码 -
@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
答案 0 :(得分:6)
无需批处理文件。从Dir1文件夹执行以下命令:
for /r /d %F in (*) do @copy /y "%F\*"
作为批处理文件
@echo off
for /r /d %%F in (*) do copy /y "%%F\*"
但是 - 请注意,您可能在多个子文件夹中具有相同的文件名。只有一个人能在你的Dir1中存活下来。
修改强>
以上假设您正在运行Dir1文件夹中的命令(或脚本)。如果脚本被扩充以包含Dir1的路径,它可以从任何地方运行。
for /r "pathToDir1" /d %F in (*) do @copy /y "pathToDir1\%F\*"
或作为批处理文件
@echo off
set "root=pathToDir1"
for /r "%root%" /d %%F in (*) do copy /y "%root%\%%F\*"
您可以将路径传递给Dir1作为批处理文件的参数。如果要使用当前文件夹,请将.
作为路径传递。
@echo off
for /r %1 /d %%F in (*) do copy /y "%~1\%%F\*"