Move-Item - Beginner

Author Message
sransell

  • Total Posts : 4
  • Scores: 0
  • Reward points : 0
  • Joined: 3/30/2010
  • Status: offline
Move-Item - Beginner Tuesday, March 30, 2010 9:46 AM (permalink)
0
Hi,
 
I am playing with the move-item command to move files between two servers. I can move the contents of Server1\Folder1 to Server2\Folder1\Work but I want to be able to do this for multiple source folders. ie Server1\Folder1 to Server2\Folder1\Work, Server1\Folder2 to Server2\Folder2\Work etc
 
I have looked at different ways of doing this but can't quite put my finger on it.
 
Any pointers in the right direction would be appreciated,
 
Si
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:Move-Item - Beginner Tuesday, March 30, 2010 10:08 AM (permalink)
    0
    Well, it depends. In the example that you have given, the structure is the same for both cases:

    Server1\Folder1 -> Server2\Folder1\Work
    Server1\Folder2 -> Server2\Folder2\Work

    So as you can see, the only thing that really changes is the name of one folder, so something like (untested):

    ('Folder1', 'Folder2') | %{Move-Item "\\Server1\$_\*" "\\Server2\$_\Work"}

    Should do the trick. If however you have a more complicated need and the cases vary in a way that can't be handled by simple substitution, then you could use a dictionary. Let's say our situation was more like this:

    \\Server1\Folder -> \\Server2\path\Folder
    \\Server3\OtherFolder\SubFolder -> \\Server4\Folder

    In this case there is no easy way to use a substitution rule, so (again untested):

    $moveInfo = @{"\\Server1\Folder\*" = "\\Server2\path\Folder"; "\\Server3\OtherFolder\SubFolder\*" = "\\Server4\Folder"}; foreach($source in $moveInfo.Keys){Move-Item $source $moveInfo[$source]}

    That is onelining it at the command line. In a script it would be easier to understand:

    $moveInfo = @{
                "\\Server1\Folder\*" = "\\Server2\path\Folder";
                "\\Server3\OtherFolder\SubFolder\*" = "\\Server4\Folder"
    }
    foreach ($source in $moveInfo.Keys){
        Move-Item $source $moveInfo[$source]
    }

    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      sransell

      • Total Posts : 4
      • Scores: 0
      • Reward points : 0
      • Joined: 3/30/2010
      • Status: offline
      Re:Move-Item - Beginner Tuesday, March 30, 2010 6:20 PM (permalink)
      0
      Thank you, this was very helpful.
       
      ('Folder1', 'Folder2') | %{Move-Item "\\Server1\$_\*" "\\Server2\$_\Work"} will do the trick as it is simply moving user data from one server to another into an appended Work folder.
       
      Is there a way of scanning the folders on server one, capturing the folder name, then substituing the new path and moving the data, then going on to the next folder?
       
      My current structure is;
       
      Users\Bob
               \Fred
               \John
       
      and I need to move the data to:
       
      Users\Bob\Work
               \Fred\Work
               \John\Work
       
      Thanks again,
       
      Si
       
      #3
        ebgreen

        • Total Posts : 8227
        • Scores: 98
        • Reward points : 0
        • Joined: 7/12/2005
        • Status: offline
        Re:Move-Item - Beginner Wednesday, March 31, 2010 1:08 AM (permalink)
        0
        Sure. To get all the subfolders, just filter for containers only:

        gci C:\Path\To\Users\* | ?{$_.PSIsContainer} | Select-Object name | %{Move-Item "\\Server1\$_\*" "Server2\$_\Work"}

        "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
        Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
        http://www.visualbasicscript.com/m_47117/tm.htm
         
        #4
          sransell

          • Total Posts : 4
          • Scores: 0
          • Reward points : 0
          • Joined: 3/30/2010
          • Status: offline
          Re:Move-Item - Beginner Wednesday, March 31, 2010 5:36 AM (permalink)
          0
          Hi,

          I really appreciate your help. I am sure we are getting closer.

          I tested this by creating these folders

          E:\Server1\A
                           \B
                           \C

          and

          E:\Server2\A\Work
                           \B\Work
                           \C\Work

          then running:

          gci E:\Server1\* | ?{$_.PSIsContainer} | Select-Object name | %{Move-Item "E:\Server1\$_\*" "E:\Server2\$_\Work"}

          It fails with the following error repeated for each folder

          Move-Item : Cannot find path 'E:\Server1\@{Name=a}' because it does not exist.
          At line:1 char:74
          + gci E:\Server1\* | ?{$_.PSIsContainer} | Select-Object name | %{Move-Item <<<<  "E:\Server1\$_\*" "E:\Server2\$_\Work
          "}
              + CategoryInfo          : ObjectNotFound: (E:\Server1\@{Name=a}:String) [Move-Item], ItemNotFoundException
              + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand

          I am sure it is me but I can't see where I am going wrong.

          Simon
           
          #5
            ebgreen

            • Total Posts : 8227
            • Scores: 98
            • Reward points : 0
            • Joined: 7/12/2005
            • Status: offline
            Re:Move-Item - Beginner Wednesday, March 31, 2010 5:47 AM (permalink)
            0
            Yeah, PS works differently when I run the code in my head as opposed to actually running it on a machine. :)

            gci E:\Server1\* | ?{$_.PSIsContainer} | Select-Object name | %{Move-Item "E:\Server1\$($_.Name)\*" "E:\Server2\$($_.Name)\Work"}
            "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
            Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
            http://www.visualbasicscript.com/m_47117/tm.htm
             
            #6
              sransell

              • Total Posts : 4
              • Scores: 0
              • Reward points : 0
              • Joined: 3/30/2010
              • Status: offline
              Re:Move-Item - Beginner Wednesday, March 31, 2010 6:19 AM (permalink)
              0
              Fantastic, that worked a treat in testing. I would never have got there in a month of Sundays.

              Thank you for your help,

              Simon
               
              #7

                Online Bookmarks Sharing: Share/Bookmark

                Jump to:

                Current active users

                There are 0 members and 1 guests.

                Icon Legend and Permission

                • New Messages
                • No New Messages
                • Hot Topic w/ New Messages
                • Hot Topic w/o New Messages
                • Locked w/ New Messages
                • Locked w/o New Messages
                • Read Message
                • Post New Thread
                • Reply to message
                • Post New Poll
                • Submit Vote
                • Post reward post
                • Delete my own posts
                • Delete my own threads
                • Rate post

                2000-2012 ASPPlayground.NET Forum Version 3.9