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

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

    注意事项:

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

    如何使用:

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

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


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

        注1:这个批处理目前有一个问题,在仅复制文件的形式下,如果多个子文件夹中有同名文件,那么这些同名文件在目标文件夹下会相互覆盖,最终只保留一个。

        注2:最近又做了一个修改,修改后,在只复制文件的形式下,支持fileList里不写出完整的文件名,仍然可以查找,所有包含了待查找字段的文件名都会被复制,比如说fileList里有一个字符串test, 那么test_1.txt, test_2.mp3, test3.txt等文件都会被复制。由于没有充分测试,所以暂时只写到了评论里,需要的可以参考下面的评论修改相应的代码。

@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


        

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值