批处理文件以读取带有文件名的txt文件,然后搜索该文件并将其复制到文件夹 [英] Batch file to read txt file with file names then search for that file and copy to folder
问题描述
我需要的是一个批处理文件,该文件读取txt文件中的部分文件名.每个文件名在其自己的行上.然后,它需要在指定的文件夹(包括子文件夹)中搜索该文件,如果找到了该文件,请将其复制到我的桌面上的文件夹中.我发现一个批处理脚本几乎可以做到这一点,但是我的文件名不是完整的文件名,只是一部分没有扩展名,并且此脚本搜索确切的文件名.我需要修改此脚本以仅使用一部分文件名来搜索文件.
What I need is a batch file that reads partial file names in a txt file. Each file name is on its own line. Then it needs to search for that file in a specified folder (including sub folders) and if the file is found, copy the file to a folder on my desktop. I found a batch script that does almost exactly that, but my file names aren't a complete file name, only part of it with no extension, and this script searches for exact file names. I need to modify this script to search for files using only a part of the file name.
@echo off
REM (c) 2013 BY NEUTRON16 (http://www.sevenforums.com/member.php?u=3585)
CLS
TITLE Mass file finder by Neutron16
REM finds files in list.txt file and copies them to C:\your_files
REM CHECK FOR ADMIN RIGHTS
COPY /b/y NUL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
IF ERRORLEVEL 1 GOTO:NONADMIN
DEL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
:ADMIN
REM GOT ADMIN RIGHTS
COLOR 1F
ECHO Hi, %USERNAME%!
ECHO Please wait...
FOR /R "%~dp0" %%I IN (.) DO for /f "usebackq delims=" %%a in ("%~dp0list.txt") do echo d |xcopy "%%I\%%a" "C:\your_files" /e /i
COLOR 2F
ECHO.
ECHO (c) 2013 by Neutron16 (http://www.sevenforums.com/member.php?u=3585)
PAUSE
GOTO:EOF
:NONADMIN
REM NO ADMIN RIGHTS
COLOR 4F
ECHO.
ECHO PLEASE RUN AS ADMINISTRATOR
ECHO.
pause
GOTO:EOF
有人可以帮我修改吗?
推荐答案
@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 inlist.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 directoriesSRC
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
这篇关于批处理文件以读取带有文件名的txt文件,然后搜索该文件并将其复制到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!