Rewrite a Powershell Add-TimeStamp to File Function (Simplified)


I am practicing Powershell V4 script recently. I found out the powershell.com is a good resource.
I came across a custom function from powershell.com: Add-TimeStamp.
It will rename a file with the last modified time added to the original file name.
It uses multiple steps to retrieve propertities of the file and manipulate it to the final format.


# space
# space

dir "C:\temp\test\processed\" | ren -NewName {$_.LastWriteTime.ToString("yyy-MM-ddTHHmmss")+ $_.Name }




 

Add-TimeStamp function:



function Add-TimeStamp ($Path) { 
    $Vars = Get-ItemProperty $Path | select Directory,Name,LastWriteTime 
    $Directory = $([string]($vars | select -ExpandProperty Directory)+'\') 
    $BaseName = ($vars | select -ExpandProperty Name) 
    $Time = ($vars | select -ExpandProperty LastWriteTime) 
    $TimeStamp = ($Time | Get-Date -Format yyy-MM-ddTHHmmss_) 
    $NewName = ($TimeStamp+$BaseName) 
    $Path1 = $([string]($Directory+$BaseName)) 
    Rename-Item -Path $Path1 -NewName $NewName 
} 


 

The fource for this function: http://powershell.com/cs/media/p/23848.aspx

Advertisements


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