简体   繁体   English

批处理文件以在目录中搜索文件列表并复制到单个文件夹

[英]Batch file to search directory for list of files and copy to single folder

I have a .txt file with a list of files that are on a network drive (M:). 我有一个.txt文件,其中包含网络驱动器(M :)上的文件列表。 I need a batch file to go through that list, search for the files which are in sub-directories of a single folder on that drive, and then copy them to another folder. 我需要一个批处理文件来浏览该列表,搜索该驱动器上单个文件夹的子目录中的文件,然后将它们复制到另一个文件夹。 I have tried quite a few solutions with no luck. 我已经尝试了很多解决方案,但都没有运气。

the text file is a list of files and extensions ie 文本文件是文件和扩展名的列表,即

abc.step
afer.iges
ca76dc7d.sldprt

Here's what I tried so far 到目前为止,这是我尝试过的

@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 
cls
set dest=C:\Users\kduquette.000\Desktop\Files 
for /f "TOKENS=*" %%f in (C:Desktop\Files\list.txt) do (
  set i=1
  for /f "tokens=*" %%F IN ('dir M: "%%~f"') Do (
    for %%N in ("%%F") Do (
      set name=%%~NN
      set ext=%%~XN
    )
    copy "%%~F" "%dest%\!name!_!i!!ext!"
    set /A i=!i!+1
  ) 
)
ENDLOCAL

We can just do a check if file exists and copy it: 我们可以检查文件是否存在并复制它:

@echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do if exist "M:\Cads\%%i" echo copy "M:\Cads\%%i" "%dest%" 

Note, for now, I added echo to the copy string, so you can test the result first. 注意,目前,我在复制字符串中添加了echo ,因此您可以首先测试结果。 Once happy with the results, remove echo 对结果满意后,请删除echo

If however you want to search for the files, recursively throughout M:\\ drive then: 但是,如果要搜索文件,请在M:\\驱动器中递归搜索,然后:

@echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do (
    pushd M:\
    for /f "delims=" %%a in ('dir /b /s "%%i"') do echo copy "%%~fa" "%dest%"
    popd
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows Batch 询问文件格式,然后从列表中搜索文件并复制到目录 - Windows Batch ask for fileformat then search files from list and copy to directory 如何将基于列表(txt)的文件批量复制到具有相同目录结构的另一个文件夹中? - How to batch copy files based on a list (txt) in to another folder with same directory structure? 如何在批处理文件中复制特定文件的目录 - how to copy directory of specific files in batch file 批处理文件可将文件从多个文件夹复制到单个文件夹,如果日期较新则覆盖 - Batch file to copy files from multiple folders to a single folder, overriding if date is newer 有没有一种方法可以将带有批处理文件的文件复制到该批处理文件所在的文件夹中? - Is there a way to copy files with a batch file to the folder that batch file is in? 在子目录中搜索特定目录名称并复制文件(Windows批处理) - Search for specific directory names within subdirectories and copy files (Windows batch) 将目录文件复制到批处理文件中的另一个目录,而无需询问? - Copy Directory files to another directory in batch file without asking? 如何基于一个列表文件从一个文件夹到下一个文件夹搜索文件直到找到 - How to search files from one folder to next till found based upon a list file in batch 在Windows批处理中复制单个文件而不是整个目录 - Copy single file instead of entire directory in windows batch 批处理文件以在PC列表中搜索文件夹/文件并创建日志文件 - Batch file to search list of PCs for a folder/file and create log file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM