Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Mulsiphix
- Posts: 3
- Joined: 05 Apr 2016 19:27
#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?
TryingCode: Select all
for /r %%a in (*.crop.jpg) do ren "%%a" "*.full.jpg"
ORCode: Select all
for /r %%I in (*.crop.jpg) do ren "%%I" "%%~nI.full.jpg"
ResultGrandma.crop.jpg ----> Grandma.crop.full.jpg
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#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
#3
Post
by Mulsiphix » 06 Apr 2016 13:07
It works
!! 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
#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
#5
Post
by Mulsiphix » 07 Apr 2016 11:10
Ah hah! I see now
. Thank you very much
.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#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