6

I have a folder called "April reports" that contains a folder for each day of the month. Each folder then contains another folder which contains PDF files:

April reports
├─01-04-2018
│ └─dayreports
│   ├─approved.pdf
│   └─unapproved.pdf
│
├─02-04-2018
│ └─dayreports
│   ├─approved.pdf
│   └─unapproved.pdf
╎
╎
└─30-04-2018
  └─dayreports
    ├─approved.pdf
    └─unapproved.pdf

The PDFs have the same name for each day so the first thing I want to do is move them up one level so that I can use the folder name containing the date to rename each file so that it will contain the date. The script I have tried is this (with the path set at "April Reports") :

$files = Get-ChildItem *\*\*
Get-ChildItem *\*\* | % {
    Move-Item $_.FullName (($_.Parent).Parent).FullName
}
$files | Remove-Item -Recurse

The step to delete the extra folders "dayreports" works but the files have not been moved.

2 Answers 2

8

There are 2 mistakes in your code:

  • Get-ChildItem *\*\* enumerates the dayreport folders (that's why the folder removal works), not the files in them. You need Get-ChildItem $files or Get-ChildItem *\*\*\* to enumerate the files.

  • FileInfo objects don't have a property Parent, only DirectoryInfo objects do. Use the property Directory for FileInfo objects. Also, dot-access can usually be daisy-chained, so all the parentheses aren't required.

Not a mistake, but an over-complication: Move-Item can read directly from the pipeline, so you don't need to put it in a loop.

Change your code to something like this, and it will do what you want:

$files = Get-ChildItem '*\*\*'
Get-ChildItem $files | Move-Item -Destination { $_.Directory.Parent.FullName }
$files | Remove-Item -Recurse
0

Something like this should do it:

$rootPath = "<FULL-PATH-TO-YOUR-April reports-FOLDER>"

Get-ChildItem -Path $rootPath -Directory | ForEach-Object {
    # $_ now contains the folder with the date like '01-04-2018'
    # this is the folder where the .pdf files should go
    $targetFolder = $_.FullName
    Resolve-Path "$targetFolder\*" | ForEach-Object {
        # $_ here contains the fullname of the subfolder(s) within '01-04-2018'
        Move-Item -Path "$_\*.*" -Destination $targetFolder -Force
        # delete the now empty 'dayreports' folder
        Remove-Item -Path $_
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.