批次:复制从TXT文件的文件到一个文件夹 [英] Batch: Copy files from txt file into one folder

查看:312
本文介绍了批次:复制从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:\\用户\\富\\图片\\照片\\月\\ P1030944.jpg结果
  C:\\用户\\富\\视频\\艺术\\ \\电影类\\影片.avi结果
  C:\\用户\\富\\音乐\\爵士\\ 20051.mp3结果
  ...等

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%"

或扩展即。

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

但我需要它来收集从列表本身的信息。结果
如果它帮助我知道,只有一个可能的39种不同特定扩展名的档案在TXT(* .JPG * .gif注意* .png等* .xhtml * .xht)

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)

任何帮助/想法?

推荐答案

开始阅读的帮助,然后尝试在命令提示符处键入以下

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

您可以看到%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:\newfolder 
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天全站免登陆