Batch: Copy files from txt file into one folder

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 19:04:07

问题


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
...etc

All 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?


回答1:


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?




回答2:


One sample which worked for me...

Replace my directories C:\whatever and C:\temp\svn with yours...

assuming that your filelist is named antidump_list.txt and located under C:\temp\svn\

> set src_folder=C:\whatever
> set dst_folder=C:\temp\svn
> for /f %%i in (C:\temp\svn\antidump_list.txt) DO copy "%src_folder%\%%i" "%dst_folder%\%%i"

Regards,

Gottfried




回答3:


I have found that the easiest way to do this is to use a powershell script.

$Files = Get-Content File-list.txt
$Dest = "C:\output"

foreach ($File in $Files) {
  Copy-Item $File $Dest
}

If you need to run it from a batch file, paste the above script to file named CopyFiles.ps1 and add the following command to your batch file

powershell -executionpolicy bypass -file .\CopyFiles.ps1

Since, powershell is included by default on Windows7 and newer, this method is as easy as doing the same with batch commands only.



来源:https://stackoverflow.com/questions/9143018/batch-copy-files-from-txt-file-into-one-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!