批处理文件到许多上市文件复制递归一个特定的文件夹 [英] Batch-file to recursively copy many listed files to one specific folder

查看:180
本文介绍了批处理文件到许多上市文件复制递归一个特定的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一个批处理文件,将递归搜索驱动器的我在寻找特定的文件列表。我相信我已经几乎下来,但我与递归部分有问题。

I am attempting to run a batch file that will recursively search a drive for a list of specific files I'm looking for. I believe I've almost to down but am having issues with the recursive part.

现在,我,我已经根据我读过的其他线程创建两个批处理文件之间的工作。

Right now, I'm working between two batch files that I've created based on other threads I've read.

在运行该批处理文件,我想命令打开位于LISTFOLDER文本文件,并宣读了所有的文件名与在这个文本文件。其中以下脚本的美丽。用下面的脚本的唯一问题是,它不通过FILESPATH递归搜索

When running this batch file I want the command to open the text file that is located in LISTFOLDER and read over all of the file names with in this text file. Which the below script does beautifully. The only issue with the below script is that it doesn't search recursively through the FILESPATH.

@echo off

set LISTFOLDER=C:\vbk\txt\test
set FILESPATH=C:\
set DESTPATH=C:\vbk\dest\test

for /f "tokens=*" %%i in ('dir /b ^"%LISTFOLDER%\*.txt^"') do (call :COPY_FILES "%LISTFOLDER%\%%i")

pause
exit

:COPY_FILES
for %%i in (%1) do set DEST=%%~ni
for /f "usebackq delims==" %%i in (%1) do echo xcopy /qv "%FILESPATH%\%%i" "%DESTPATH%\*"

在做一些调查研究后,我发现,通过recursivally搜索FILESPATH,但不会阅读文本文件的命令,它似乎是搜索文本文件的完整路径(例如C:\\ VBK \\ TXT \\测试\\ test.txt的)与文本文件中的信息。

After doing some more research I found a command that recursivally searched through FILESPATH but wouldn't read the text file and it appears to be search for the entire path of the text file (ex. C:\vbk\txt\test\test.txt) vs the information within the text file.

@echo off

set LISTFOLDER=C:\vbk\txt\test
set FILESPATH=C:\
set DESTPATH=C:\vbk\dest\test

for /f "tokens=*" %%i in ('dir /b ^"%LISTFOLDER%\*.txt^"') do (call :COPY_FILES "%LISTFOLDER%\%%i")

pause
exit

:COPY_FILES
for %%i in (%1) do set DEST=%%~ni
for /r %FILESPATH% %%I in (%1) do echo xcopy /qvs "%%I" "%DESTPATH%\*"

下面是这将是在位于LISTFOLDER .txt文件的一个小例子。

Below is a small example of what would be in the .txt file located in LISTFOLDER.

01PNK2.PVD
01PR52.PVD
01PVN19.PVD
01PW07S.PVD
01Q0G19.PVD
01Q0W2.PVD
01Q102.PVD
01Q182.PVD
01Q1L2.PVD
...

我如何能做到这一点的任何建议将不胜AP preciated。

Any suggestions on how I can accomplish this would be greatly appreciated.

推荐答案

如果我这样做是正确,下面code应该做你想要什么:

If I got it right, the following code should do what you want:

@echo off

set "LISTFOLDER=C:\vbk\txt\test"
set "FILESPATH=C:\"
set "DESTPATH=C:\vbk\dest\test"

for %%F in ("%LISTFOLDER%\*.txt") do (
    for /F "usebackq delims=" %%L in ("%%~F") do (
        for /R "%FILESPATH%" %%I in ("%%~L*") do (
            if /I "%%~nxL"=="%%~nxI" (
                ECHO xcopy /Q /V "%%~fI" "%DESTPATH%\"
            )
        )
    )
)
pause
exit /B

这是我做过什么:


  • 一个子程序:这里不需要( COPY_FILES ),所以我忽略它;

  • FOR / F 不接受通配符(如 * ),所以我把在标准;

  • FOR / F 环意在阅读中发现的文本文件(S)一行一行(至少在你的$ C $我间pretation C),所以没有必要 DIR / B ;

  • 现在,到了棘手的部分:为/ R 遍历目录树递归搜索匹配在其设置指定文件(即在<$ C $部分C>在在());但是如果提供了一个专用的文件有没有任何通配符(如 * ),它实际上并不搜索文件系统文件,但只返回被附加在文件树中的所有目录;给力为/ R 来真正查找你需要使用通配符的文件;所以这就是为什么我追加一个 * ;但是这个过程可能会返回不需要的文件,也就是这样一个包含之初所期望的名称;所以我把循环中的如果语句来过滤出相应的项目(在比较了〜NX 修改变量扩展为纯文件名只加扩展);

  • 因为为/ R 已不通过目录树走递归的工作,你不需要指定开关 / S XCOPY ; (删除大写 ECHO 实际拷贝文件!)

  • 而不是退出,你应该使用退出/ B 批处理文件中,因为这将退出该批处理文件只而退出终止包含命令提示符实例( CMD )太; (这里退出/ B 是多余的实际,但我把它放在这里只是提...)

  • 设置语法进行了改进,以便有围绕整个分配前pression引号;这就避免了一些特殊的字符,如麻烦 ^ &安培; ,例如;请注意,不会成为这里的变量值的一部分;

  • 某些引号标记相关的问题已得到修复整个脚本中,所有的路径都在修饰符用于多种变量,以消除潜在的周围的;

  • a subroutine (:COPY_FILES) is not needed here, so I omitted it;
  • for /F does not accept wildcards (like * or ?), so I put over a standard for;
  • the for /F loop is intended to read the found text file(s) line by line (at least upon my interpretation of your code), so there is no need for dir /B;
  • now comes the tricky part: for /R walks through a directory tree recursively, searching for matching files given in its set (that is the part after in within ()); but if a dedicated file is provided there without any wildcards (like * or ?), it does not actually search the file system for the file but merely returns all directories in the tree with the file appended; to force for /R to really search the file you need to use a wildcard; so that's why I appended a *; but this of course might return unwanted files, namely such that contains the desired name at the beginning; so I placed an if statement inside of the loop to filter out the respective items (the ~nx modifier of the compared for variables expand to the pure file names plus extensions only);
  • since for /R already does the job of walking through a directory tree recursively, you do not need to specify the switch /S for xcopy; (remove the upper-case ECHO to actually copy files!)
  • instead of exit, you should use exit /B within batch files, because this quits the batch file only while exit terminates the containing command prompt instance (cmd) too; (here exit /B is superfluous actually, but I put it here just to be mentioned...)
  • the set syntax has been improved so that there are quotation marks around the entire assignment expression; this avoids trouble with some special characters like ^ and &, for example; note that the "" do not become part of the variable value here;
  • some quotation-mark-related issues have been fixed throughout the script, all paths are enclosed within "", and the ~ modifier is used for several for variables to remove potential surrounding "";

这篇关于批处理文件到许多上市文件复制递归一个特定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆