A N

May 7, 2015

Powershell – [IO.Directory]::EnumerateFiles VS Get-Childitem VS [IO.Directory]::GetFiles

Filed under: Tips — donrsh @ 8:47 am

The goal is to see how long it takes to change file extension of large amount of files using different methods in Powershell.

1) Create 500 000 files
2) Change file extension using [IO.Directory]::EnumerateFiles / Get-Childitem / [IO.Directory]::GetFiles and compare how long it takes.

Create files

1..500000 | % {New-Item -Path C:\utdelat\Archive2 -name "$_.txt" -Value (get-date).ToString() -ItemType file -Force}

Lets change file extension using powershell

############## EnumerateFiles
$newExtensionLOG = "logx"
$EenumerateFiles = Measure-Command -Expression {       
        foreach ($itemEn in [IO.Directory]::EnumerateFiles("C:\utdelat\Archive2"))
        {
           Rename-Item $itemEn -NewName ([System.IO.Path]::ChangeExtension($itemEN,$newExtensionLog)) 
             
        }
} 

 
 $EenumerateFiles
############## Get-ChildItem

$GetChilditem = Measure-Command -Expression {
        $newExtensionXLS = "xls"
        foreach ($itemGet in Get-ChildItem -Path "C:\utdelat\Archive2")
        {
            
           Rename-Item $itemget.FullName -NewName ([System.IO.Path]::ChangeExtension($itemGet,$newExtensionXLS)) 
             
        }
}

$GetChilditem

############## GetFiles

$GetFiles =  Measure-Command -Expression {
        $newExtensionZIP = "zip"
        foreach ($itemZIP in [IO.Directory]::GetFiles("C:\utdelat\Archive2"))
        {
           
            Rename-Item $itemZip -NewName ([System.IO.Path]::ChangeExtension($itemZip,$newExtensionZIP) )
        }
}

$GetFiles

The result is:

[IO.Directory]::EnumerateFiles

Days              : 0
Hours             : 0
Minutes           : 12
Seconds           : 37
Milliseconds      : 312
Ticks             : 7573125757
TotalDays         : 0,00876519184837963
TotalHours        : 0,210364604361111
TotalMinutes      : 12,6218762616667
TotalSeconds      : 757,3125757
TotalMilliseconds : 757312,5757

Get-Childitem

Days              : 0
Hours             : 0
Minutes           : 27
Seconds           : 3
Milliseconds      : 635
Ticks             : 16236359088
TotalDays         : 0,0187920822777778
TotalHours        : 0,451009974666667
TotalMinutes      : 27,06059848
TotalSeconds      : 1623,6359088
TotalMilliseconds : 1623635,9088
[IO.Directory]::GetFiles

Days              : 0
Hours             : 0
Minutes           : 28
Seconds           : 33
Milliseconds      : 451
Ticks             : 17134517876
TotalDays         : 0,019831617912037
TotalHours        : 0,475958829888889
TotalMinutes      : 28,5575297933333
TotalSeconds      : 1713,4517876
TotalMilliseconds : 1713451,7876

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: