复制文件列表 (txt)
我见过一些脚本示例,但似乎没有一个提供如何从 .txt 列表中读取文件名的示例。
这个例子很好,以便将所有文件从 A 文件夹复制到 B 文件夹
xcopy c:\olddir\*.java c:\newdir /D /E /Q /Y
但我需要类似下一个的东西,我可以在其中实际填充源文件夹和目标文件夹:
@echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
而不是 src_folder = c:\whatever\*。 *
,那些*.*
需要是从txt文件读取的文件列表。
File-list.txt(示例)
file1.pds
filex.pbd
blah1.xls
有人可以建议我该怎么做吗?
I've seen some scripts examples over SO, but none of them seems to provide examples of how to read filenames from a .txt list.
This example is good, so as to copy all files from A to B folder
xcopy c:\olddir\*.java c:\newdir /D /E /Q /Y
But I need something like the next, where I can fill actually the source and destination folder:
@echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And instead of src_folder = c:\whatever\*.*
, those *.*
need to be list of files read from a txt file.
File-list.txt (example)
file1.pds
filex.pbd
blah1.xls
Could someone suggest me how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
给定名为
File-list.txt
的文件中的文件名列表,以下几行应该执行您想要的操作:Given your list of file names in a file called
File-list.txt
, the following lines should do what you want:我只是尝试使用 Frank Bollack 和 sparrowt 的答案,但没有成功,因为它包含 xcopy 的 /U 开关。据我了解, /U 意味着只有当文件已存在于目标中时才会复制这些文件,这对我来说不是这种情况,而且对于原始提问者来说似乎也不是这种情况。它可能意味着 /V 表示验证,这更有意义。
删除 /U 开关解决了该问题。
I just tried to use Frank Bollack and sparrowt's answer, but without success because it included a /U switch for xcopy. It's my understanding that /U means that the files will only be copied if they already exist in the destination which wasn't the case for me and doesn't appear to be the case for the original questioner. It may have meant to have been a /V for verify, which would make more sense.
Removing the /U switch fixed the problem.
这将做到这一点:
This will do it:
以下将从列表中复制文件并保留目录结构。例如,当您需要压缩在一系列 Git/SVN 提交中更改的文件时很有用。它还将处理目录/文件名中的空格,并适用于相对路径和绝对路径:
(基于这个问题:如何在批处理文件中的 for 循环内扩展两个局部变量)
请注意,如果您的文件列表具有绝对路径,则必须将
source
设置为绝对路径,如下所示出色地。[¹] 如果使用 Git,请参阅:仅导出在 Git 中具有文件夹结构的已修改和添加的文件
The following will copy files from a list and preserve the directory structure. Useful when you need to compress files which have been changed in a range of Git/SVN commits¹, for example. It will also deal with spaces in the directory/file names, and works with both relative and absolute paths:
(based on this question: How to expand two local variables inside a for loop in a batch file)
Note that if your file list has absolute paths, you must set
source
as an absolute path as well.[¹] if using Git, see: Export only modified and added files with folder structure in Git
这也将保留文件的原始文件目录:
This will also keep the files original file directory:
也可以使用robocopy并且不使用for循环与xcopy - 可以解析参数中的文件列表。
用于复制带有空格分隔符的字符串的文件。
例如:
Also can use robocopy and Not use for loop with xcopy - can parse list of files in argument.
Files to copy it's string with Space delimiter.
For example: