PowerShell Script to move folders one level up and delete the previous containing folder

戏子无情 提交于 2019-12-14 04:01:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!