1.

Solve : Trouble With A Recursive Rename Batch File?

Answer»

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]for /r %%a in (*.crop.jpg) do ren "%%a" "*.full.jpg"
OR

Code: [Select]for /r %%I in (*.crop.jpg) do ren "%%I" "%%~nI.full.jpg"
Result
Grandma.crop.jpg ----> Grandma.crop.full.jpgWhen you say "recursive" do you mean you want to operate on every file in a folder, and every file in every subfolder under that folder?
Quote from: Salmon Trout on April 06, 2016, 01:04:44 PM

When you say "recursive" do you mean you want to operate on every file in a folder, and every file in every subfolder under that folder?

Yes Try this
Put it in the top folder and if it shows the right renames
change this line:
echo REN "!oldname!" "!newname!"
to this:
REN "!oldname!" "!newname!"

Perhaps best to try it on some TEST files before you let it loose on your real data (if you are sensible you make backups!)

Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f %%A in ('dir /s /b *.full.jpg') do (
set folder=%%~dpA
set filename=%%~nA
set oldname=!folder!%%~nA.jpg
set newname=!filename:~0,-5!.crop.jpg
echo REN "!oldname!" "!newname!"
)
echo Finished
Pause
Example output

Code: [Select]C:\Batch\Test>test.bat
REN "C:\Batch\Test\apicture.full.jpg" "apicture.crop.jpg"
REN "C:\Batch\Test\sub1\testpicture.full.jpg" "testpicture.crop.jpg"
REN "C:\Batch\Test\sub1\sub2\anotherpicture.full.jpg" "anotherpicture.crop.jpg"
Finished
Press any key to continue . . . Quote from: Error Returned
File Not Found
Finished
Press any key to continue . . .

This looks awesome. I had no idea you could write scripts like this for the Command Line. Sadly, it isn't working yet. I have a test directory structure set up like this:
  • Grandma Photos (root dir)
  • Image0.crop.jpg
    • Grandma Trip 1 (1st Level sub dir)
      • Image1.crop.jpg
      • Image2.crop.jpg
    • Grandma Trip 2 (1st Level sub dir)
      • Image3.crop.jpg
      • Image4.crop.jpg
Folders = Red
Files = BlueInterchange crop and full in my scriptAlso posted here.
http://www.dostips.com/forum/viewtopic.php?f=3&t=7066Quote from: Squashman on April 07, 2016, 09:45:46 AM
Also posted here.
http://www.dostips.com/forum/viewtopic.php?f=3&t=7066
I received a workable solution at this link the other day. Then I CHECKED my email, saw this response, and it seemed impossible. My understanding of the command line is that you can do single line commands. This one is written more like code form a programming language. I was extremely intrigued and wanted to understand how this worked. When it didn't work I decided to follow up with it rather than announce I no longer needed it. I apologize if my actions have upset anyone .

Quote from: Salmon Trout on April 07, 2016, 12:48:49 AM
Interchange crop and full in my script
While the script runs all the way through, none of the actual file names are changed . This is the output:

I used a common testing safeguard, namely to preface the rename command with 'echo' so that you can first verify the renames are correct. I showed you how to deal with this, when I previously posted this above:

Quote
if it shows the right renames
change this line:
echo REN "!oldname!" "!newname!"
to this:
REN "!oldname!" "!newname!"
Quote from: Mulsiphix on April 07, 2016, 12:15:07 PM
My understanding of the command line is that you can do single line commands. This one is written more like code form a programming language.
At the command prompt you can only enter single line commands but you can combine them in a 'batch script'. The language is called Windows command language, or Windows batch language. You can find numerous reference guides to the syntax online.
Quote from: Salmon Trout on April 07, 2016, 12:38:19 PM
I used a common testing safeguard, namely to preface the rename command with 'echo' so that you can first verify the renames are correct. I showed you how to deal with this, when I previously posted this above:
I mean none of the actual file names on the hard drive are changed. The batch still isn't affecting the files. I made the change you requested previously. Just to be certain, this is what I'm running:

Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f %%A in ('dir /s /b *.crop.jpg') do (
set folder=%%~dpA
set filename=%%~nA
set oldname=!folder!%%~nA.jpg
set newname=!filename:~0,-5!.full.jpg
echo REN "!oldname!" "!newname!"
)
echo Finished
Pause
Quote from: Salmon Trout on April 07, 2016, 12:43:14 PM
At the command prompt you can only enter single line commands but you can combine them in a 'batch script'. The language is called Windows command language, or Windows batch language. You can find numerous reference guides to the syntax online.
That is awesome. I had no idea. Considering it's utility, I'm definitely going to make an effort. I recently took the time to really try and learn AutoHotKey. It has changed my life, but there are some things (like recursive renaming) that are overly complicated without a higher degree of programming knowledge and prowess. So this is welcome news. Thank you for cluing me in Quote
Just to be certain, this is what I'm running:
Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f %%A in ('dir /s /b *.crop.jpg') do (
set folder=%%~dpA
set filename=%%~nA
set oldname=!folder!%%~nA.jpg
set newname=!filename:~0,-5!.full.jpg
echo REN "!oldname!" "!newname!"
)
echo Finished
Pause

Can you see the big red echo? You need to delete this, as I said above (twice).

@echo off
setlocal enabledelayedexpansion
for /f %%A in ('dir /s /b *.crop.jpg') do (
set folder=%%~dpA
set filename=%%~nA
set oldname=!folder!%%~nA.jpg
set newname=!filename:~0,-5!.full.jpg
echo REN "!oldname!" "!newname!"
)
echo Finished
PauseEgg on my face . I thought your reference to echo was explaining the output I saw in the CMD window. I had no idea it was actually interfering with the OPERATION of the batch. I have made the change and the script is now returning errors.

Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f %%A in ('dir /s /b *.crop.jpg') do (
set folder=%%~dpA
set filename=%%~nA
set oldname=!folder!%%~nA.jpg
set newname=!filename:~0,-5!.full.jpg
REN "!oldname!" "!newname!"
)
echo Finished
Pause


I'm messing with the script and I'm getting now here simply fiddling with various parts of it. So I used some echo's and figured out the variables aren't holding the correct information. No clue how to fix that, but its a start. If you'd like to just pass on this, given I have a workable solution and I very well may just be aggravating you at this point, I completely understand. Thank you so MUCH for your help thus far. I appreciate you taking a moment to help me learn .

You'll notice "-=-=" appears in the Filename variable below. While the batch is in the root of the Grandma Photos directory, Grandma Photos itself is within a folder named "-=-= test dir 2 =-=-". Seems like Filename is somehow pulling a portion of that directory name instead.

what do you see if you open a command prompt in the folder where the batch is located, and type (or paste) this at the prompt and hit ENTER?

dir /s /b *.crop.jpg


Quote from: Salmon Trout on April 07, 2016, 01:51:37 PM
what do you see if you open a command prompt in the folder where the batch is located, and type (or paste) this at the prompt and hit ENTER?

dir /s /b *.crop.jpg



Also, I tried changing the test dir's name to one with no spaces and ran the echo modified script again. This returned the following:



Discussion

No Comment Found