Skip to content

Work with files using PowerShell

Quick reminder to myself, since this can easily be looked up online, the following code snippets are useful for mass renaming files in a directory, for instance when renaming a series of TV Show episodes into a format that TV Rename can recognize and subsequently rename with correct episode title information for use in XBMC.

By the way, these code snippets were spread out to multiple lines for ease of reading, but PowerShell doesn’t care about them and when I write them into the console myself, they are all in a single line.

   1: $count=1; 

   2: foreach ($f in gci) 

   3: { 

   4:     $s = "S01E{0:0#}" -f $count; 

   5:     $count++; 

   6:     $s2 = "{0}{1}" -f $s, $f.extension; 

   7:     ren $f -newname $s2 

   8: }

While I am pretty sure that the above code can be made much more inline than what I have made it, or more elegant, for a beginner with PowerShell (who still has a book on PowerShell to read) like myself, this is sufficient, again, not sure how efficient it is, but I know it can be made better. What this particular snippet does is, keep a counter, and rename each file it finds with a number that sequentially increments by 1, yielding: S01E01.avi, S01E02.avi, S01E03.avi. You will notice that the file is renamed while retaining the extension of the original file, so if the files are .mpg or .jpg, the extension will not change with the new filename.

 

This next snippet, is useful if you have a few files that you can want to convert to AVI (or any other format that FFmpeg supports converting to) all the files in a directory, this was useful when I had a few WMV files that I needed to convert to AVI so that I could then use HandBrake to convert to MP4s that I could view on my iPhone.

(This snippet requires the .basename property to be registered/added to the FileInfo object: http://blogs.msdn.com/powershell/archive/2006/06/24/645981.aspx has details)

   1: foreach ($f in gci) 

   2: { 

   3:     if ($f.extension -eq ".wmv") 

   4:     {

   5:          .\ffmpeg -i $f.name -b 900 -vcodec libx264 -f avi -threads 6 "$($f.basename).avi" 

   6:     }

   7: }

In essence, this snippet queries all the files in the directory, if their extension is .WMV the files are then passed through an ffmpeg command which will convert the files to an H.264 encoded AVI file, keeping the audio stream the same. It is stuff like this that makes scripting (and PowerShell in particular) so interesting and useful, it saves you the time and repetition of certain tasks and allows you to set them overnight and wake up to them having completed in the morning.

 

This concludes my first post, I will be posting some more in the future, mainly for my own self reference, but I am sure that if I need to remember something, someone else might be interested in these little bits of information that I am storing away, at the very least I know where I am filing them away so that I can get rid of the sticky notes that they occupy on my desktop!

2 Comments

  1. mig wrote:

    After posting this, I was looking at the comments on the MSDN blog post linked, and it seems Scott Hanselman wrote his own little script (not too different from the one on this post) for the FFmpeg tip, always more than one way to accomplish something: http://www.hanselman.com/…/…ideosUsingPowerShell.aspx

    Sunday, September 6, 2009 at 5:07 am | Permalink
  2. Adelaide wrote:

    Thanks for your job. I love your blog.

    Thursday, July 1, 2010 at 3:22 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*