Home > OS >  Powershell move to folder in the same directory
Powershell move to folder in the same directory

Time:12-25

I have 20000 more folders under Z:\testfolder, it's a network mapped drive. I need to find all the folders with specific name like "01-Jan","02-Feb","03-Mar","04-Apr","05-May","06-Jun","07-Jul","08-Aug","09-Sep","10-Oct","11-Dec","12-Dec", and move these folders to the folder "2022" under the same directory. All folders are empty folders. The folder "2022" needs to be created.

I already created the 2022 folder using the following command under a directory with these files:

Get-ChildItem -Path Z:\testfolder -Filter 01-Jan -Directory -Recurse -Exclude 2020 -ErrorAction SilentlyContinue -Force | Copy-Item -Destination {$($_.Fullname) -replace '01-Jan',"2022"}

But I don't know how to move the folder to the current folder.

Current Tree:

Z:.
├───A
│   ├───01-JAN
│   ├───02-Feb
│   ├───03-Mar
│   ├───04-Apr
│   ├───05-May
│   ├───06-Jun
│   ├───07-Jul
│   ├───08-Aug
│   ├───09-Sep
│   ├───10-Oct
│   ├───11-Nov
│   └───12-Dec
├───B
│   └───D
│       ├───01-JAN
│       ├───02-Feb
│       ├───03-Mar
│       ├───04-Apr
│       ├───05-May
│       ├───06-Jun
│       ├───07-Jul
│       ├───08-Aug
│       ├───09-Sep
│       ├───10-Oct
│       ├───11-Nov
│       └───12-Dec
└───C
    ├───01-JAN
    ├───02-Feb
    ├───03-Mar
    ├───04-Apr
    ├───05-May
    ├───06-Jun
    ├───07-Jul
    ├───08-Aug
    ├───09-Sep
    ├───10-Oct
    ├───11-Nov
    └───12-Dec

The expected effect:

Z:.
├───A
│   └───2022
│       ├───01-Jan
│       ├───02-Feb
│       ├───03-Mar
│       ├───04-Apr
│       ├───05-May
│       ├───06-Jun
│       ├───07-Jul
│       ├───08-Aug
│       ├───09-Sep
│       ├───10-Oct
│       ├───11-Nov
│       └───12-Dec
├───B
│   └───D
│       └───2022
│           ├───01-Jan
│           ├───02-Feb
│           ├───03-Mar
│           ├───04-Apr
│           ├───05-May
│           ├───06-Jun
│           ├───07-Jul
│           ├───08-Aug
│           ├───09-Sep
│           ├───10-Oct
│           ├───11-Nov
│           └───12-Dec
└───C
    └───2022
        ├───01-Jan
        ├───02-Feb
        ├───03-Mar
        ├───04-Apr
        ├───05-May
        ├───06-Jun
        ├───07-Jul
        ├───08-Aug
        ├───09-Sep
        ├───10-Oct
        ├───11-Nov
        └───12-Dec

Thanks

CodePudding user response:

This is untested, but may be in the correct, general direction. The 2020 directory is created if it does not exist. When you are confident that the directory will be moved correctly, remove the -WhatIf from the Move-Item command.

$DirNames = @('01-jan', '02-feb', '03-mar','04-apr', '05-may', '06-jun',
    '07-jul', ' 08-aug', '09-sep', '10-oct', '11-nov', '12-dec')
Get-ChildItem -Recurse -Directory -Path 'Z:\' |
    Where-Object { $DirNames -contains $_.Name } |
    ForEach-Object {
        $NewDir = Join-Path -Path $_.Directory -ChildPath '2020'
        if (-not (Test-Path -Path $NewDir)) {
            New-Item -ItemType Directory -Path $NewDir | Out-Null
        }
        Move-Item -Path $_.FullName -Destination $NewDir -WhatIf
    }

CodePudding user response:

This should do it:

$folderNames = 1..12 | ForEach-Object { "$('{0:d2}' -f $_)-$([cultureinfo]::InvariantCulture.DateTimeFormat.GetAbbreviatedMonthName($_))" }
Get-ChildItem -Path 'Z:\testfolder' -Recurse | Where-Object BaseName -In $folderNames | Group-Object -Property Parent | ForEach-Object {
    $dir2022 = Join-Path -Path $_.Name -ChildPath '2022'
    New-Item -Path $dir2022 -ItemType Directory
    foreach ($dir in $_.Group) {
        Move-Item -Path $dir.FullName -Destination $dir2022
    }
}

$folderNames will contain only the names 01-Jan...12-Dec. First, we get all the directories recursively in Z:\testfolder then we select only the folders that match the $folderNames. We then group them by their parent such that we retain the structure B\D for example. We then iterate the groups and create a 2022 directory B\D\2022. Finally, for each member in the group, we will move them in the 2022 dir (i.e. B\D\01-Jan to B\D\2022\01-Jan).

  • Related