Question

I have an array of files that need to be moved to a backup locations. I am collecting the array of desired items using a get-childitem command. I am looking to use robocopy to move stuff once the list of collected items is ready.

$paths=@()

$srcitems = get-childitem $paths 

robocopy $srcitems $dest /move

Does this work?
If not what is the best way to pipe to each individual item to robocopy?

Thanks
Steeluser

Was it helpful?

Solution

           Usage :: ROBOCOPY source destination [file [file]...] [options]

         source :: Source Directory (drive:\path or \\server\share\path).
    destination :: Destination Dir  (drive:\path or \\server\share\path).
           file :: File(s) to copy  (names/wildcards: default is "*.*").

Robocopy is expecting a source directory, a destination directory, and a file spec as arguments. It's difficult to give a definitive answer without knowing what your "list of collected items" looks like. If it's source directories, then you can foreach that list through a an ivocation of robocopy, and hardcode a wildcard spec for the file names. If you've got a list of files, you'll need to split those off into directory/file (I'd use split-path), and do an invocation of robocopy for each source directory, specifying the list of files in that directory.

OTHER TIPS

Similar scenario, posting in case it helps:

I needed to copy (move) some folders all beginning with "Friday" and items within from a source to a destination, and came up with this that seems to be working:

Get-ChildItem T:\ParentFolder -Filter "Friday*" -Name | ForEach-Object { robocopy "T:\ParentFolder\$_" "E:\$_" /z /s /MOVE }
  • The "Get-ChildItem" portion lists the folder names (-Name) starting with "Friday" (-Filter "Friday*").
  • This gets piped to the ForEach-Object where robocopy will execute for every instance found.
  • The robocopy /MOVE argument obviously moves the folders/files.

I'm fairly new to Powershell; not sure if there is a better way. The script is still running, but so far so good.

@walid2mi uses Move-Item which I'm sure works; I just like robocopy b/c it has a restartable mode (/Z).

Syntax that worked for me:

$srcPath = "C:\somefolder"
$dstPath = "C:\someOtherFolder"
$srcitems = get-childitem $srcPath #whatever condition

$srcitems | Select Name | ForEach-Object {robocopy $srcPath $dstPath $_.name}

(which is obvious according to the robocopy documentation)

robocopy

Get-ChildItem $paths | Move-Item -Destination $dest  -WhatIf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top