0

I have initial file: CAR_001.dat I have 3 other files next to it:

CAR_001_prev0.png
CAR_001_prev1.png
CAR_001_prev2.png

I'm trying to rename these 3 files to:

CAR_001_NEWSTRING_prev0.png
CAR_001_NEWSTRING_prev1.png
CAR_001_NEWSTRING_prev2.png

here's my code:

ren %~dpn1_prev*.png %~n1_NEWSTRING_prev*.png

I drop CAR_001.dat onto the script and it processes PNG files. It's working okay except it removes the numeration and is able to rename only 1 file instead of 3

How can I process them correctly?

1 Answers1

0

I assume only the png files that have a *.dat file associated with them should be renamed. You have to drag and drop the folder where the *.dat & *.png files are to the batch file...

@echo off

set String=_banana

if /i exist "%~1" (set "Folder=%~1") else (exit)

pushd "%Folder%" for /f "delims=" %%a in ('dir /b .dat') do For /f "Delims=" %%b in ('dir /b "%%~na".png') do call :Rename "%%~na" "%%b"

exit

:Rename set "Part1=%~1" set "Part2=%~2" call set "Part2=%%Part2:%Part1%=%%" ren "%~2" "%Part1%%String%%Part2%" goto :EOF

enter image description here

In this example the dog_cat PNGs are not renamed because they don't have *.dat file that have part of the name associated with them.