问题
I'm having a windows server 2008 machine on which i've a folder structure like
root_folder
...........username1
....................project1
............................category
....................................subproject1
....................................subproject2
....................................subproject3
....................project2
............................category
....................................subproject1
....................................subproject2
....................................subproject3
...........username2
....................project1
............................category
....................................subproject1
....................................subproject2
....................................subproject3
....................project2
............................category
....................................subproject1
....................................subproject2
....................................subproject3
Now here what i need to do is remove the category folder and copy its content directly under the respective project(x) folder. Somewhat like this
root_folder
...........username1
....................project1
....................................subproject1
....................................subproject2
....................................subproject3
....................project2
....................................subproject1
....................................subproject2
....................................subproject3
-----------------------------------------------------------
...........username2
....................project1
............................subproject1
............................subproject2
............................subproject3
....................project2
............................subproject1
............................subproject2
............................subproject3
-----------------------------------------------------------
How this can be done with powershell script. Also what would be the best a powershell script or a batch file.
回答1:
EDIT: I added a modification in order to also move the files present in category folder.
@echo off
rem Enter into Root Folder
cd \root_folder
rem Process each User Name folder
for /D %%u in (*) do (
rem Enter into the user name folder
cd "%%u"
rem Process each Project
for /D %%p in (*) do (
rem Enter into the category of this project
cd "%%p\category"
rem Move all subprojects one level up
for /D %%s in (*) do move "%%s" ..
rem Move all files one level up
move *.* ..
rem Go back one level up to project folder
cd ..
rem Remove the now empty category folder
rd category
rem Go back one level up to user folder
cd ..
)
rem Go back one level up to root folder
cd ..
)
回答2:
I had approximately the same problem. I wrote something like this:
$category_list = Get-ChildItem *\*\*
Get-ChildItem *\*\*\* | % { Move-Item $_.FullName (($_.Parent).Parent).FullName }
$category_list | Remove-Item -Recurse
来源:https://stackoverflow.com/questions/15946964/powershell-script-to-move-folders-one-level-up-and-delete-the-previous-containin