Move files up one folder level

ellemgcf picture ellemgcf · Jul 15, 2018 · Viewed 10.1k times · Source

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.

Answer

Ansgar Wiechers picture Ansgar Wiechers · Jul 15, 2018

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