问题
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.
回答1:
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:
$files = Get-ChildItem '*\*\*'
Get-ChildItem $files | Move-Item -Destination { $_.Directory.Parent.FullName }
$files | Remove-Item -Recurse
回答2:
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 $_
}
}
回答3:
Run the following script on April reports
directory to move files up one level.
Get-ChildItem -Recurse -Filter "dayreports" | Get-ChildItem | Move-Item -Destination {$_.Directory.Parent.FullName}
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.
Get-ChildItem -Recurse -Filter "dayreports" | Remove-Item -Recurse
To make it a one liner just-
Get-ChildItem -Recurse -Filter "dayreports" | Get-ChildItem | Move-Item -Destination {$_.Directory.Parent.FullName}; Get-ChildItem -Recurse -Filter "dayreports" | Remove-Item -Recurse
来源:https://stackoverflow.com/questions/51346965/move-files-up-one-folder-level