如何使用xcopy从文本文件复制文件和文件夹结构? [英] How do I copy file and folder structure using xcopy from a text file?

查看:263
本文介绍了如何使用xcopy从文本文件复制文件和文件夹结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中包含文件和文件夹的列表.我想做的是使用xcopy复制文本文件中写的内容.我的文本文件如下所示:

I have a text file containing a list of files and folders. What I want to do is use xcopy to replicate what is written in the text file. My text file looks like this:

"C:\FOLDER"  
"C:\FOLDER\FILE1.TXT"
"C:\FOLDER\FILE2.TXT"
"C:\FOLDER\FOLDER2"
"C:\FOLDER\FOLDER2\FILE3.TXT"

对于给定的输出目录"C:\OUTPUT",我想复制整个结构,所以:

For a given output directory "C:\OUTPUT" I would like to replicate the entire structure, so:

"C:\OUTPUT\FOLDER"  
"C:\OUTPUT\FOLDER\FILE1.TXT"
"C:\OUTPUT\FOLDER\FILE2.TXT"
"C:\OUTPUT\FOLDER\FOLDER2"
"C:\OUTPUT\FOLDER\FOLDER2\FILE3.TXT"

我该怎么做?到目前为止,我已经编写了一个for循环,该循环读取文件的每一行,但是如果该行是文件夹,它将复制所有文件.我只想复制并创建文本文件中提到的文件和文件夹.

How can I accomplish this? So far I have written a for loop that reads in each line of the file, but it copies all files if the line is a folder. What I want to do is only copy and create the files and folders that are mentioned in the text file.

@echo off
for /f "delims=] tokens=1*" %%a in (textfile.txt) do (
   XCOPY /S /E %%a "C:\OUTPUT"
)

我在正确的轨道上吗?

感谢您和最诚挚的问候,

Thank you and best regards,

安德鲁

推荐答案

是的,您很亲密.只需使用现有路径作为附加的目标路径.

Yes, you are close. Just need to use the existing path as the appended destination path.

@echo off
for /f "delims=" %%A in (textfile.txt) do if exist "%%~fA\*" (
    md "C:\Output\%%~pA"
    copy /y "%%~fA" "C:\Output\%%~pnxA"
)


原始

如果%%A ="C:\ Folder \ Folder2 \ File3.txt",则%%~pA =文件夹\ Folder2


Original

If %%A = "C:\Folder\Folder2\File3.txt", then %%~pA = Folder\Folder2

@echo off
for /f "delims=" %%A in (textfile.txt) do (
    md "C:\Output\%%~pA"
    if not exist "%%~fA\*" echo f | xcopy "%%~fA" "C:\Output\%%~pnxA" /y
)

if not exist "%%~fA\*"确保仅复制不是目录的条目. 有关更多信息,请参见参考技术和评论

The if not exist "%%~fA\*" makes sure to only copy the entry if it is not a directory. See Reference for more Techniques and Comments

在命令行中输入for /?以查看变量修饰符的列表. %%~A将从变量中删除周围的引号(如果有).

Type in for /? at the command line to view a list of the variable modifiers. %%~A will remove the surrounding quotations (if any) from the variable.

有关xcopy提示问题的帖子.备用设置,因为您很可能不需要xcopy功能.

Alternate Setup, since you most likely will not need the xcopy abilities.

@echo off
for /f "delims=" %%A in (textfile.txt) do (
    md "C:\Output\%%~pA"
    if not exist "%%~fA\*" copy /y "%%~fA" "C:\Output\%%~pnxA"
)

这篇关于如何使用xcopy从文本文件复制文件和文件夹结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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