Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

DOS: Rename files recursivly

Hi
I need to rename some files

This works if I cd to the directory first

cd \s z:\Path\with\files\in
ren *.jpeg *.jpg

Open in new window



How do I write a simple bat file that takes the parent folder as a parameter and recursively  renames the files skipping over if the files exist

so if a folder had both MyCat.jpeg and mycat.jpg  it won't over right mycat.jpg (Windows file names are not case sensitive) and won't prompt
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

Thanx Bill I'll give it ago when I'm on the system

assuming I've named the file

RenJPG.bat

how do run this

RenJPG.bat "z:\Path\with space\files\in"

Open in new window


I'm guessing something like this

@echo off
setlocal

set BaseDir="%~1"

for /r "%BaseDir%" %%F in ("*.jpeg") do (
    if not exist "%%~dpnF.jpg" (
        ren "%%~F" "*.jpg"
    )
)

Is the a wildcard?
"%%~dpnF.jpg"
Yes to passing the base folder path in as a parm, that looks okay, except don't add quotes around the %~1.

No, %%~dpnF.jpg is not using wildcards.  The %%F is the loop variable from the FOR statement, it will contain the full path of the current file as the FOR itterates.  %%dpnF uses modifiers dpn to get just the drive, path and base file name of the file, basically removing the .jpeg extension.  Then %%~dpnF.jpg concatenates .jpg at the end so we can determine if there is already a JPG file there with the same name.


»bp
Thanx Bill for the answer and follow up explanation
Welcome, glad that was helpful.


»bp