Skip navigation

Tag Archives: file management

Redid the File Renamer script for you based on what you asked for. This should tag common video files as v and pictures (well everything that isn’t excluded) as p.  I also added a file browser to the script as well as a general graphical interface to kind of make things simpler.  If you don’t want it in there I can easily strip it out and put it back to command line.

<#---------------------------------------------------------------------
Script: Renamer.ps1
Author: Daniel Belcher
Modified: 11/5/11
---------------------------------------------------------------------#>
FUNCTION Folder($MSG, $PATH) { 
    $SHELL = New-Object -comObject Shell.Application  
	$FOLDER = $SHELL.BrowseForFolder(0, $MSG, 0, $PATH)
	    if ($FOLDER -ne $NULL) 
			{$FOLDER.self.Path}
} 
	FUNCTION Message($MSG, $TITLE) {
		$SHELL = New-Object -ComObject Wscript.Shell
		$SHELL.Popup($MSG,0,$TITLE,1)
		}
<#---------------------------------------------------------------------
Select Folder with contnets to modify via Shell.Application namespace
and verify that the choice is correct with Wscript popup method.
---------------------------------------------------------------------#>
   $FOLDER = Folder -MSG "Select your folder..." -Path .\
if($FOLDER -eq $NULL) {
Message -MSG "Must select a folder to continue." -Title "Error" 
		break}
$VERIFY = (Message `
"You Selected:
$Folder
Its contents will be renamed, are you sure?" -Title "Verify")
	if($VERIFY -eq "2") {break}
<#---------------------------------------------------------------------
Grab directory contents and process
---------------------------------------------------------------------#>
	$LIST = Get-ChildItem "$FOLDER\*" -Exclude `
	*ps1,*exe,*mp3,*dll,*ini,*cfg,*ocx,*doc?,*xls?,*txt|Sort-Object lastwritetime
			$X = 0
	ForEach($OBJECT in $LIST)
		{$EXTENSION = $OBJECT.ToString().Split("\") | Select -Last 1; 
		$EXTENSION = $EXTENSION.Split(".") | Select-Object -Last 1
	if($EXTENSION.Contains("ps1") -eq $TRUE){break}
$TYPE = "p"
		switch($EXTENSION){
			mov{$TYPE = "v"}
			avi{$TYPE = "v"}
			wmv{$TYPE = "v"}
			mpg{$TYPE = "v"}
			mpeg{$TYPE = "v"}
			}
			$FILEINFO = New-Object System.IO.DirectoryInfo($OBJECT)
				$NAME = $FILEINFO.LastWriteTime.GetDateTimeFormats() |`
					Select-Object -Index 99
				$NAME = "$($NAME) ($($X)) $($TYPE).$($EXTENSION)"
						Write-Output $NAME
			Rename-Item -Path "$OBJECT" $NAME
			$X = $X+1
			}
<#---------------------------------------------------------------------
END
---------------------------------------------------------------------#>

#.

Feel free to modify this anyway you like, add more extensions, exclude more etc. This was kind of a lazy hack to get you what you told me, and I had a few minutes to kill since I couldn’t sleep tonight.

So as some of you may or may not know.  My wife is conducting a 365 picture project this year.  What’s a 365 picture project?  Essentially take a pic of you, or something in your life for each day of the year and compile it all together at the end of the year to sort of journal everything.  It’s, as you can imagine, a lot of pictures being taken a day.  I would say on average she has 5 to 10 pictures taken a day.  Realistically speaking, some days it’s about 2, others it’s near 30 or more. 

 

Ok, great, why are you telling me this? 

 

Well, I’m glad you asked.  She would spend a lot of time sorting, renaming, and organizing them.  I’ve been telling her a long time “just automate it” which is something I tell her about almost everything she does that’s repetitive.  Well finally after a long night or sorting through some 100+ photos she called me on it, and had me write her a script to rename all the files for her.  I also had a chance to get to show her a bit of that “computer stuff” I do everyday at work. Also, it’s been a while since I’ve done a scripting post, so I’m using this. 🙂

I figured I would go with powershell since I would have (easier) access to the .NET System.IO.DirectoryInfo class.  One of the things my wife loves is chronological accuracy, so pulling a timestamp from the image as part of its name seemed like a good idea.  She already had a routine where she copied them from her camera to an appropriate folder so specific file information was the only real concern here so this was going to be a very simple script.  Below is the code:

 


PARAM([string]$FOLDER)
$ErrorActionPreference = "silentlycontinue"
if($FOLDER -eq "")
    {$FOLDER = Read-Host "Path to picture folder?"}
    $LIST = Get-ChildItem "$FOLDER*" -Exclude *ps1 `
    -Include *jpg,*tiff,*jpeg,*gif,*bmp,*txt
            $X = 0
    ForEach($OBJECT in $LIST)
        {$EXTENSION = $OBJECT.ToString().Split("") | Select -Last 1
            $EXTENSION = $EXTENSION.Split(".") | Select-Object -Last 1
              $FILEINFO = New-Object System.IO.DirectoryInfo($OBJECT)
                $NAME = $FILEINFO.LastWriteTime.GetDateTimeFormats() `
                | Select-Object -Index 99
                    $NAME = "$($NAME) ($($X)).$($EXTENSION)"
                        Write-Output $NAME
            Rename-Item -Path "$OBJECT" $NAME
            $X = $X+1
            }       


So what’s happening?

First we run this script, either by launching it directly, or by launching it on a command line followed by the folder where our pictures reside. The folder path is our only variable here, if one isn’t entered at the start of execution, it will prompt the user for one.

Now the script will build an object list of everything inside the folder taking special care to exclude any potential powershell scripts in the directory to the $LIST variable.  I also built it to specifically include image file types (I didn’t want to rename some random non-picture files she might be storing in the directory (well, and txt for my testing purposes)). 

Now we define an integer value to $X so we can enumerate it for a counter during our ForEach loop, which we begin next.

For each file in $LIST we:

  1. Grab the extension for the file to variable $EXTENSION
  2. Retrieve the last write time of the file and store it as $NAME
  3. Set $NAME to $NAME + $X + $EXTENSION
  4. Write the final $NAME to console
  5. Rename the item to $NAME
  6. Enumerate $X by 1
  7. Loop

The outcome?

 

I showed her the way it renamed a series of text files I had placed in a test folder on my laptop.  At first, I received a rather dismissive “Oh, that’s good babe” however after I sat down at her desk and showed her the bad boy in action renaming another folder of 100 or so images in seconds, the sly grin found it’s way across her face.  The joy of automation.  The kind you can only get when you see something so small perform such a mind numbing laborious task for you.

She’s not quite ready to learn scripting, but at least now her eyes are open to other possibilities for automated solutions in her everyday computing.  And as a stay at home mother of 2, I’m more than willing to help her streamline all her recreational and productive time at the keyboard.  She is after all, my number 1 customer ;).