Jump to content

Recommended Posts

Posted

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 ;)

Posted

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 = ""
           }
     }
}

Posted
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!
Posted (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 by ChrisH
Posted

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
           }
     }
}

Posted
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)
Posted (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 by synaesthesia
Posted
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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...