如何仅使用cmd从子文件夹复制文件? [英] How to copy files from sub folder only using cmd?

查看: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从子文件夹复制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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