简体   繁体   English

在Windows 7(cmd或PowerShell)上压缩子文件夹中的文件

[英]Flatten Files in Subfolders on Windows 7 (cmd or PowerShell)

I have a large volume of files organized in a very hierarchical folder structure. 我有大量的文件组织在一个非常分层的文件夹结构中。 In this structure, the file that I care about is always located in the lowest level of the folders. 在此结构中,我关心的文件始终位于文件夹的最低级别。 As such, I'd like to flatten the directory so that it's easier to access the files that I care about. 因此,我想展平目录,以便更容易访问我关心的文件。 However, I'd like to preserve the 2 higher levels (Person & Project) of the folder structure. 但是,我想保留文件夹结构的2个更高级别(Person&Project)。

Here's an example of the EXISTING folder directory: 以下是EXISTING文件夹目录的示例:

  • Directory 目录
    • Tom 汤姆
      • Project 1 项目1
        • Subfolder Level A 子文件夹级别A.
          • FileA FILEA
        • FileB FILEB
      • Project 2 项目2
        • Subfolder Level C 子文件夹级别C.
          • FileC FileC
        • FileD 提交
    • Jerry 杰瑞
      • Project 1 项目1
        • Subfolder Level E 子文件夹级别E.
          • FileE FileE

Here's an example of the DESIRED folder directory: 以下是DESIRED文件夹目录的示例:

  • Directory 目录
    • Tom 汤姆
      • Project 1 项目1
        • FileA FILEA
        • FileB FILEB
      • Project 2 项目2
        • FileC FileC
        • FileD 提交
    • Jerry 杰瑞
      • Project 1 项目1
        • FileE FileE

I have tried doing something like this, however this flattens all of the files into a single directory: 我尝试过这样的事情,但是这会将所有文件压缩成一个目录:

for /r %f in (*) do @copy "%f" .

However, this produces: 但是,这会产生:

  • Directory 目录
    • FileA FILEA
    • FileB FILEB
    • FileC FileC
    • FileD 提交
    • FileE FileE

I'd appreciate any guidance that you can offer. 我很感激您提供的任何指导。 Thanks a lot! 非常感谢!

Throw a couple of extra for loops around the one that works then. 在那个有效的循环周围抛出几个额外的循环。

eg First change to the name folder, then the project folder, looping through both levels. 例如,首先更改为名称文件夹,然后是项目文件夹,循环遍历两个级别。

for /D %n in (*) do (
    pushd %n
    for /D %p in (*) do (
        pushd %p
        for /r %f in (*) do @copy "%f" .
        popd
    )
    popd
)

If you put this in a bat file, remember to replace % with %% 如果你把它放在一个bat文件中,记得用%%替换%

Here is a Powershell approach. 这是一个Powershell方法。 It gets a list of the folders at the level that you want. 它获取所需级别的文件夹列表。 Then it moves all the sub files up to that level. 然后它将所有子文件移动到该级别。 it will also remove the sub folders. 它还将删除子文件夹。

$Rootfolder = Dir directory\*\* -Directory 

ForEach($folder in $Rootfolder)
{
    Dir $folder.fullname -Recurse -File | Copy-Item -Destination $folder.fullname
    Dir $folder.fullname -Recurse -Directory | Remove-Item -Force -Recurse -WhatIf
}

If you want it to delete, remove the -WhatIf from the last line. 如果要删除它,请从最后一行删除-WhatIf。

@echo off
pushd yourDirectory
for /d %%A in (*) do for /d %%B in ("%%A\*") do for /d %%C in ("%%B\*") do (
  pushd "%%C"
  for /r %%F in (*) do move /y "%%F" "%%B" >nul
  popd
  rd /q /s "%%C"
)
popd

%%A contains something like "yourDirectory\\Tom" %% A包含类似“yourDirectory \\ Tom”的内容
%%B contains something like "yourDirectory\\Tom\\Project 1" %% B包含类似“yourDirectory \\ Tom \\ Project 1”的内容
%%C contains something like "yourDirectory\\Tom\\Project 1\\subdirectory1" %%F contains a file to move, to any depth %% C包含类似“yourDirectory \\ Tom \\ Project 1 \\ subdirectory1”的内容%% F包含要移动到任何深度的文件

At first I thought I could eliminate PUSHD/POPD and use 起初我以为我可以消除PUSHD / POPD并使用

for /r "%%C" %%F in (*) do ...  THIS DOES NOT WORK

But that doesn't work - the value after /R cannot use a FOR variable or delayed expansion because of how the command is parsed. 但这不起作用 - / R之后的值不能使用FOR变量或延迟扩展,因为解析命令的方式。

I tweaked @ScottC's answer and used the following code: 我调整了@ ScottC的答案并使用了以下代码:

for /D %%n in (*) do (
    pushd %%n
    for /D %%p in (*) do (
        pushd %%p
        for /r %%f in (*.ppt) do (
            move "%%f" "[ROOT_PATH_THAT_I_WANT]\%%n\%%p".
        )
        popd
    )
    popd
)

I ran this solution as a .bat file, which is why I used %% instead of %. 我将此解决方案作为.bat文件运行,这就是我使用%%而不是%的原因。

%%n = name (aka C:\\Directory\\Name) %% n = name(又名C:\\ Directory \\ Name)
%%p = project (aka C:\\Directory\\Name\\Project) %% p =项目(又名C:\\ Directory \\ Name \\ Project)
%%f = file to be moved (recursively drilling through the folders and moving them up to the project level) %% f =要移动的文件(递归浏览文件夹并将它们移动到项目级别)

Ultimately, I wasn't able to get @dbenham's suggestion of deleting the empty folders to work, so I ended up using this utility: http://www.jonasjohn.de/red.htm . 最终,我无法得到@ dbenham关于删除空文件夹的建议,所以我最终使用了这个实用工具: http//www.jonasjohn.de/red.htm So far it seems pretty intuitive and like it's taking care of the problem without much effort from me :) 到目前为止它看起来非常直观,就像它在没有我太多努力的情况下处理问题:)

Thanks for the help everybody! 感谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM