Trouble With A Recursive Rename Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mulsiphix
Posts: 3
Joined: 05 Apr 2016 19:27

Trouble With A Recursive Rename Batch File

#1 Post by Mulsiphix » 05 Apr 2016 19:32

I have a ton of files with names that end in *.crop.jpg and I would like to change crop to full. I have done a lot of searching and I read the Microsoft TechNet articles for REN and FOR, but I just can't get it. Is what I am trying to do impossible inside a batch file?

Trying

Code: Select all

for /r %%a in (*.crop.jpg) do ren "%%a"  "*.full.jpg"

OR

Code: Select all

for /r %%I in (*.crop.jpg) do ren "%%I"  "%%~nI.full.jpg"

Result
Grandma.crop.jpg ----> Grandma.crop.full.jpg

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trouble With A Recursive Rename Batch File

#2 Post by foxidrive » 06 Apr 2016 12:29

Remove the echo after you test this to see what it shows on the console.

Code: Select all

@echo off
for %%a in (*.crop.jpg) do for %%b in ("%%~na") do echo ren "%%a" "%%~nb.full%%~xa"
pause & goto :EOF

Mulsiphix
Posts: 3
Joined: 05 Apr 2016 19:27

Re: Trouble With A Recursive Rename Batch File

#3 Post by Mulsiphix » 06 Apr 2016 13:07

It works :mrgreen:!! Thank you so much!!

I hadn't even noticed the %%~x variable with modifier yesterday on the MS For web page. I'm trying to understand what is happening in your code and the %%b variable is throwing me off. I surprised the code works, based on my very limited understanding of the Command-Line commands. The parts highlighted in red are what throw me, the blue I understand.

for %%a in (*.crop.jpg) do for %%b in ("%%~na") do echo ren "%%a" "%%~nb.full%%~xa"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trouble With A Recursive Rename Batch File

#4 Post by foxidrive » 07 Apr 2016 09:16

I find that echo and pause are the most useful debugging commands, so give the code below a shot and see if you can figure out what if happening. Ask again if it's still unclear.

Code: Select all

@echo off
for %%a in (*.crop.jpg) do for %%b in ("%%~na") do echo "~na"="%%~na" "~nb"="%%~nb" & pause

Mulsiphix
Posts: 3
Joined: 05 Apr 2016 19:27

Re: Trouble With A Recursive Rename Batch File

#5 Post by Mulsiphix » 07 Apr 2016 11:10

Ah hah! I see now :oops:. Thank you very much :wink:.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trouble With A Recursive Rename Batch File

#6 Post by foxidrive » 07 Apr 2016 16:12

I will clarify it a little for future readers.

The %%~n part is given the filename up to the last period in the filename, and the extension is dropped.

If you have more periods in a filenames such as apple.two.plus.txt then you can get the first segment with %%~n and it will contain apple.two.plus in the for variable.

If you add more for loops then you can extract each part when you use the %%~n with the previous for variable.

Run this example code:

Code: Select all

@echo off
set "variable=apple.two.plus.txt"
echo The variable is %variable%
for %%a in ("%variable%") do (echo 1: %%~na
for %%b in ("%%~na")      do (echo 2: %%~nb
for %%c in ("%%~nb")      do (echo Here's the %%~nc
)))
pause



This is the output:

The variable is apple.two.plus.txt
1: apple.two.plus
2: apple.two
Here's the apple

Post Reply