批处理:将txt文件中的文件复制到一个文件夹中 [英] Batch: Copy files from txt file into one folder

查看:32
本文介绍了批处理:将txt文件中的文件复制到一个文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个批处理文件以将文本文件中列出的多个文件复制到一个新文件夹中.我发现了几个与此相关的线程,但我仍然无法让批处理正常工作.我遇到的问题是txt中列出的文件都在不同的源位置并且具有不同的扩展名.该列表显示,例如:

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:UsersFooPicturesPhotographsSeptemberP1030944.jpg
C:UsersFooVideosArtMoviesClassmovie.avi
C:UsersFooMusicJazz20051.mp3
...等

C:UsersFooPicturesPhotographsSeptemberP1030944.jpg
C:UsersFooVideosArtMoviesClassmovie.avi
C:UsersFooMusicJazz20051.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%"

或扩展名,即

for /R c:source %f in (*.xml) do copy "%f" x:destination

但我需要它从列表本身收集信息.
如果有帮助,我知道 txt (*.jpg *.gif *.png ... *.xhtml *.xht) 中只有可能 39 种不同特定扩展名的文件

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)

有什么帮助/想法吗?

推荐答案

开始阅读 HELP FOR 然后在命令提示符下尝试以下操作

Start reading HELP FOR and then try the following at the command prompt

FOR /F %a in (input.txt) DO @ECHO COPY %a c:
ewfolder\%~nxa

您可以看到 %a 被扩展为输入文件中的实际行,而 %~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.

经过仔细测试,将命令移到你的BAT文件中,将%a替换为%%a,去掉ECHO命令

After careful testing, move the command to your BAT file, replace %a to%%a, and remove the ECHO command

@echo off
SET destfolder=c:
ewfolder 
FOR /F "delims=" %%a IN (input.txt) DO COPY "%%a" "%destfolder%\%%~nxa"

注意用引号括起来的名称 ";以及包含 "delims=" 选项;如果文件名包含空格,则两者都是必需的.

notice the wraping of the names with quotes "; and the inclusion of the "delims=" option; both are needed in case filenames contain blanks.

最后要小心目标文件夹中可能的名称重复.如果可能,您需要找到应对此类冲突的策略.但这可能是另一个 SO 问题的主题,不是吗?

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?

这篇关于批处理:将txt文件中的文件复制到一个文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆