复制第一级文件夹并合并子文件夹中的所有文件 [英] Copy first level folder and consolidate all the files within subfolders

查看:265
本文介绍了复制第一级文件夹并合并子文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主目录上有多个文件夹s1,s2 ..... sn.这些文件夹中的每一个都有多个子文件夹,文件夹中有文件夹,然后是文件.结构看起来像:

I have multiple folders s1,s2.....sn on main directory. Each of these folders have multiple sub folders, folders within folders and then files. Structure kind of looks like:

mainDir

  -s1
       -f1
         -c1
           -a.txt
           -b.txt
         -c2
           -m.txt
           -n.txt
       -f2
         -c1
           -aaa.txt
           -bbb.txt
         -c2
           -mmm.txt
           -nnn.txt
     -s2
       -f1
         -c1
           -a.txt
           -b.txt
         -c2
           -m.txt
           -n.txt
       -f2
         -c1
           -aaa.txt
           -bbb.txt
         -c2
           -mmm.txt
           -nnn.txt
     -s3 ..................

现在,我们需要除去所有子文件夹,并仅保留.一级文件夹和所有基础文件.所以最终目的地应该是:

Now we need to get rid of all subfolders and keep only . the first level folders and all the underlying files. So final destination should like:

DestDirectory
      -s1
        -a.txt
        -b.txt
        -m.txt
        -n.txt
        -aaa.txt
        -bbb.txt
        .....so on
      -s2
        -a.txt
        -b.txt
        -m.txt
        -n.txt
        -aaa.txt
        -bbb.txt
        .....so on

推荐答案

我将使用 for /D循环进行迭代第一级子目录,然后是嵌套的 for /R循环遍历直接子目录中的所有文件,然后使用 copy 分别复制每个文件,以实现统一的目标目录层次结构.为避免文件名冲突(意味着具有相同名称的文件出现在不同的源子目录层次结构级别中),可以使用if exist指令.所以这是代码:

I would use a for /D loop to iterate the first-level sub-directories, then a nested for /R loop to iterate through all files in the immediate sub-directories, then use copy to copy each file individually in order to achieve the flat destination directory hierarchy. To avoid file name collisions (meaning that files with the same name appear in different source sub-directory hierarchy levels), an if exist directive can be used. So here is the code:

rem // Loop through immediate sub-sirectories of main directory:
for /D %%I in ("D:\MainDir\*") do (
    rem // Temporarily change to the currently iterated sub-directory:
    pushd "%%~I" && (
        rem // Iterate through all files in the current directory recusrively:
        for /R %%J in ("*.*") do (
            rem /* Create destination directory if necessary, which is named like
            rem    the current directory, that is the first source sub-directory: */
            2> nul md "D:\CopyDir\%%~nxI"
            rem // Temporarily change to the destination directory:
            pushd "D:\CopyDir\%%~nxI" && (
                rem // Check for potential file name collisions:
                if not exist "%%~nxJ" (
                    rem // No collision, hence copy current file:
                    copy "%%~J" "%%~nxJ"
                ) else (
                    rem // Collision, so return an error message:
                    >&2 echo ERROR: "%%~nxI\%%~nxJ" already exists!
                )
                rem // Return from destination directory:
                popd
            )
        )
        rem // Return from currently iterated source sub-directory:
        popd
    )
)

这篇关于复制第一级文件夹并合并子文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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