Sunday, 30 November 2014

Supporting my bulk rename with PowerShell (2)

So the script got to be more sophisticated and can now create folders if they don’t exist. What pleases me about PS is that it has a full range of file handling functions built right into it and doesn’t require extensions to do this kind of thing I am doing with it.

The bulk rename has taken all day because it is necessarily slow and tedious when dealing with 65,000 images. In the process I have crashed IrfanView many times and discovered several bugs in it. I could have used something else but stuck with IV because it is such a powerful piece of software that ends up getting used for a lot of things that I do with photos.

Key PS cmdlets called:
  • Get-ChildItem is the function that looks up all the filenames that are in a directory path.
  • Test-Path tells us if a folder path exists.
  • Move-Item moves a file from one place to another
  • New-Item creates a new directory.
There are a few things in PS that take some getting used to and easily the biggest is the comparison operators which can be seen in the If statements. The biggest gain I have made in PS today is actually being able to write a multiline script which (incredibly) I had no idea how to do this morning. I have used a lot of PS commands to do Office365 stuff but never got around to putting the key commands into a script so they don’t have to be typed over and over. Well now I know how to do that…

Putting the Gigabyte video card in the computer is already proving its worth, mainly in better display outcomes. The biggest advantage of using an external card is to free up onboard memory that is normally shared with onboard graphics chips. I don’t know that that necessarily makes a big difference here. It is simply that the Gigabyte drivers are better than Intel’s that is likely to make the main improvement for me with this PC.

$photonames = Get-ChildItem -Path "D:\users\kahuk_000\Pictures\Photos"
foreach($filename in $photonames)
{
    if ($filename.Attributes -ne "Directory")
    {
        if ($filename.Length -ge 10)
        {
            $folderyear = $filename.Name.Substring(0,4)
            if ($folderyear -ge 1980)
            {
                if ($folderyear -le 2014)
                {
                    $foldermonth = $filename.Name.Substring(4,2)
                    $folderday = $filename.Name.Substring(6,2)
                    $foldername = $folderyear + "_" + $foldermonth + "_" + $folderday
                    $foldername
                    if ($folderyear -eq 2014)
                    {
                        $folderpath = "D:\users\kahuk_000\Pictures\Photos\" + $foldername
                    }
                    else
                    {
                        $folderpath = "D:\users\kahuk_000\Pictures\Photos\" + $folderyear + "\" + $foldername
                    }
                    if ((Test-Path $folderpath -pathtype container) -eq $true)
                    {
                        $filename.Name
                        Move-Item $filename.FullName $folderpath
                    }
                    else
                    {
                        New-Item $folderpath -type directory
                        Move-Item $filename.FullName $folderpath
                    }
                     
                }
            }
        }
    }
}