复制文件列表并递归使用Windows CMD

huangapple go评论53阅读模式
英文:

Copy list of files recursively using Windows CMD

问题

我有一个文本文件中的文件列表。

示例,t.txt 文件的内容:

a.txt
b.jpg
c.bat

我想要一个脚本来从源目录及其子目录中复制文件到特定目标位置。

目前,我在命令提示符(CMD)中使用以下命令:

for /f %I in (t.txt) do copy "C:\ProgramFiles\%I" . /y

这仅从 ProgramFiles 目录复制文件到当前目标位置。

如何修改此命令以允许搜索其子目录?

英文:

I have a list of files in a text file.

Example, content of t.txt file:

a.txt
b.jpg
c.bat

I want a script to copy files from a source directory, and its subdirectories, to a particular destination.

As of now, I am using the following within the Command Prompt (CMD):

for /f %I in (t.txt) do copy "C:\ProgramFiles\"%I . /y

This copies files only from ProgramFiles directory to the present destination.

How can I modify this, to allow the search of its subdirectories too?

答案1

得分: 0

你可以通过Windows的xcopy命令来轻松完成这个任务,类似这样:

@echo off
set "src=H:\JP and Stella\Part 1\Style\Logo\Free dom"
set "dest=C:\Users\starz\Downloads\Selected Photos\"

for /f "usebackq delims=" %%I in ("t.txt") do (
    for /r "%src%" %%J in ("*%%I") do (
        xcopy /y "%%J" "%dest%"
    )
)

在我的代码中,它会从t.txt中读取文件名,在H:\JP and Stella\Part 1\Style\Logo\Free dom目录及其子目录中搜索这些文件,并将它们复制到C:\Users\starz\Downloads\Selected Photos\目录。

英文:

you can easily do that via xcopy in windows(cmd), something like this :

@echo off
set "src=H:\JP and Stella\Part 1\Style\Logo\Free dom"
set "dest=C:\Users\starz\Downloads\Selected Photos\"

for /f "usebackq delims=" %%I in ("t.txt") do (
    for /r "%src%" %%J in ("*%%I") do (
        xcopy /y "%%J" "%dest%"
    )
)

in my code, it will read the file names from t.txt, search for them in the H:\JP and Stella\Part 1\Style\Logo\Free dom directory and its subdirectories, and copy them to the C:\Users\starz\Downloads\Selected Photos\ directory

is it ok ?

huangapple
  • 本文由 发表于 2023年6月18日 19:10:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定