user3450848
user3450848

Reputation: 23

Batch file to unzip then rename its jpgs sequentially

I have this structure:

Biographies <base folder>
----Elon Musk <person #1 folder>
----|---Modern-Day Genius <chapter name folder>
----|-------Elon Musk - Modern-Day Genius.txt <person #1's biography>
----|-------Elon Musk - Modern-Day Genius.zip <50-100 photos of person #1>
----|-----------Driving a Tesla.jpg <photo 1>
----|-----------Elon Musk on the train.jpg <photo 2>
----|-----------SpaceX factory.jpg <photo ...>
----Steve Jobs <person #2 folder>
----|---Visionary <chapter name folder>
----|-------Steve Jobs - Visionary.txt <bio>
----|-------Steve Jobs - Visionary.zip <zip of photos>
----|-----------At his home in California.jpg <photo 1>
----|-----------At work on the first iPhone.jpg <photo 2>
----|-----------Steve Jobs yelling at people.jpg <photo ...>

I'm looking to 1) unzip the .zip file (using 7zip) 2) rename the extracted .jpgs using A) the text file name + B) an incremented number.

So, you'd end up with:

Under the "Modern-Day Genius" folder, you'd have:
Elon Musk - Modern-Day Genius 001.jpg
Elon Musk - Modern-Day Genius 002.jpg
Elon Musk - Modern-Day Genius 003.jpg

because "Elon Musk - Modern-Day Genius" was the root filename of the txt file and 001-00n because of some counter.

and under the "Visionary" folder, you'd have:
Steve Jobs - Visionary 001.jpg
Steve Jobs - Visionary 002.jpg
Steve Jobs - Visionary 003.jpg

etc.

Pieces of this process sound easy enough but I have literally spent 20 hours trying to figure this out over the last week. I'm not brand new to Windows batch files, but I'm not very good at it. Obviously. ;-) I've read a hundred or so posts, examples, 'tutorials', etc. and I'm not finding pieces of solutions anywhere online. I've tried so many variants and have a dozen batch files that sort of do, but don't really work.

I think figuring this out would be of benefit not only to me but to a bunch of other people trying to unzip a file, the rename its contents based on some external parameter. Seems pretty universally useful.

Here's one example but not one w/ renumbering. I have 'working' renumbering batch files, but they don't really work (they rename the first file only, etc.) it's a mess. I'm hoping someone much smarter than me at this stuff can help figure something out.

@echo off
setlocal enabledelayedexpansion
set PATH=%PATH%;C:\Program Files\7-Zip\

for %%t in ('dir /b *.txt) do (
set textfilename=%%t)

7z e *.zip

for /f %%a in ('dir/b *.jpg) do (
rename "*.jpg" "!textfilename! - *.jpg"
)

Upvotes: 0

Views: 222

Answers (2)

user6811411
user6811411

Reputation:

So you start in Biographies and recursively search zips,
unzip jpg's from them into the same folder and
rename these with the name of the zip file and an incremented number. Using a for /f and afterwards for var modifiers ~ this isn't to hard.

This batch:

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
:: CD /D "X:\Biographies"
For /f "delims=" %%Z in ('Dir /B/S *.zip') Do (
  Pushd "%%~dpZ"
  7z.exe e "%%~Z" *.jpg >Nul 2>&1
  Set Cnt=1000
  For %%J in (*.jpg) Do (
    Set /A Cnt+=1
    Ren "%%J" "%%~nZ !Cnt:~-3!%%~xJ"&&Echo Renamed  "%%J" to "%%~nZ !Cnt:~-3!%%~xJ"
  )
  PopD
)

Has this sample output.

Renamed  "Elon Musk on the train.jpg" to "Elon Musk - Modern-Day Genius 001.jpg"
Renamed  "Driving a Tesla.jpg" to "Elon Musk - Modern-Day Genius 002.jpg"
Renamed  "SpaceX factory.jpg" to "Elon Musk - Modern-Day Genius 003.jpg"

Renamed  "Steve Jobs yelling at people.jpg" to "Steve Jobs - Visionary 001.jpg"
Renamed  "At his home in California.jpg" to "Steve Jobs - Visionary 002.jpg"
Renamed  "At work on the first iPhone.jpg" to "Steve Jobs - Visionary 003.jpg"

This is a tree output of the resulting structure.

 > tree /F Biographies
X:\BIOGRAPHIES
 ├───Elon Musk
 │   └───Modern-Day Genius
 │           Elon Musk - Modern-Day Genius 001.jpg
 │           Elon Musk - Modern-Day Genius 002.jpg
 │           Elon Musk - Modern-Day Genius 003.jpg
 │           Elon Musk - Modern-Day Genius.txt
 │           Elon Musk - Modern-Day Genius.zip
 │
 └───Steve Jobs
     └───Visionary
             Steve Jobs - Visionary 001.jpg
             Steve Jobs - Visionary 002.jpg
             Steve Jobs - Visionary 003.jpg
             Steve Jobs - Visionary.txt
             Steve Jobs - Visionary.zip

Upvotes: 1

abelenky
abelenky

Reputation: 64710

To get the name of the .txt file:

for %%a in (*.txt) do ( set Prefix=%%~na)

(example: %PREFIX% will be set to Modern-Day Genius)

Putting it together with some pretty straightforward numbering code:

@echo off    
setlocal enabledelayedexpansion
for %%a in (*.txt) do ( set Prefix=%%~na)

set ZEROS=0000000
set COUNTER=1
for %%a in (*.jpg) do (
    set Value=%ZEROS%%COUNTER%
    set Value=!VALUE:~-3!
    echo rename %%a "%PREFIX% - !VALUE!"
    set /A COUNTER=COUNTER+1 )

I'm not going to deal with recursing into multiple directories, or dealing with ZIP files. It looks like you can handle that part.

Upvotes: 0

Related Questions