Sunday, May 22, 2011

 

powershell rename file group to sequence

I have one of those camera's that can't keep consistent file names. So I was manually incrementing file names, not fun. So I tired some powershell. Beginning with some searching the closest I came to what I wanted was on this blog http://geekswithblogs.net/tmurphy/archive/2007/07/18/Renaming-Groups-of-Files-With-PowerShell.aspx

This didn't seem to work for my version of powershell and the max file name needed the full file name instead of just a "StartsWith". So I made some modifications:
    $maxname = @(get-childitem -path $picpath | where {($_.Name.StartsWith($maxname))})[0].Name  #@() turns it to an array even if it's just a single file
$prefix = $Args[3]
Write-Host "renaming $minname to $maxname with $prefix#$($(@(get-childitem -path $picpath | where {($_.Name.StartsWith($maxname))})[0].Extension))"
get-childitem -path $picpath | where -filterscript {($_.Name -ge $minname) -and ($_.Name -le $maxname)} | foreach -process {ren -path $_.FullName -NewName "$prefix$($script:idx++;$script:idx)$($_.Extension)"}
That $($script:idx++;$script:idx) means run the increment then return that incremented value for display. Now the script worked great, except every time I ran the script I wasn't going to remember what all these parameters meant and didn't want to relearn. So I added a new case to the to the top to handle 0 parameters. The idea was I'd put this script in the root folder of my pictures. I'd prompt the user for the relative path, the folder name that needed the renaming. Then everything else would be already pre-set in the script.

I needed the preset to rename all files that started with 00 to a 5 digit number. This number would start just over the number of the last time I did a rename. This "just over" number I have to hard code into the script file.
if($Args.Length -lt 1)
{
#use shawn's naming for movies from the ZS7 camera
$idx = 127 #this is exactly the last counter value, not +1. nameing starts with +1
$prefix = if ($idx -le 99) {"000"} elseif ($idx -le 999) {"00"} else {"0"}
$folder = Read-Host "Movies will start at $($idx + 1). Enter folder name where movies are stored"
$picpath = ".\$folder"
$minname = "00"
$allfiles = @(get-childitem -path $picpath | where {($_.Name.StartsWith($minname)) -and ($_.Name -le "$prefix$($idx - 10)")}) #don't select the last 10 renamed files. the -10 isn't safe from idx 100 to 109 due to $prefix not matching on the -10 value.
$maxname = $allfiles[$($allfiles.Length - 1)].Name #get the last alphabetic file that matches $minname
get-childitem -path $picpath | where -filterscript {($_.Name -ge $startwith) -and ($_.Name -le $maxname)} | foreach -process {ren -path $_.FullName -NewName "$prefix$($script:idx++;$script:idx)$($_.Extension)"}
}
That did the trick for me. Naming files that were 00000.ext, 00001.ext as 00128.ext, 00129.ext. If you want to do this with files that start differently you can use the Windows 7 rename functionality to get them to start similarly. I would like the script to parse itself and update the $idx itself, but that's a small benefit for jumping into some deep water.

Labels: ,


Wednesday, May 04, 2011

 

Add Visual Studio 2010 Command Prompt to IDE

Short steps to help me recall after a future system format.

Find the command prompt to copy arguments to the clipboard:
1. Click the windows button and search for "Visual Studio Command Prompt (2010)".
2. Right click it and select properties.
3. Copy the "target" field to the clipboard, Ctrl-c. it should look similiar to the following:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86


Add it to the visual studio IDE
1. On the menu bar open Tools / External Tools...
2. Click Add
3. Give it a name, such as "Visual Studio 2010 Command Prompt"
4. For Command, enter
cmd.exe

5. For Arguments, paste from your clipboard, Ctrl-v:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86


The Visual Studio 2010 Command Prompt will now show up on the Tools Menu.

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]