Off topic: Powershell to rename pictures

I’m trying hard to convert myself from VBScript to Powershell… so in the need of a powerfull filerenamer I wrote one in Powershell.

You need to download the Powershell Pack from http://code.msdn.microsoft.com/PowerShellPack

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#                                                                                           Rikard Ronnkvist / snowland.se
#  Will rename JPG and AVI files, uses EXIF-tag on JPG-images and filedate on AVI-files.
#
#  Files will be named:
#    \some path\YYYYMM\YYYYMMDD_HHMMSS_00.JPG
#               ^^^^^^ - Optional, if you have createSubdir set to True
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$basePath = "F:\Pictures\Import dir"
$createSubdir = $True
$testMode = $False
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Import-Module PowerShellPack
 
# Add \* to use when searching
$searchPath = $basePath + "\*"
 
# Search for files
Write-Host "           Searching: " -ForegroundColor DarkGray -NoNewline
Write-Host $basePath -ForegroundColor Yellow
 
$allFiles  = Get-ChildItem -Path $searchPath -Include *.AVI,*.JPG -Exclude folder.jpg
 
Write-Host "               Found: " -ForegroundColor DarkGray -NoNewline
Write-Host $allFiles.Count -ForegroundColor Yellow -NoNewline
Write-Host " files" -ForegroundColor DarkGray
 
$fNum = 0
# Loop thru all files
foreach ($file in $allFiles )
{
        $fNum++
        # If it is an jpg use the exif-data, otherwise use date on file
        if ($file.Extension -eq ".JPG") {
                $imgInfo = $file | Get-Image | Get-ImageProperty
                $fileDate = $imgInfo.dt
        } else {
                $fileDate = $file.LastWriteTime
        }
 
        if ($createSubdir -eq $True) {
                # Set new filepath
                $fileDir = $basePath + "\" + $fileDate.ToString("yyyyMM")
 
                # Check directory
                if (!(Test-Path($fileDir))) {
                        # Create a new subdirectory
                        if ($testMode -ne $True) {
                                $newDir = New-Item -Type directory -Path $fileDir
                                Write-Host "            Creating: " -ForegroundColor DarkGray -NoNewline
                                Write-Host $fileDir -ForegroundColor Red
                        }
                }
        } else {
                # Use current directory
                $fileDir = $basePath
        }
 
        # Set new name to current to get "False" on first while
        $newPath = $file.Fullname
 
        $i = 0
        while (Test-Path $newPath) {
                # Set new filename
                $newPath = $fileDir + "\" + $fileDate.ToString("yyyyMMdd_HHmmss") + "_" + $i.ToString("00") + $file.Extension
                $i++
        }
 
        # Write som info
        Write-Host $fNum.ToString().PadLeft(4) -ForegroundColor DarkYellow -NoNewline
        Write-Host " / " -ForegroundColor DarkGray -NoNewline
        Write-Host $allFiles.Count.ToString().PadRight(4) -ForegroundColor Yellow -NoNewline
        Write-Host "   Moving: " -ForegroundColor DarkGray -NoNewline
        Write-Host $file.Name -ForegroundColor Cyan -NoNewline
        Write-Host " -> " -ForegroundColor DarkGray -NoNewline
        Write-Host $newPath -ForegroundColor Green
 
        # Move and rename the file
        if ($testMode -ne $True) {
                Move-Item $file.Fullname $newPath
        }
}

Ending up with something like this:

Oh, and you can add -Recurse on the Get-ChildItem row…