我的文件是这样,有一个总目录,然后里面有很多子目录,子目录又有很多子目录。这样大概5-6层,才是我所需要的文件。
现在我的需求是将这些文件统一都复制到一个目录里,文件夹不要被复制,只需要复制文件,但是因为里面有很多同名文件,复制的时候会起冲突,询问是否覆盖。我要求是不能覆盖,如果同名则自动重命名,重命名规范无所谓。
找了一朋友写了一份批处理,但是只能复制当前目录的,如果有很多子目录就不能复制了。代码如下:
@echo off
set num=0
set aa=目录1
set bb=目录2
for /f "delims=" %%i in ('dir /a-d /b %aa%\*.*') do (
if not exist "%bb%\%%i" (
copy "%aa%\%%i" %bb%\>nul
) else call :copy_txt "%aa%\%%i" "%%~ni" %%~xi
)
goto :eof
:copy_txt
set file_n=%2
set "file_n=%file_n:~1,-1%"
set file_x=%3
set /a num+=1
if not exist "%bb%\%file_n%%num%%file_x%" (
copy %1 "%bb%\%file_n%%num%%file_x%">nul
) else call :copy_txt %1 "%file_n%" %file_x%
set num=0
goto :eof
提示这样的错误。
追答@echo off
setlocal enabledelayedexpansion
set s=d:\work
set d=e:\back
pushd %s%
(for /f "tokens=*" %%i in ('dir/s/b/a-d') do (
for /f %%j in ('dir/b "%d%\%%~nxi"^|find /c /v ""') do set /a n=%%j+1
if !n!==1 (echo copy "%%i" "%d%\%%~nxi") else copy "%%i" "%d%\%%~ni!n!%%~xi"))>out.txt
notepad out.txt
把out.txt的信息hi给我看看。
正确答案为
@echo off
setlocal enabledelayedexpansion
set s=D:\m1
set d=D:\m2
pushd %s%
(for /f "tokens=*" %%i in ('dir/s/b/a-d') do (
set /a n=1
if exist "%d%\%%~nxi" for /f %%j in ('dir/b "%d%\%%~nxi"^|find /c /v ""') do set /a n=%%j+1
if !n!==1 (copy "%%i" "%d%\%%~nxi") else copy "%%i" "%d%\%%~ni_!n!%%~xi"))
谢谢sudoku52