Click here for important information about Computing.net.

Batch rename with exclusion

January 27, 2021 at 02:38:30
Specs: Windows 10
Good day to you all,
im trying to complete this batch file that i put together very slowly (being a newbie with it),the aim is to rename files in different folders of my external HD with the option to rename them numerically,and so far it works, to the point where every folder has a file that i dont want to rename it (the file name is always the same in every folder through the HD named cover.jpg) ,only the other present ones,they have various different namings and so far with what i put together with online help too, it works great ,to this exclusion point that after 3 days gave me head aches.
It works ONLY by putting the batch file in every folder to execute it,what about if i want to put it at the top of my directories in order to execute it in all sub directories? here the code , i use
Notepad +++

@echo off
setlocal ENABLEDELAYEDEXPANSION
set/a fileNum = 1

for %%f in (*.jpg) do (
ren %%~nf%%~xf !fileNum!%%~xf
set/a fileNum += 1
)

I tried many combinations with the "exclude" but no hurray unfortunately.
And honestly with all those Switches ,Functions and Arrays,regular expressions i´m literally confused,obviously i didn't expect to learn and understand it all in 3 days :)
Anyone could be helpful please ?
thank you in advance


message edited by desmoulins


See More: Batch rename with exclusion


#1
January 27, 2021 at 12:46:50
Batch files are ok, but Bulk Rename Utility (BRU) would likely be better for such:

https://www.bulkrenameutility.co.uk/

I use it all the time and haven't had a single bag or advertisement.

"Channeling the spirit of jboy..."


Reply ↓  Report •

#2
January 27, 2021 at 23:39:15
Thank You for your input, i know it and used it already since a long time ago.I asked for help to do it in a different way and to learn something new.Thanx

Reply ↓  Report •

#3
January 28, 2021 at 18:31:59
maybe, untested yet:
@echo off & setlocal
set prev=""
for /f "tokens=*" %%a in ('dir f:*.jpg /s /b^|find /v /i "cover.jpg"') do call :xx "%%~npa" "%%~nxa" "%%a"
goto :eof

:xx
:: path is arg1, name-ext is arg2
if %prev% neq %1 (
set prev=%1
set c=0
)
set /a c+=1
echo ren %3 %c%.jpg

I'm assuming you want the sequence to start at 1.jpg for each subdir. It does not attempt to rename,
it just shows what would happen. You could direct output to another batchfile, then run that
if you're confident that it does what you want. (That would also gives you a record of what was done to what.)


Reply ↓  Report •

Related Solutions

#4
January 28, 2021 at 23:14:13
Thank you for your feedback,
yes i wish that the sequence starts at 1.jpg for each subdirectory.Saying that it does not attempt to rename you mean, that looking at the code i see that it only " echo ren %3 %c%.jpg " and for the little i understand the "do ren" part is missing in purpose, like you said it does not attempt to rename.If i direct the output to another batch file would it be the one with the "ren" part?In the meantime i play around a bit and see with what i come up with.By the way, could i just have all in one batch instead of redirect and what would be the main purpose to redirect in such a scenario?
Very grateful and appreciate your suggestion.

Reply ↓  Report •

#5
January 29, 2021 at 19:01:16
As I mentioned, two reasons: a) we don't want a "loose cannon on deck". Things can
go really south turning mass process loose on a filesystem. b) It will give you a record
of what was renamed to what.
You can put it into one batch easily enough, I just
wanted to "safety" the whole operation at least until you're satisfied it's not going to
trash your filesystem or data.
I figure you can make this adjustment (to one monolithic batch script),
but if you want it "sutured up" I will attempt do so.

Reply ↓  Report •

#6
January 30, 2021 at 02:27:30
Yes i know how bad things can go ,i did understood your good intentions THANX.And i understood that the output would be a record of what was done.I would and i try anyway only in a test environment that is an external HD where i can " let loose all the cannons " :) :) and see what happens.Well, it's funny how you mention "how easily" you can put it in one file,meaning, i would be very happy to master this "maze" of combinations myself, quite a long way to accomplish that.Ok great i accept your gentle offer to "suturing it up" :) for me mainly because i move on in the meantime having a lot of files to sort out and i mean a lot (20 years of photography approximately ) and with your help i will be able to make some adjustments (in the code) further on, according to my needs sorting out all those files.Thanx again and enjoy your weekend.

Reply ↓  Report •

#7
January 31, 2021 at 19:57:24
here's a "uni-body" version, still untested:
@echo off & setlocal
set prev=""
for /f "tokens=*" %%a in ('dir f:*.jpg /s /b^|find /v /i "cover.jpg"') do call :xx "%%~dpa" "%%~nxa" "%%a"
goto :eof

:xx
:: path is arg1, name+ext is arg2. arg3 is the fullpathfilename for convenience.
:: test to see if new directory has started, to reset the count/sequence
if %prev% neq %1 (
rem echo changing dir to %1 and resetting counter
set prev=%1
set c=0
)
set /a c+=1
ren %3 %c%.jpg

Found a couple of dumb mistakes (mine!) hopefully corrected here.

Reply ↓  Report •

Ask Question