Wednesday, June 24, 2015

Add File Extensons via Powershell

We are using an extraction tool to export data from one of our business applications in preparation to switching to our ERP solution. Unfortunately this tool doesn't add the file extensions to the exported files!

So I needed to create a powershell script to do the job for us.

As with most things there is no point reinventing the wheel.  I did a google search and found this useful blog post.

http://blogs.msdn.com/b/dan_fay/archive/2012/09/17/rename-file-extension-with-powershell.aspx

The script was pretty much there, but I needed to tweak it slightly.  I added a line which asks the user which file extension they want to use.  I also changed it so that it would only change files which currently have no file extensions configured.
What users are presented with

The result is as follows
$extractedfiles = Get-ChildItem | Where-Object {$_.Extension -eq ""}
$fileext = read-host "what file extension?"
ForEach ($file in $extractedfiles) {
$filenew = $file.Name + ".$fileext" Rename-Item $file $filenew
}
All files without extensions are now .jpg

0 comments:

Post a Comment