+ Post New Thread
Results 1 to 10 of 10
Scripts Thread, Script/bulk moving folders in Coding and Web Development; Right, I'll try and explain this. Lots and lots of homefolders. Each one has a subfolder "appdata" thanks to previous ...
  1. #1

    synaesthesia's Avatar
    Join Date
    Jan 2009
    Location
    Northamptonshire
    Posts
    5,843
    Thank Post
    570
    Thanked 994 Times in 766 Posts
    Blog Entries
    15
    Rep Power
    460

    Script/bulk moving folders

    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

  2. #2
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,996
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    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!

    Code:
    $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 = ""
                }
          }
    }

  3. #3

    synaesthesia's Avatar
    Join Date
    Jan 2009
    Location
    Northamptonshire
    Posts
    5,843
    Thank Post
    570
    Thanked 994 Times in 766 Posts
    Blog Entries
    15
    Rep Power
    460
    quick edit: ignore what I've just said, let's not name the script something stupid like "move.ps1"

    You sir, are a star
    Last edited by synaesthesia; 28th July 2012 at 09:53 PM.

  4. #4

    synaesthesia's Avatar
    Join Date
    Jan 2009
    Location
    Northamptonshire
    Posts
    5,843
    Thank Post
    570
    Thanked 994 Times in 766 Posts
    Blog Entries
    15
    Rep Power
    460
    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!

  5. #5
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,996
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    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.
    Last edited by ChrisH; 29th July 2012 at 10:55 AM.

  6. #6
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,996
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    Okay here is the same code with more logging.

    Code:
    $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
                }
          }
    }

  7. #7

    synaesthesia's Avatar
    Join Date
    Jan 2009
    Location
    Northamptonshire
    Posts
    5,843
    Thank Post
    570
    Thanked 994 Times in 766 Posts
    Blog Entries
    15
    Rep Power
    460
    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)

  8. #8
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,996
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    So what do the lines "The status of the move operation was..." say?

  9. #9

    synaesthesia's Avatar
    Join Date
    Jan 2009
    Location
    Northamptonshire
    Posts
    5,843
    Thank Post
    570
    Thanked 994 Times in 766 Posts
    Blog Entries
    15
    Rep Power
    460
    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.
    Last edited by synaesthesia; 29th July 2012 at 04:14 PM.

  10. #10
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,996
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106
    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.

SHARE:
+ Post New Thread

Similar Threads

  1. Script to change folder permissions
    By scottyses in forum General Chat
    Replies: 1
    Last Post: 15th January 2008, 08:52 AM
  2. Pupils moving folders
    By wadeeuk in forum How do you do....it?
    Replies: 14
    Last Post: 14th November 2007, 07:08 AM
  3. VB Script to create folders
    By markesmith in forum Scripts
    Replies: 2
    Last Post: 26th April 2007, 03:03 PM
  4. Need to carry out bulk home folder move to another drive
    By projector1 in forum How do you do....it?
    Replies: 4
    Last Post: 22nd January 2007, 07:50 PM
  5. Prevent users from moving folders ?
    By pooley in forum Windows
    Replies: 2
    Last Post: 9th May 2006, 10:30 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •