Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 246
m_power Avatar asked Sep 18 '25 09:09

m_power


2 Answers

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.

like image 110
mjolinor Avatar answered Sep 21 '25 00:09

mjolinor


$array = $files -split '\r?\n'

I'm assuming $files is text separated by line breaks. This will split the string by line breaks and assign to $array.

like image 32
briantist Avatar answered Sep 21 '25 00:09

briantist