使用批处理脚本递归复制文件,而无需使用Xcopy [英] Recursively Copy Files with Batch Script Without Using Xcopy

查看:227
本文介绍了使用批处理脚本递归复制文件,而无需使用Xcopy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用批处理(DOS)脚本以递归方式复制一组文件,并保持原始目录结构.听起来很简单,对吧?这是并发症:

I need to recursively copy a set of files using a batch (DOS) script, maintaining the original directory structure. Sounds easy, right? Here are the complications:

  1. xcopy在服务器上不可用,我将在其上使用批处理脚本.因此,我必须使用复制命令.
  2. 在Internet上对该主题进行彻底的搜索只会导致使用xcopy或对该脚本进行某些专门的使用(这只是一般的复制/粘贴作业).
  3. 自90年代以来,我还没有用DOS编写过.

如何获取复制命令以与旧名称/位置相同的名称/位置保存到新目录?

How do I get the copy command to save to a new directory with the same name/location as the old one?

推荐答案

这未经测试.该代码据说可以复制文件夹结构,但不能复制文件.如果测试似乎正确,请从copy命令中删除ECHO部分.第一个参数是"sourceDir",第二个参数是"targetDir".

This is untested. The code supposedly duplicate the folder structure, but not copy files. If the test seems to be correct, remove the ECHO part from the copy command. The first parameter is "sourceDir" and the second one is "targetDir".

编辑:修复了小细节

@echo off
if not exist %2 md %2
set targetDir=%~F2
cd %1
call :processFolder
goto :EOF

:processFolder
setlocal EnableDelayedExpansion
rem For each folder in this level
for /D %%a in (*) do (
   rem Enter into it, process it and go back to original
   cd %%a
   set "targetDir=%targetDir%\%%a"
   if not exist "!targetDir!" md "!targetDir!"
   ECHO copy *.* "!targetDir!"
   call :processFolder
   cd ..
)

这篇关于使用批处理脚本递归复制文件,而无需使用Xcopy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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