Page 1 of 1
How to search for files with file names listed in a text file (DIR)?
Posted: 18 Sep 2019 03:07
by tinfanide
A text file named 'source.txt' containing the file names to be searched for:
a.txt
b.txt
c.txt
I'd like to use 'DIR /B /S' command by referring to 'source.txt' to search for those files and copy those files to a new folder called 'destination'. Could anyone show me how to do that, please? Thank you!
Re: How to search for files with file names listed in a text file (DIR)?
Posted: 21 Sep 2019 00:00
by Hackoo
You can give a try with this batch script :
Code: Select all
@echo off
Title Search and copy from file written by Hackoo on 21/09/2019
set "Source=source.txt"
Set "SourceFolder=E:\Cam_Recorder" Rem You should modify this line to set your sourcefolder
set "DestinationFolder=E:\MyData\backupfiles" Rem You should modify this line to set your DestinationFolder
If Not Exist "%DestinationFolder%" MD "%DestinationFolder%"
set /a count=0
SETLOCAL enabledelayedexpansion
for /F "delims=" %%a in ('Type "%Source%"') do (
For /f "delims=" %%F in ('dir /s /b "%SourceFolder%" ^|find /I "%%a"') Do (
Set /a count+=1
Set "File[!count!]=%%F"
)
)
For /L %%i in (1,1,%Count%) Do (
set /a N=1
echo Copying file [%%i] "!File[%%i]!"
Call :MakeCopy "!File[%%i]!" "%DestinationFolder%"
)
pause & Exit
::*******************************************************
:MakeCopy <Source> <Destination>
Set Source="%~1"
Set Destination="%~2\"
set "Filename=%~n1"
set "Ext=%~x1"
echo N | Copy /-Y %Source% %Destination%>nul && goto :Loop
Exit /b
::*******************************************************
:Loop
if exist %Destination%%Filename%(%N%)%Ext% set /a N+=1
echo N | Copy /-Y %Source% %Destination%%Filename%(%N%)%Ext%>nul
Exit /b
::*******************************************************