Selecting just the last file in each folder and renaming it

A swapping-ground for Regular Expression syntax

Selecting just the last file in each folder and renaming it

Postby martinmart » Sat Oct 24, 2020 1:21 pm

Dear friends,

I've tried to find the answer to this topic, but couldn't

I have folders with jpgs in sequential order (..._001, ..._002, ..._003, etc.)

My problem is how to select automatically just the last file in every folder
I have approx. 2000 such folders, number of files is different in each folder

a path name is 39_...._0_0_.(..)_...
example - files in folder 1624, subfolder 1:
it starts with 39_1624_0_0_1_001
and ends with 39_1624_0_0_1_089

i need to select this last file numbering "_089" and change it for "_999_okladka" = 39_1624_0_0_1_999_okladka

another example - files in folder 1642, subfolder 88:
it starts with 39_1642_0_0_88_001
and ends with 39_1642_0_0_88_101

and i need to change ..._101 to ..._999_okladka

Any suggestions would be greatly appreciated :)
martinmart
 
Posts: 3
Joined: Sat Oct 24, 2020 12:22 pm

Re: Selecting just the last file in each folder and renaming it

Postby therube » Mon Oct 26, 2020 5:59 pm

pseudo-code (so you'd have to work through the details)...

Code: Select all
:loop through dirs
for i in (*)
do
   dir  /b /s  %i  |  tail.exe -1 >> c:\tmp\rename_list.efu

Code: Select all
for 1...2000 directories
do a directory listing
   piping the output to tail
   tail says to only retain the last file name
   & then (append) that name to a file named, rename_list.efu


That should give you a list of file names like, rename_list.efu:
Code: Select all
C:\TT2015\american english\Acoustic Alchemy - American-English - Front.jpg
C:\TT2015\Flower Travelling Band - Anywhere \Flower Travelling Band - Inlay.jpg
C:\TT2015\Kitaro - House of Blues - Chicago\HOB.jpg

Open that file into Everything (File | Open File List),
which will show you that list of file names. Select them all (Ctrl+A), & drag them into BRU,
& do the actual renaming from there.

tail is UNIX-like command.
Found at GNU coreutils.
You'll need both: coreutils-5.3.0-bin.zip & coreutils-5.3.0-dep.zip.
From bin.zip, pull tail.exe out of the /bin/ directory.
From dep.zip, pull both .dll files out of the /bin/ directory.
Place all 3 files somewhere in your path.

And you'll have to have actually working code (batch file), not just "pseudo-code".
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Selecting just the last file in each folder and renaming it

Postby martinmart » Tue Oct 27, 2020 4:37 am

Thank You Very Much!

your advice is invaluable...
martinmart
 
Posts: 3
Joined: Sat Oct 24, 2020 12:22 pm

Re: Selecting just the last file in each folder and renaming it

Postby Luuk » Tue Oct 27, 2020 7:26 am

Greetings everybody, Im using the everything.exe too! The exiftool.exe can do this also do this, but its very long command like this....
Code: Select all
For /r %X in (.) do @exiftool -q -ext jpg -Fast4 -FileOrder -FileName -if "$FileSequence==0 and $Filename =~ /_\d{3}\.(?i)jpe?g$$/" -TESTname"<${filename;s/(.*_)\d+(.*)/$1\999$2/}" "%X"

You must first go to the top folder to type all that on the command, then the exiftool presents to screen like this..
'C:/Path/file_123_023.jPg' --> 'C:/Path/file_123_999.jPg'

If to be happy with all the results, then it can be safe to change -TESTname into -Filename
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: Selecting just the last file in each folder and renaming it

Postby martinmart » Thu Oct 29, 2020 10:04 pm

Thank You Luuk!

am experimenting with it...
martinmart
 
Posts: 3
Joined: Sat Oct 24, 2020 12:22 pm

Re: Selecting just the last file in each folder and renaming it

Postby Luuk » Fri Oct 30, 2020 3:44 pm

Greetings Martin, Im just noticed the _okladka! So in the experiments, just to put _okladka after 999 like this..
Code: Select all
For /r %X in (.) do @exiftool -q -ext jpg -Fast4 -FileOrder -FileName -if "$FileSequence==0 and $Filename =~ /_\d{3}\.(?i)jpe?g$$/" -TESTname"<${filename;s/(.*_)\d+(.*)/$1\999_okladka$2/}" "%X"
Its to rename only when *_### is also the last name in folder.. This to let names like zzz.jpg forbid the rename, but if you like it the other way, so that zzz.jpg not forbids the rename, the exiftool may conduct this way also.
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm


Return to Regular Expressions