Batch: Copy files from txt file into one folder
Start reading HELP FOR
and then try the following at the command prompt
FOR /F %a in (input.txt) DO @ECHO COPY %a c:\newfolder\%~nxa
you can see that %a
gets expanded to the actual line in the input file, and that %~nxa
is a way to extract the name and the extension from the file.
After careful testing, move the command to your BAT file, replace %a
to%%a
, and remove the ECHO
command
@echo off
SET destfolder=c:\newfolder
FOR /F "delims=" %%a IN (input.txt) DO COPY "%%a" "%destfolder%\%%~nxa"
notice the wraping of the names with quotes "
; and the inclusion of the "delims="
option; both are needed in case filenames contain blanks.
Finally be careful with possible name duplicates in the destination folder. If that is possible, you need to find an strategy to cope with such collisions. But this can be the subject of another SO question, can't it?
Admin
Updated on July 26, 2020Comments
-
Admin almost 4 years
I am attempting to create a batch file to copy several files listed in a text file to a new folder. I have found several threads relating to this, but I can still not get the batch to work properly. The problem I am encountering is that the files listed in the txt are all in different source locations and have different extensions. The list reads, for example:
C:\Users\Foo\Pictures\Photographs\September\P1030944.jpg
C:\Users\Foo\Videos\Art\Movies\Class\movie.avi
C:\Users\Foo\Music\Jazz\20051.mp3
...etcAll the copy commands I could find have to list either the source directory i.e.
set src_folder=c:\whatever\ set dst_folder=c:\foo for /f %%i in (File-list.txt) DO xcopy /S/E/U "%src_folder%\%%i" "%dst_folder%"
or the extension i.e.
for /R c:\source %f in (*.xml) do copy "%f" x:\destination\
but I need it to gather that information from the list itself.
If it helps I know that there are only files of a possible 39 different specific extensions in the txt (*.jpg *.gif *.png ... *.xhtml *.xht)Any help/ideas?