none
RoboCopy Powershell RRS feed

  • Question

  • I hope you can help....   

    I’m in a situation where I have certain files I need to pull out of a File Share.   I am able to export a list of these files I need to move into a csv file.

    Example:   

    e:\Folder1\Common\test1.txt

    e:\Folder1\Common\App\test2.txt

    e:\Folder1\Common\Mobile\test3.txt

    I am not sure if Robocopy will help, or if PowerShell  (move-item) is my solution.

    But, I need to move the Example shown above to a different location while maintaining the Directory Tree

    Example:

    e:\Export\Folder1\Common\test1.txt

    e:\Export\Folder1\Common\App\test2.txt

    e:\Export\Folder1\Common\Mobile\test3.txt

    I think the key here is;   I need to move the files,  I exported to a CSV file,  and move them to a different location,  while maintaining the original directory tree.

    Thank you


    Friday, January 18, 2013 1:57 AM

Answers

  • Try this...

    $files=get-content 'files.csv'
    $files | ForEach{
        $newpath=$_ -replace 'e:\\folder1\\common\\','e:\export\folder1\common\'
        $testfolder=Split-Path $newpath -Parent
        If(-not(Test-Path $testfolder)){md $testfolder}
        copy-item $_ $newpath
    }

    If the files are all in the same path... "e:\folder1\common" that will work fine.

    Because -replace uses regular expressions you have to escape the backslash with another backslash - \\


    Inspired by Heineken.

    • Marked as answer by Semperfi4000 Monday, January 21, 2013 12:58 PM
    Friday, January 18, 2013 4:52 AM

All replies

  • If I understand correctly, the source folder in question also contains files taht you do not want to move, correct?

    If so, it will be difficult with robocopy itself, unless you can select the files with a filename filter, like "move all *.jpeg files". in the case above, you could use three robocopy commands, one for test1.txt and etc. But that would fail if there was a test1.txt file in another folder that you did not want to move.

    One thing that might work for robocopy is to set (clear?) the archive bit on all files listed in the .csv file, and then get robocopy (or even xcopy) to move only those with the bit set (or cleared, I forget which).

    But in the general case I think you may need to have a powershell examine the source path of each file to be copied, generate the equivalent path in the destination directory, and do a move-item.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Friday, January 18, 2013 4:22 AM
  • Try this...

    $files=get-content 'files.csv'
    $files | ForEach{
        $newpath=$_ -replace 'e:\\folder1\\common\\','e:\export\folder1\common\'
        $testfolder=Split-Path $newpath -Parent
        If(-not(Test-Path $testfolder)){md $testfolder}
        copy-item $_ $newpath
    }

    If the files are all in the same path... "e:\folder1\common" that will work fine.

    Because -replace uses regular expressions you have to escape the backslash with another backslash - \\


    Inspired by Heineken.

    • Marked as answer by Semperfi4000 Monday, January 21, 2013 12:58 PM
    Friday, January 18, 2013 4:52 AM
  • Hi,

    Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.

    If you have any feedback on our support, please click here .


    Cataleya Li
    TechNet Community Support

    Monday, January 21, 2013 7:00 AM
    Moderator
  • this worked out great,  thank you
    Monday, January 21, 2013 12:58 PM