简体   繁体   中英

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:). 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. Once happy with the results, remove echo

If however you want to search for the files, recursively throughout M:\\ drive then:

@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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM