批处理查找并复制文件到指定文件夹

xiaoxiao2021-02-28  198

 

        如何通过批处理,在一个目录及其子目录中查找指定列表中的所有文件,并把这些文件复制到指定的文件夹中呢?下面这个批处理可堪一用:

    注意事项:

批处理支持只复制文件和保留目录结构两种功能目录支持带有空格,但是不要以反斜杠结尾保留目录结构时,sourcePath只能是一个单独的目录,且不能用分号结尾;只复制文件时,支持多目录查找(用分号隔开多目录,且要保证每个目录都是存在的)。

    如何使用:

        sourcePath是查找的文件夹,targetPath是目标文件夹,dirCopy表示是只复制文件(值为0)还是带目录复制(值为1),fileList是所有的待查找文件列表,是一个文本文件,格式如下:

光辉岁月.txt 江南.txt 123.mp3

        执行完毕后会得到类似的结果,找不到的文件给出文件名,然后给出复制了多少个文件的结果:

@echo off setlocal enabledelayedexpansion rem set parameter here rem dirCopy: 0-file copy, 1-directory copy rem path do not end with backslash rem sourcePath only support one path when use directory copy, multi path(separated by semicolons and make sure all path is exist) is available when use file copy rem eg: rem file copy: rem set sourcePath=C:\Users\yy\Desktop\test 1;C:\Users\yy\Desktop\test2 rem set targetPath=C:\Users\yy\Desktop\target rem set fileList=list.txt rem set dirCopy=0 rem directory copy: rem set sourcePath=C:\Users\yy\Desktop\test 1 rem set targetPath=C:\Users\yy\Desktop\tar get rem set fileList=list.txt rem set dirCopy=1 set sourcePath=C:\Program Files (x86)\NetSarang\Xshell 6\;C:\Program Files (x86)\NetSarang\Xftp 6\;C:\Program Files\Microsoft MPI\Bin\; set targetPath=C:\Users\yy\Desktop\tar get set fileList=list.txt set dirCopy=0 rem add double quotes set before=!sourcePath! set after= :QUOTE_LOOP for /f "tokens=1* delims=;" %%i in ("!before!") do ( set after=!after!"%%i"; if not "%%j"=="" ( set before=%%j goto QUOTE_LOOP ) ) rem begin job set /a copyCount=0 dir /s /b !after!>filelist.txt if !dirCopy! equ 0 ( goto FILE_COPY )else ( goto DIR_COPY ) :DIR_COPY for /f "delims=" %%i in (!fileList!) do ( find "\%%i" filelist.txt > findfile.txt if !errorlevel! equ 0 ( for /f "skip=2 delims=" %%j in (findfile.txt) do ( set tPath=%%~fj set tPath=!tPath:%sourcePath%=! echo f | xcopy /y "%%j" "!targetPath!!tPath!">nul set /a copyCount+=1 ) )else ( echo can't find file %%i ) ) goto END :FILE_COPY for /f "delims=" %%i in (!fileList!) do ( find "\%%i" filelist.txt > findfile.txt if !errorlevel! equ 0 ( for /f "skip=2 delims=" %%j in (findfile.txt) do ( copy /y "%%j" "!targetPath!\%%i">nul set /a copyCount+=1 ) )else ( echo can't find file %%i ) ) goto END :END del filelist.txt del findfile.txt echo ************************************ echo copy file success: !copyCount! pause

注:上面的批处理是修改版的,没有充分测试,如果使用中出现问题,可以使用下面的再试一试,变量的设置和上面的相同:

@echo off setlocal enabledelayedexpansion rem set parameter here rem dirCopy: 0-file copy, 1-directory copy set sourcePath=C:\Test\source set targetPath=C:\Test\target set fileList=list.txt set dirCopy=1 set /a copyCount=0 dir /s /b !sourcePath!>filelist.txt if !dirCopy! equ 0 ( goto FILE_COPY )else ( goto DIR_COPY ) :DIR_COPY for /f "delims=" %%i in (!fileList!) do ( find "%%i" filelist.txt > findfile.txt if !errorlevel! equ 0 ( for /f "skip=2 delims=" %%j in (findfile.txt) do ( set tPath=%%~fj set tPath=!tPath:%sourcePath%=! echo f | xcopy /y %%j !targetPath!!tPath!>nul set /a copyCount+=1 ) )else ( echo can't find file %%i ) ) goto END :FILE_COPY for /f "delims=" %%i in (!fileList!) do ( find "%%i" filelist.txt > findfile.txt if !errorlevel! equ 0 ( for /f "skip=2 delims=" %%j in (findfile.txt) do ( copy /y %%j !targetPath!\%%i>nul set /a copyCount+=1 ) )else ( echo can't find file %%i ) ) goto END :END del filelist.txt del findfile.txt echo ************************************ echo copy file success: !copyCount! pause

        

转载请注明原文地址: https://www.6miu.com/read-66684.html

最新回复(0)