I am attempting to copy specific files from a list, "filelist.txt" to a destination folder. With the code presented below, I can only do this from a specific source folder and have only the files names in the text file (as compared to the full path). I wanted to copy files from subfolders in the main folder. How can I do this if I already have the full path of the files that I need copied in the text file?
Here is the start of the code that I have (built from the code presented here):
CODE
@ECHO ON
SET FileList=G:\filelist.txt
SET Source=G:\fold1
SET Destination=G:\Copy1
FOR /F "USEBACKQ TOKENS=*" %%F IN ("%FileList%") DO XCOPY /F /Y "%Source%\%%~F" "%Destination%\"
GOTO :EOF
I was asking myself the same question when I stumbled across this page. I ended up going a different route. Read to the end but my simplest method to copy files listed in a .txt file is this. Also note that xcopy can be used instead of copy if you need to.
for /f "tokens=1,2,3,4" %%a in ('type %TxtFilePath%\List.txt') do (copy %LogFilePath%\%%a %DestinationFilePath%\%%a)
In my instance I was creating the list of files at runtime from a directory. I was specifically looking for dated .log files in this case.
Since I am looking for dated .log files in my case I put the EnterDate section in at the top to be able to specify the date. The second line in that section is to format the entered date of YYYYMMDD to be MM/DD/YYYY so that it matches the formatting of the List.txt file generated by the DIR command in the first part.
The first part creates the List.txt of .log files from the directory I want.
The second part reads each line of the list and copies the file listed
@echo off
set TxtFilePath=c:\users\public\desktop
set LogFilePath=c:\data\logs
set DestinationFilePath=c:\windows\temp\logs
:EnterDate
set /p StrtDate=Enter Date YYYMMDD :
set Date=%StrtDate:~-4,-2%/%StrtDate:~-2%/%StrtDate:~0,4%
:FirstPart
cd /d %LogFilePath%
dir *.log | find "%Date%" > %TxtFilePath%\List.txt
:SecondPart
for /f "tokens=1,2,3,4" %%a in ('type %TxtFilePath%\List.txt') do (copy %LogFilePath%\%%d %DestinationFilePath%\%%d)
Since I am generating the List.txt at runtime the list looks like this
04/21/2023 08:04 1,000,032 EABD.133265594456432271.log
04/21/2023 13:06 266,794 EABD.log
I you have a list with just file names like the one below you can change the %%d to %%a in the copy portion of the second part and just use the second part to do the copying
EABD.133265594456432271.log
EABD.log
here is the modified second part. This will work to grab any file listed in the List.txt file and will work with examples like the filelist.txt posted earlier.
for /f "tokens=1,2,3,4" %%a in ('type %TxtFilePath%\List.txt') do (copy %LogFilePath%\%%a %DestinationFilePath%\%%a)
Finally, if you have a list file like the filelist.txt example posted in another answer, you can modify the second part more to select specific files or file types from the list to copy.
filelist.txt
test.txt
text.bat
test.cmd
biltudas1.md
The modified code would look like this
for /f "tokens=1,2,3,4" %%a in ('type %TxtFilePath%\List.txt ^| find ".txt"') do (copy %LogFilePath%\%%a %DestinationFilePath%\%%a)
I added the [ ^| find ".txt" ] portion to this. In this case using the filelist.txt it would only copy the test.txt. However if you put "test" in place of ".txt" it would copy test.txt and test.cmd. So instead of using ".txt" you can put whatever string in place of .txt to filter. This will only copy ones in the List.txt file that have that string as part of the text and not copy ones that do not.