且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

批处理文件以读取带有文件名的txt文件,然后搜索该文件并将其复制到文件夹

更新时间:2023-01-30 22:00:38

@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION

SET "DEST_DIR=%USERPROFILE%\Desktop\Folder"
SET "SEARCH_DIR=S:\Class"

FOR /F "tokens=*" %%a IN ('type %~dp0list.txt') DO (
    FOR /R "%SEARCH_DIR%" %%f IN (*%%a*) DO (
        SET "SRC=%%~dpf"
        SET DEST=!SRC:%SEARCH_DIR%=%DEST_DIR%!
        xcopy /S /I "%%~f" "!DEST!"
    )
)

这是您发布的脚本中一行(FOR循环)的修改.

This is a modification of a single line (the FOR loop) in the script you posted.

  • list.txt中的每一行进行递归搜索.这效率低下,但是如果您想提高效率,则可能不会使用纯批处理/CMD.
  • 匹配按*<pattern>*完成,其中< pattern>是list.txt中的完整行.
  • tokens=*:不管分隔符如何,都将每一行上的所有文本捕获为变量的内容. (这实际上与"delims="相同.)
  • /S:请勿处理(或创建)空目录
  • SRC用于允许替换以构建目标路径(它是目录,而不是文件名-xcopy是为目录设计的).
  • A recursive search is done for each line in list.txt. This is inefficient, but if you wanted efficiency, you probably wouldn't be using pure batch/CMD.
  • The matching is done as *<pattern>*, where <pattern> is a complete line in list.txt.
  • tokens=*: Regardless of delimiter, capture all text on each line as the content of the variable. (This is effectively the same as "delims=".)
  • /S: don't process (or create) empty directories
  • SRC is used to allow substitution to build a destination path (which is a directory, not a file name—xcopy is designed for directories).

list.txt :

micro
pipeline
exhaust