[英]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.