Real World Renaming Folder Using Powershell

15 Sep

Renaming folder in Powershell is easy enough, but many times especially in production environment (yes, I am talking to YOU cowboy!), you want to ensure that you changes is done and pre- and post-checks are done between you scripts.

Here we were required to retire old folders. Its simple enough if there is only 10, but being a clean up job, we found hundreds and needed to rename them to _RETIRED_yyy-mm-dd_filename. Addition checks for folder access and if the change was done sucessfully was added. Input is a CSV file as below:


Results is output to a log file called retire-fldr.log


$outlog = ".\retire-fldr.log"
$d = Get-Date $ds = "$($d.year)-$($d.month)-$($"
$f = import-csv $file
foreach ($fldr in $f)
 $new = "_RETIRED_$($ds)_$($fldr.folder)"
 Write-Output "Working on... $($fldr.path)`\$($fldr.folder)"
 if (test-path "$($fldr.path)`\$($fldr.folder)")
  Rename-Item -path "$($fldr.path)`\$($fldr.folder)" -NewName $new
  if (test-path "$($fldr.path)`\$new")
    {"$($fldr.path)`\$new `t DONE" |out-file $outlog -append}
    {"$($fldr.path)`\$new `t ERROR" |out-file $outlog -append}

else {"$($fldr.path)`\$($fldr.folder) `t NO ACCESS" |out-file $outlog -append}
Leave a comment

Posted by on September 15, 2011 in powershell, Scripts



Leave a Reply

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

You are commenting using your 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

%d bloggers like this: