简体   繁体   English

将子文件夹中的文件夹/文件递归移动到其父文件夹

[英]Recursively move folders/files within subfolders to its parent

I have a high level folder (call it 'level 0'), which contains a hundred or so subfolders ('level 1'), and within those subfolders consist of lower level subfolders and files ('level 2'). 我有一个高级别的文件夹(称为“ 0级”),其中包含一百个左右的子文件夹(“ 1级”),并且在这些子文件夹中包含较低级别的子文件夹和文件(“ 2级”)。 I need to dynamically move every file and folder from 'level 2' to its level 1 subfolder (or relatively speaking - its parent). 我需要将每个文件和文件夹从“级别2”动态移动到其级别1的子文件夹(相对而言,它是其父文件夹)。

To better visualize: 为了更好地可视化:

Master folder
├ Parent folder 1
│ ├─ Subfolder A
│ │  ├─ File A
│ │  └─ File B
│ ├─ Subfolder B
│ │  ├─ File C
│ │  └─ File D
│ ├─ File E
│ └─ File F
.
. ... many folders more ...
.
└─ Parent folder 134
   ├─ Subfolder CS
   │  ├─ File AGF
   │  └─ File ARH
   ├─ File ROQ
   └─ File JGL

I need to move everything from any subfolders' content within its parent folder. 我需要将所有内容从其父文件夹中任何子文件夹的内容中移出。 As above, you can see that there may be some files already in the parent folder (eg Files E, F) and they should stay put. 如上所述,您可以看到父文件夹中已经有一些文件(例如,文件E,F),这些文件应保持原样。

Objective: 目的:

Master folder
├─ Parent folder 1
│  ├─ File A
│  ├─ File B
│  ├─ File C
│  ├─ File D
│  ├─ File E
│  └─ File F
├─ Parent folder 119
│  ├─ File AZA
│  ├─ File AZB
│  ├─ File AZC
│  └─ File AZD
... and so on

The challenge here is that there are over a hundred of these parent folders beneath the master folder and all have different names. 这里的挑战是在主文件夹下有超过一百个这些父文件夹,并且它们都有不同的名称。 The subfolders within the parent folders also have different names as well. 父文件夹中的子文件夹也具有不同的名称。

I've tried approaching this using Get-ChildItem then with a 'ForEach' attempting to assign the child item (ie folder) as a variable and then performing another Get-ChildItem recursive and moving all content to the parent, but I'm getting nowhere and if any movement, it seems to be referring back to C:\\Windows\\ folder. 我尝试使用Get-ChildItem来解决这个问题,然后尝试使用'ForEach'尝试将子项(即文件夹)分配为变量,然后执行另一个Get-ChildItem递归并将所有内容移至父项,但是我正在无处可寻,似乎有什么动静,似乎是在指C:\\ Windows \\文件夹。

Note [sorry, correction Not*] literal code, but the approach, I'm thinking of: 注意[对不起,没有更正*]文字代码,但是我在想这种方法:

$master = "\\data\Master"
gci $master | foreach {
    $parent = $_
    gci ... | Move-Item -Destination $parent
}

I did try to make something for you. 我确实为你做点什么。 This will move everything from level2 folder to the level1 folder. 这会将所有内容从level2文件夹移至level1文件夹。 Pls try it, and let me know how its working. 请尝试一下,让我知道它是如何工作的。

$master = "C:\Temp\Level0\"
Get-ChildItem $master | ForEach-Object {
    $dest = ($_.fullname)+"\"
    $Loc = (Get-ChildItem $_.FullName | Select-Object -ExpandProperty fullname)+"\*"
    Move-item -Path $Loc -Destination $dest
}

This should do what you want. 这应该做您想要的。 It will however not move any files with the same filename to the parent folder if the filename has already been used. 但是,如果已经使用了文件名,它将不会将任何具有相同文件名的文件移动到父文件夹。

$FilePath = Read-Host "Enter FilePath"
$FileSource = Get-ChildItem -Path $FilePath -Directory

foreach ($Directory in $FileSource)
{
    $Files = Get-ChildItem -Path $Directory -File

    foreach ($File in $Files){
        Move-item -Path $File.Fullname -Destination $FilePath
    }
}

Hope that is of some use. 希望这是有用的。

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

相关问题 递归计算子文件夹中的文件 - Recursively count files in subfolders Powershell:将文件夹和子文件夹中的所有文件移动到单个文件夹中 - Powershell: Move all files from folders and subfolders into single folder 递归地将 setCaseSensitiveInfo 应用于所有文件夹和子文件夹 - Apply setCaseSensitiveInfo recursively to all folders and subfolders Powershell:如何在文件夹中递归创建文本文件? - Powershell: How can I create text files within folders recursively? 在不通过 Powershell 提示用户的情况下递归删除文件夹中的文件 - Recursively deleting files within folders without prompting the user via Powershell 在自己的组中递归移动文件夹 - Move folders recursively in their own group PowerShell脚本将文件和文件夹(包括子文件夹)从一个位置移动到x天以外的另一个位置 - PowerShell script to move files and folders including subfolders from one location to another older than x days 将 pdf 文件从子文件夹移动到 1 个主文件夹,并为多个文件夹重复使用脚本 - Move pdf files from subfolders to 1 main folder and re-use script for multiple folders PowerShell 将多个文件夹的子项移动到它们自己的子文件夹中 - PowerShell to move subitems of multiple folders into their own subfolders Powershell:递归移动文件 - Powershell: Move Files recursively
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM