复制文件列表 (txt)

发布于 2024-11-14 07:20:00 字数 534 浏览 4 评论 0原文

我见过一些脚本示例,但似乎没有一个提供如何从 .t​​xt 列表中读取文件名的示例。

这个例子很好,以便将所有文件从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

固执像三岁 2024-11-21 07:20:00

给定名为 File-list.txt 的文件中的文件名列表,以下几行应该执行您想要的操作:

@echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
    xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)

Given your list of file names in a file called File-list.txt, the following lines should do what you want:

@echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
    xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
真心难拥有 2024-11-21 07:20:00

我只是尝试使用 Frank Bollack 和 sparrowt 的答案,但没有成功,因为它包含 xcopy 的 /U 开关。据我了解, /U 意味着只有当文件已存在于目标中时才会复制这些文件,这对我来说不是这种情况,而且对于原始提问者来说似乎也不是这种情况。它可能意味着 /V 表示验证,这更有意义。

删除 /U 开关解决了该问题。

@echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)

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.

@echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
萌梦深 2024-11-21 07:20:00

这将做到这一点:

@echo off
set src_folder=c:\batch
set dst_folder=c:\batch\destination
set file_list=c:\batch\file_list.txt

if not exist "%dst_folder%" mkdir "%dst_folder%"

for /f "delims=" %%f in (%file_list%) do (
    xcopy "%src_folder%\%%f" "%dst_folder%\"
)

This will do it:

@echo off
set src_folder=c:\batch
set dst_folder=c:\batch\destination
set file_list=c:\batch\file_list.txt

if not exist "%dst_folder%" mkdir "%dst_folder%"

for /f "delims=" %%f in (%file_list%) do (
    xcopy "%src_folder%\%%f" "%dst_folder%\"
)
∞觅青森が 2024-11-21 07:20:00

以下将从列表中复制文件并保留目录结构。例如,当您需要压缩在一系列 Git/SVN 提交中更改的文件时很有用。它还将处理目录/文件名中的空格,并适用于相对路径和绝对路径:

(基于这个问题:如何在批处理文件中的 for 循环内扩展两个局部变量)

@echo off

setlocal enabledelayedexpansion

set "source=input dir"
set "target=output dir"

for /f "tokens=* usebackq" %%A in ("file_list.txt") do (
    set "FILE=%%A"
    set "dest_file_full=%target%\!FILE:%source%=!"
    set "dest_file_filename=%%~nxA"
    call set "dest_file_dir=%%dest_file_full:!dest_file_filename!=%%"
    if not exist "!dest_file_dir!" (
        md "!dest_file_dir!"
    )
    set "source_file_full=%source%\!FILE:%source%=!"
    copy "!source_file_full!" "!dest_file_dir!"
)
pause

请注意,如果您的文件列表具有绝对路径,则必须将 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)

@echo off

setlocal enabledelayedexpansion

set "source=input dir"
set "target=output dir"

for /f "tokens=* usebackq" %%A in ("file_list.txt") do (
    set "FILE=%%A"
    set "dest_file_full=%target%\!FILE:%source%=!"
    set "dest_file_filename=%%~nxA"
    call set "dest_file_dir=%%dest_file_full:!dest_file_filename!=%%"
    if not exist "!dest_file_dir!" (
        md "!dest_file_dir!"
    )
    set "source_file_full=%source%\!FILE:%source%=!"
    copy "!source_file_full!" "!dest_file_dir!"
)
pause

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

半夏半凉 2024-11-21 07:20:00

这也将保留文件的原始文件目录:

@echo off
set src_folder=c:\whatever
set dst_folder=c:\target
set file_list=C:\file_list.txt

for /f "tokens=*" %%i in (%file_list%) DO (
   echo f | xcopy /E /C /R /Y "%src_folder%\%%i" "%dst_folder%\%%i"
)

This will also keep the files original file directory:

@echo off
set src_folder=c:\whatever
set dst_folder=c:\target
set file_list=C:\file_list.txt

for /f "tokens=*" %%i in (%file_list%) DO (
   echo f | xcopy /E /C /R /Y "%src_folder%\%%i" "%dst_folder%\%%i"
)
铃予 2024-11-21 07:20:00

也可以使用robocopy并且不使用for循环与xcopy - 可以解析参数中的文件列表。

robocopy Source_folder Destination_folder [files_to_copy] [options]

用于复制带有空格分隔符的字符串的文件。
例如:

robocopy . "d:\my folder" *.txt "my file one.cpp" file2.cpp
robocopy "d:\F 15" "d:\backup\F 15" /E

Also can use robocopy and Not use for loop with xcopy - can parse list of files in argument.

robocopy Source_folder Destination_folder [files_to_copy] [options]

Files to copy it's string with Space delimiter.
For example:

robocopy . "d:\my folder" *.txt "my file one.cpp" file2.cpp
robocopy "d:\F 15" "d:\backup\F 15" /E
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文