RSS

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:

path,folder
c:\temp,test11
c:\temp,test22

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

param([string]$file)

$outlog = ".\retire-fldr.log"
$d = Get-Date $ds = "$($d.year)-$($d.month)-$($d.day)"
$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}
  else
    {"$($fldr.path)`\$new `t ERROR" |out-file $outlog -append}
 }

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

Posted by on September 15, 2011 in powershell, Scripts

 

Tags:

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

 
%d bloggers like this: