Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found out the <code>GetChildItem</code> is the slowest option when working with many items in a directory.</p> <p>Look at the results:</p> <pre><code>Measure-Command { Get-ChildItem C:\Windows -rec | Out-Null } TotalSeconds : 77,3730275 Measure-Command { listdir C:\Windows | Out-Null } TotalSeconds : 20,4077132 measure-command { cmd /c dir c:\windows /s /b | out-null } TotalSeconds : 13,8357157 </code></pre> <p>(with listdir function defined like this:</p> <pre><code>function listdir($dir) { $dir [system.io.directory]::GetFiles($dir) foreach ($d in [system.io.directory]::GetDirectories($dir)) { listdir $d } } </code></pre> <p>)</p> <p>With this in mind, what I would do: I would stay in PowerShell but use more lowlevel approach with .NET methods:</p> <pre><code>function DoForFirst($directory, $max, $action) { function go($dir, $options) { foreach ($f in [system.io.Directory]::EnumerateFiles($dir)) { if ($options.Remaining -le 0) { return } &amp; $action $f $options.Remaining-- } foreach ($d in [system.io.directory]::EnumerateDirectories($dir)) { if ($options.Remaining -le 0) { return } go $d $options } } go $directory (New-Object PsObject -Property @{Remaining=$max }) } doForFirst c:\windows 100 {write-host File: $args } # I use PsObject to avoid global variables and ref parameters. </code></pre> <p><em>To use the code you have to switch to .NET 4.0 runtime -- enumerating methods are new in .NET 4.0.</em> </p> <p>You can specify any scriptblock as <code>-action</code> parameter, so in your case it would be something like <code>{Move-item -literalPath $args -dest c:\dir }</code>.</p> <p>Just try to list first 1000 items, I hope it will finish very quickly:</p> <pre><code>doForFirst c:\yourdirectory 1000 {write-host '.' -nonew } </code></pre> <p>And of course you can process all items at once, just use</p> <pre><code>doForFirst c:\yourdirectory ([long]::MaxValue) {move-item ... } </code></pre> <p>and each item should be processed immediately after it is returned. So the whole list is not read at once and then processed, but it is processed during reading.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload