1

I need to sort through many folders (all in the same directory) to move and rename all the JPGs as "parentfolder" + "#", where "parentfolder" is the parent folder name and "#" is a consecutive label for each group of files in their respective folder.

For example, in the folder .\Input\MainID1 is a.jpg, b.jpg, c.jpg and some other file types. In another folder .\Input\MainID2 is also a.jpg, b.jpg, c.jpg and some other file types.

A desired output would be to save all the jps in a single folder renamed as:

File Path           New File name
MainID1\a.jpg       MainID1_1
MainID1\b.jpg       MainID1_2
MainID1\c.jpg       MainID1_3

Then have the loop reset when looking in the next folder to allow for:

File Path           New File name
MainID2\a.jpg       MainID2_1
MainID2\b.jpg       MainID2_2
MainID2\c.jpg       MainID2_3

I'm having trouble finding a way to restart the loop for each individual folder. Currently, my loop is iterating the entire directory's .jpg files.

C:\Users\Rango\Renaming\Input> Get-ChildItem -Recurse -Filter *.jpg |
ForEach-Object {$c = 1} 
{Write-Host $_.Directory.Name $c; $c++}
{ $c = 1}

Output:

Main ID 1 1
Main ID 1 2
Main ID 1 3
Main ID 1 4
Main ID 10 5
Main ID 10 6
Main ID 10 7
Main Id 100 8
Main Id 101 9
Main Id 102 10
Main Id 102 11
Main Id 102 12
Main Id 102 13
Main Id 102 14
Main Id 102 15
Main Id 102 16
Main Id 102 17
Main Id 102 18
Main Id 102 19
Main Id 102 20
Main Id 102 21
Main Id 102 22
Main Id 103 23
Main Id 104 24
Main Id 104 25
Main Id 105 26
Main Id 105 27
Main Id 106 28
Main Id 107 29
Main Id 109 30
etc.
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Andre
  • 15
  • 5

2 Answers2

0

One way to do this is to create a variable to store the directory name of the previous file (e.g. $oldName), and reset $c when this changes:

Get-ChildItem -Recurse -Filter *.jpg |
ForEach-Object {
    if($_.Directory.Name -ne $oldName){$c = 1}
    Write-Host $_.Directory.Name $c; $c++
    $oldName = $_.Directory.Name
}
G42
  • 9,791
  • 2
  • 19
  • 34
0

You can group by directoryName and loop on every files group like this :

Get-ChildItem "c:\temp" -Recurse -File -Filter *.jpg | group DirectoryName |%{
$Dir=$_
$Cpt=1
$_.Group | %{rename-item $_.FullName -NewName ("{0}\{1}_{2}" -f $Dir.Name, (Split-Path $Dir.Name -Leaf), $Cpt++) -WhatIf}
}

Notes:

  1. Use -File into Get-childitem (otherwise if a directory contain jpg directory is incude )
  2. If you want file name ended by 001, 002, 003 (keep same format of number), modify my code and replace "{0}{1}_{2}" by "{0}{1}_{2:d3}"
  3. Of course remove what-if if you want really apply this rename command
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • I'm not sure why, but this code comes up with an error saying that the JPG files do not exist - while referencing the correct path to the JPG file. – Andre Nov 04 '17 at 20:31
  • My mistake. Used -LiteralPath to specify Rename-Item's path as my paths had wildcard characters. This works perfectly, thank you. – Andre Nov 04 '17 at 21:06
  • Final Edit: Addition of .jpg at the end of renamed file. – Andre Nov 04 '17 at 21:24