【问题标题】:search for a folder structure and copy to another folder in windows OS在 Windows 操作系统中搜索文件夹结构并复制到另一个文件夹
【发布时间】:2013-12-29 20:20:40
【问题描述】:

我正在寻找一个 Windows 批处理文件脚本来搜索特定名称并将其复制到另一个文件夹作为子目录。

我尝试使用 xcopy 命令将所有以 service/test.txt 结尾的子目录复制到另一个目录,完整目录名称如下:

xcopy .\alpha1\beta1\service\test .\copydir
xcopy .\alpha1\beta2\service\test .\copydir 

但是,我有许多文件结构要检查和复制。所以,我想知道是否有可以用来复制这些文件的脚本或命令。

【问题讨论】:

  • Windows DOS? Microsoft Windows 中的命令行界面与古老的 MS-DOS 操作系统无关。
  • 我认为这是一个与此线程非常相似的问题:[stackoverflow.com/questions/8609028/… [1]:stackoverflow.com/questions/8609028/…
  • 嗨,你是对的。它类似于上面的线程。这是详细信息。我有以下文件夹结构: /configs/abc1/server1/service/test.txt /configs/abc1/server1/website/test.txt /configs/abc2/service/test.txt /configs/abc2/website/test.txt .... .... /configs/abc30/server1/service/test.txt /configs/abc30/server1/website/test.txt 从上面的文件夹结构中,我想复制所有名为“服务”的子目录/test.txt" 以 /config/ 子文件夹名称开头到另一个文件夹。再次感谢您的帮助。

标签: windows batch-file


【解决方案1】:
@ECHO OFF
SETLOCAL 
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
MD %destdir% 2>nul
FOR /f "delims=" %%a IN (
  'dir /s /b /ad "%sourcedir%\*"^|findstr /l /i /e "\service\test" '
 ) DO (
 SET "target=%%a"
 CALL SET target=%%target:%sourcedir%=%destdir%%%
 CALL md "%%target%%\" 2>nul
 CALL XCOPY /s /e /v "%%a" "%%target%%\" >nul
)
GOTO :EOF

这应该可以完成任务。

它查看目录树,挑选出所有目录名 (dir../ad) 并过滤那些以“\service\test”结尾的目录名 (findstr../e..)

然后它将源名称分配给target,将源名称替换为目标名称,创建新的目标名称,抑制'already exists' 消息并将源内容复制到(新)目录。

【讨论】:

  • 大家好,我找到了以下解决方案: echo \Mors\ > Mors.txt xcopy /s /f /r /y /EXCLUDE:Mors.txt .\configs .\CopyTo Echo 命令创建一个.txt 文件,其中包含需要排除的文件夹名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-14
  • 2022-10-25
相关资源
最近更新 更多