Need a script to rename files in series of subfolders.

I'm pretty new to Powershell and would like some help.

I have a folder: C:\Pictures

In that folder is a bunch of sub folders, each with a unique name.  Then in those folders are pictures in .JPG format.

I would like a script that sequentially renames each .JPG within the subfolders, but leaves the subfolders alone (doesn't rename them) and then also renames the extension to a lowercase .jpg.

For example:

C:Pictures\Graduation\239490238.JPG, 349083048.JPG, etc, etc....becomes C:\Pictures\Graduation\Pic 01.jpg, Pic 02.jpg, etc, etc.

Then the script moves on to the next subfolder so:

C:\Pictures\Wedding\12120981209.JPG, 43048930489.JPG, etc, etc, becomes C:\Pictures\Graduation\Pic 01.jpg, Pic 02.jpg, etc, etc.

The order that the .JPGs are in when the script starts stays the same....just each file is renamed sequentially.  And then it moves on to the next subfolder.

I appreciate any help whatsoever!!!

June 23rd, 2015 9:52pm

We don't write custom scripts. The forum is for technicians and admins using script in their work.  You can try and write a script and post back with questions but we will not write it for you.

Help Get-ChildItem
Help Rename-Item

These are good places to start.

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 10:17pm

Hi Cable123

Try this as a start:

$FolderPath = "C:\Pictures"
$SubFolders = Get-ChildItem -Path $FolderPath -Recurse -Directory
foreach($SubFolder in $SubFolders)
    {
    $i= 1
    $Pictures = Get-ChildItem -Path $SubFolder.FullName -Filter *.JPG
    foreach ($Pic in $Pictures)
        {
        Rename-Item -Path $Pic.FullName -NewName ("Pic"+"{0:000}" -f $i+".jpg")
        $i++
        }
    }

Cheers

June 24th, 2015 7:07am

Thanks UnitWinGuy!!!

I've tried your script and I get the following error.  I have the PS1 file on the root of C:\ along with the C:\Pictures folder.  Wasn't sure if it should be placed elsewhere?

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At C:\rename.ps1:2 char:67
+  $SubFolders = Get-ChildItem -Path $FolderPath -Recurse -Directory <<<<
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
 
Rename-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\rename.ps1:9 char:27
+          Rename-Item -Path <<<<  $Pic.FullName -NewName ("Pic"+"{0:000}" -f $i+".jpg")
    + CategoryInfo          : InvalidData: (:) [Rename-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand

Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 12:17pm

The -directory parameter was not introduced until PowerShell 3.0. If you intend to use PowerShell you should upgrade to PowerShell 4.0.
June 24th, 2015 12:53pm

Okay....I've gotten it to work....in the original on the second line, you see the -Directory.  I changed that to -D and it works now (see below), but it throws up a bunch of errors when I run it in the Powershell editor/module/whatever it's called.  An example error is:

Rename-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\rename.ps1:9 char:27
+          Rename-Item -Path <<<<  $Pic.FullName -NewName ("Pic "+"{0:00}" -f $i+".jpg")
    + CategoryInfo          : InvalidData: (:) [Rename-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand

Since it works, should I just ignore those errors or correct whatever to eliminate them.

The next thing I need to address is the numbering.  The original script renamed everything to Pic001.jpg, Pic002.jpg, etc, etc.  What I would like to know is how to make it so that the script renames the pics in a certain way.  For example, if the folder contains less than 100 pictures, it renames them Pic 01.jpg, Pic 02.jpg...all the way to Pic 99.jpg.  Then if a folder contains more than 100 pictures, it renames them Pic 001.jpg, Pic 002.jpg...all the way to Pic 121.jpg, for example.  I would like the script to be intelligent enough to do that instead of just having another script to deal with folders that have more than 100 pictures.

Again...appreciate all the help....the little push that UnitWinGuy gave made all the difference.  I've been able to google much of this to help understand more.  I need a more formal PowerShell education though.  = )

$FolderPath = "C:\Pictures"
 $SubFolders = Get-ChildItem -Path $FolderPath -Recurse -D
 foreach($SubFolder in $SubFolders)
     {
     $i= 1
     $Pictures = Get-ChildItem -Path $SubFolder.FullName -Filter *.JPG
     foreach ($Pic in $Pictures)
         {
         Rename-Item -Path $Pic.FullName -NewName ("Pic "+"{0:00}" -f $i+".jpg")
         $i++
         }
     }

Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 2:58pm

Once I have the renaming part all done....I want to use Powershell to zip up the files in each of these subfolders and name the zip file to the name of each subfolder.

I've found this script and am playing with it...

function ZipFiles( $zipfilename, $sourcedir )
{
     Add-Type -Assembly System.IO.Compression.FileSystem
     $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
     [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
                                                          $zipfilename,
                                                          $compressionLevel,
                                                          $false)
}

June 24th, 2015 4:09pm

A formal PowerShell education is prohibitively expensive.  Why the room and board alone would bankrupt most of us.

The reason -D works is because you spelled "Directory" wrong.

Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 5:29pm

Hi Cable123

I added a few lines for you to address your naming pattern.

$FolderPath = "C:\Pictures"
$SubFolders = Get-ChildItem -Path $FolderPath -Recurse -Directory

foreach($SubFolder in $SubFolders)
    {
    $i = 1
    $j = 0
    $NamingFormat = "{0:"
    $Pictures = Get-ChildItem -Path $SubFolder.FullName -Filter *.JPG
    $PicCountLength = $Pictures.Count.ToString().Length
    while($j -lt $PicCountLength)
        {
        $NamingFormat = $NamingFormat +"0"
        $j++
        }
    $NamingFormat = $NamingFormat +"}"
    foreach ($Pic in $Pictures)
        {
        Rename-Item -Path $($Pic.FullName) -NewName ("Pic"+"$NamingFormat" -f $i+".jpg")
        $i++
        }
    }

There is probably a sleeker solution to solve this.

Where to you have to place your compressed folders?

Cheers

June 25th, 2015 3:26am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics