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.
Run the following script on
April reports
directory to move files up one level.The following line is to remove
dayreports
. Use it after you have already moved the files to parent folder. You can check if `dayreports' is empty. I skipped that part assuming you already moved the files.To make it a one liner just-
Something like this should do it:
There are 2 mistakes in your code:
Get-ChildItem *\*\*
enumerates thedayreport
folders (that's why the folder removal works), not the files in them. You needGet-ChildItem $files
orGet-ChildItem *\*\*\*
to enumerate the files.FileInfo
objects don't have a propertyParent
, onlyDirectoryInfo
objects do. Use the propertyDirectory
forFileInfo
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: