synaesthesia Posted July 28, 2012 Posted July 28, 2012 Right, I'll try and explain this. Lots and lots of homefolders. Each one has a subfolder "appdata" thanks to previous roaming profiles. And within that folder are the users "favorites" (spot the RM network - gee, thanks RM!) Need to move that favorites folder into the root of the homefolder for each user. Any fast and furious ways of doing that? Bear in mind I used powershell for the first time this Friday
ChrisH Posted July 28, 2012 Posted July 28, 2012 Probably less lines in batch for a change but....... Use at your own risk as this coding session was fuelled by some real ale! I mocked up a dummy test folder environment like yours and it worked fine but take a snapshot first etc! $basePath = "d:\homedirs" $subPath = "\Appdata\favorites" $folders = dir -path $basePath $moveTo = "" $fullPath = "" foreach ($folder in $folders) { if ($folder.Attributes -eq "Directory") { $fullPath = $folder.fullname + $subPath # write-host $fullpath if (Test-Path -path $fullpath) # see if the favorites folder exists { write-host "folder found in " $fullpath $moveTo = $basePath + "\" + $folder.name write-host $moveTo mv $fullpath $moveTo $moveTo = "" $fullPath = "" } } }
synaesthesia Posted July 28, 2012 Author Posted July 28, 2012 (edited) quick edit: ignore what I've just said, let's not name the script something stupid like "move.ps1" You sir, are a star Edited July 28, 2012 by synaesthesia
synaesthesia Posted July 29, 2012 Author Posted July 29, 2012 Strange one. It moves a small batch of random user favorites folders but not the rest. Checked permissions and their fine :\ Made test folders in the same location - works fine. Strange!
ChrisH Posted July 29, 2012 Posted July 29, 2012 (edited) They haven't got the proper spelling or something for some reason have they? Just looking are there any dollar signs in your paths? That could cause issues as well. Edited July 29, 2012 by ChrisH
ChrisH Posted July 29, 2012 Posted July 29, 2012 Okay here is the same code with more logging. $basePath = "d:\homedirs" $subPath = "\Appdata\favorites" $folders = dir -path $basePath $moveTo = "" $fullPath = "" $logFile = "MoveLog.txt" New-Item $logFile -type file -force foreach ($folder in $folders) { if ($folder.Attributes -eq "Directory") { Add-Content $logFile "***********************************" Add-Content $logFile ("`r`nCurrent folder = " + $folder.fullname) $fullPath = $folder.fullname + $subPath if (Test-Path -path $fullpath) # see if the favorites folder exists { Add-Content $logFile ("Favorites folder found in " + $fullpath) write-host "`r`nFavorites folder found in " $fullpath $moveTo = $basePath + "\" + $folder.name write-host "Moving folder to " $moveTo mv $fullpath $moveTo Add-Content $logFile ("`r`nThe status of the move operation was " + $?) $moveTo = "" $fullPath = "" } Else { Add-Content $logFile ("Favorites folder NOT found in " + $fullpath) write-host "`r`nFavorites folder NOT found in " $fullpath } } }
synaesthesia Posted July 29, 2012 Author Posted July 29, 2012 As expected that gives the same result but tells me there's a few folders (the few that did move in the first place) where the my settings\favorites folder is not present. (the first two bits changed to E:\Users\Students 2005 and \My Settings\Favorites as appropriate)
ChrisH Posted July 29, 2012 Posted July 29, 2012 So what do the lines "The status of the move operation was..." say?
synaesthesia Posted July 29, 2012 Author Posted July 29, 2012 (edited) PS E:\> .\newmove.ps1 Directory: E:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 29/07/2012 12:54 0 MoveLog.txt Favorites folder NOT found in E:\Users\Students\Entry 2005\05xxxx\My Settings\favorites Favorites folder NOT found in E:\Users\Students\Entry 2005\05xxxx\My Settings\favorites Favorites folder NOT found in E:\Users\Students\Entry 2005\05xxxx\My Settings\favorites Favorites folder NOT found in E:\Users\Students\Entry 2005\05xxxx\My Settings\favorites Favorites folder NOT found in E:\Users\Students\Entry 2005\05xxxx\My Settings\favorites Favorites folder NOT found in E:\Users\Students\Entry 2005\05xxxx\My Settings\favorites Favorites folder NOT found in E:\Users\Students\Entry 2005\05test\My Settings\favorites PS E:\> Mirrored with formatting in the .log with nothing else said. This is why it feels to me like permissions or something. Edited July 29, 2012 by synaesthesia
ChrisH Posted July 29, 2012 Posted July 29, 2012 I thought the spaces in the paths were maybe messing it up some how but I tried a path with spaces and it was fine. Try launching a powershell prompt and doing the move command interactively and see what happens.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now