简体   繁体   English

在自己的组中递归移动文件夹

[英]Move folders recursively in their own group

I have 2 folders like this我有 2 个这样的文件夹

Alfa X - Volume 1
Alfa X - Volume 2

This script这个脚本

$Folder    = Get-ChildItem -Path C:\Some\Path -Filter "*volume*" -Directory -Recurse #| ForEach-Object -Process { Move-Item -Path $_.FullName -Destination C:\Some\OtherFolder }
$Folder1   = $Folder.FullName.split('\')[-1]
$newFolder =  "$($Folder1.Split(' ')[0])" + " $($Folder1.Split(' ')[1])"
If(-not (Test-Path -Path C:\Some\Path\$newFolder)){
    New-Item -Path C:\some\path -Name $newFolder -ItemType Directory -OutVariable Location
    }

    
Foreach($Directory in $Folder.FullName){
    Move-Item -Path $Directory -Destination $Location.FullName}

create (if not exists) and move these folders into a folder called创建(如果不存在)并将这些文件夹移动到一个名为

Alfa X -

What is the problem?问题是什么? it doesn't work recursively.它不能递归工作。 I try to explain.我试图解释。 If I have a set of folders like this如果我有一组这样的文件夹

Alfa X - Volume 1
Alfa X - Volume 2
beta - volume 4
beta - volume 6

This script moves all folders in Alfa X in this way此脚本以这种方式移动 Alfa X 中的所有文件夹

Alfa X -
|
|
|----- Alfa X - Volume 1
|----- Alfa X - Volume 2
|----- beta - volume 4
|----- beta - volume 6

But I don't want this.但我不想要这个。 I wish that it moves folders in this way我希望它以这种方式移动文件夹

Alfa X
|
|---- Alfa X - Volume 1
|---- Alfa X - Volume 2
|
beta
|
|---- beta - volume 4
|---- beta - volume 6

Question : is it possible to move folders recursively in their own group?问题:是否可以在自己的组中递归移动文件夹? By 'own group' I mean folders that have the same name until they find the word 'volume' or a keyword in their name that differentiate them “自己的组”是指具有相同名称的文件夹,直到他们找到“卷”一词或名称中的关键字来区分它们

I'd recommend to use separated source and target directories but if I got it right something like this should work actually:我建议使用分开的源目录和目标目录,但如果我做对了,这样的东西实际上应该可以工作:

$SourcePath = 'C:\Some\Path'
$TargetPath = 'C:\someOther\path'

$FolderList = Get-ChildItem -Path $SourcePath -Filter "*volume*" -Directory 

foreach ($Folder in $FolderList) {
    $Name1 = ($Folder.Name -split '-')[0].Trim()
    $TargetFolder = Join-Path -Path $TargetPath -ChildPath $Name1
    if (-not (Test-Path -Path $TargetFolder)) {
        New-Item -Path $TargetFolder -ItemType Directory | Out-Null
    }
    Move-Item -Path $Folder.FullName -Destination $TargetFolder
}

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

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