【问题标题】:Move content of each sub-subdirectory to folder将每个子目录的内容移动到文件夹
【发布时间】:2019-10-14 08:32:40
【问题描述】:

我有一个文件夹结构,其中Folder1 有子目录,每个子目录又包含一个同名的子目录。我想从那个目录将所有子目录移动到Folder0。有同名的子目录,所以将其内容移动到Folder0的相应子目录中。

例如:

Folder1/Fotos Teil 1 von 178/Fotos Teil 1 von 178/Photos/* 应移至Folder0/Photos/*

这是结构

Folder0
├─Photos
├─Memories
├─Albums and Favorites
│
Folder1
├─Fotos Teil 1 von 178
│ └─Fotos Teil 1 von 178
│   ├─Photos
│   ├─Memories
│   └─Albums and Favorites
│
├─Fotos Teil 2 von 178
│ └─Fotos Teil 2 von 178
│   ├─Photos
│   ├─Memories
│   └─Albums and Favorites
╎
.
.
.
╎
├─Fotos Teil 178 von 178
│ └─Fotos Teil 178 von 178
│   ├─Photos
│   └─Albums and Favorites

我知道这可能是 PowerShell 的工作。我只使用过 bash 脚本,但这需要在 Windows 中完成。

【问题讨论】:

  • 所以您希望将更深的photos 目录中的所有文件移动到顶级photos 目录?对更深层次的其他目录也要这样做吗?
  • 是的,就是这样。将“Folder1/foo/foo/”中的所有文件夹移动到“Folder0”。如果它们已经存在(例如“照片”),请移动包含的文件。
  • 当您使用Get-ChildItem 加载目录信息对象时,您可以使用.Parent 属性来查找父目录。你可以做.Parent.Parent 来获取两个父目录......然后比较它们。如果它们相同,则您有一个双名目录。 ///// 然而,一种更简单的方法可能是获取任何具有所需名称但不在顶层的目录。

标签: powershell


【解决方案1】:

这是一个简短的 sn-p(未经测试!)。
是否可以帮助您了解逻辑以及如何在 PowerShell 中执行此操作。

$Source = '.\Folder1'
$Destination = '.\Folder0'

foreach ($Folder in (Get-ChildItem -Path ($Source + '\*\*\'))) #For each folder -> Photos / Memories / Albums and Favorites
{
    foreach ($Item in (Get-ChildItem -Path $Folder.FullName)) #Get all items
    {
        Move-Item -Path $Item.FullName -Destination ($Destination + '\' + $Folder.Name) #Move the items
    }
}

【讨论】:

    猜你喜欢
    • 2011-07-07
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2021-11-29
    相关资源
    最近更新 更多