如何仅使用cmd从子文件夹复制文件?

编程入门 行业动态 更新时间:2024-10-22 11:24:46
本文介绍了如何仅使用cmd从子文件夹复制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的文件夹结构如下:

-MainDir -f0 -f0 -f0 -a.txt -b.txt -c.txt -f1 -f1 -f1 -aa.txt -bb.txt -cc.txt

如果您在上方看到我的文件,则它们位于层次结构的第3个文件夹中.现在,我的要求是仅能够将主文件夹及其文件复制到另一个位置.例如:以上结构的输出应如下所示:

If you see above my files are in 3rd folder in terms of hierarchy. Now my requirement is to be able to copy only the main folder and it's files to another location. For example: output for above structure should be as following:

-MainDir -f0 -a.txt -b.txt -c.txt -f1 -aa.txt -bb.txt -cc.txt

但是当我尝试使用XCOPY时,所有文件夹都照原样复制.因此,我试图找出一种实现目标文件夹结构的方法,如上所示

but when I try using XCOPY, all the folders get copied as it is. So I am trying to figure out a way to achieve my target folder structure as shown above

推荐答案

以下内容将循环查找main下的目录,查找所有文件,并将它们全部放入目标位置中最顶层的子目录中:

The following will loop the directories under main, find all files, and put them all in the top most sub directory in destination as you're looking for:

CMD脚本:

@(SETLOCAL ECHO OFF SET "_MainDir=C:\Main\Dir" SET "_DestDir=%Temp%\Destination\Main\Dir" ) FOR /D %%A in ( %_MainDir%\* ) DO ( IF NOT EXIST "%_DestDir%\%%~nxA" MD "%_DestDir%\%%~nxA" FOR /F %%a IN (' DIR /A-D /S /B "%%~fA\*" ') DO ( ECHO Copying: "%%~fa" ECHO To: "%_DestDir%\%%~nxA\%%~nxa" COPY /B /V /Y "%%~fa" "%_DestDir%\%%~nxA\%%~nxa" ) )

更多推荐

如何仅使用cmd从子文件夹复制文件?

本文发布于:2023-11-09 21:09:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573444.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   文件   从子   cmd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!