Search code examples
powershellrobocopypowershell-cmdlet

Get file list with Robocopy


I'm trying to see if a solution with Robocopy could be faster than using the Get-ChildItem for getting a list of files that are inside a given folder (and subfolders...).

In my code I'm using the Get-ChildItem cmdlet to get a list of all files inside a specific folder in order to loop on each of these file:

$files = Get-ChildItem "C:\aaa" -Recurse | where {! $_.PIsContainer} # ! because I don't want to list folders
foreach ($file in $files){
...
}

Now, I have the robocopy command to get a list of all files, however, the output of robocopy is a string.

[string]$result = robocopy "C:\aaa" NULL /l /s /ndl /xx /nc /ns /njh /njs /fp

So, how can I use the output from the robocopy command to loop on each file (similar to what was done with Get-ChildItem?


Solution

  • If you're just looking for a faster way to get that list of files, the legacy dir command will do that:

    $files = cmd /c dir c:\aaa /b /s /a-d
    foreach ($file in $files){
    ...
    }
    

    Edit: Some comparative performance tests-

    (measure-command {gci -r |? {-not $_.psiscontainer } }).TotalMilliseconds
    (measure-command {gci -r -file}).TotalMilliseconds
    (measure-command {(robocopy . NULL /l /s /ndl /xx /nc /ns /njh /njs /fp) }).TotalMilliseconds
    (measure-command {cmd /c dir /b /s /a-d }).TotalMilliseconds
    
    627.5434
    417.8881
    299.9069
    86.9364
    

    The tested directory had 6812 files in 420 sub-directories.