How to replace extension of a filename stored in variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to replace extension of a filename stored in variable?

#1 Post by pstein » 28 Feb 2020 01:10

I have a variable %file% which contains a filename including drive and path.

Now I want to replace the extension of this filename (whatever it is) and append .png to it.

If the filename would be stored in a numeric system variable %1 I could write something like

set newfile=%~dpn1.png

But what if the file name is %file% as mentioned above?

set newfile="%~dpnfile".png

does not work.

How else can I achieve it?

Keep in mind that the filename could contain blanks and/or special chars like "-" dashes and "." dots

Thank you
Peter

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to replace extension of a filename stored in variable?

#2 Post by aGerman » 28 Feb 2020 01:20

Parameters and FOR variable share the modifiers you're looking for. So all you have to do is process the variable in a FOR loop

Code: Select all

set "file=C:\somewhere\foo.bar"
for %%i in ("%file%") do set "newfile=%%~dpni.png"
Steffen

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: How to replace extension of a filename stored in variable?

#3 Post by pstein » 28 Feb 2020 02:02

Hmm, dann bekomme ich ein Abort mit:

The following usage of the path operator in batch-parameter
substitution is invalid: %~dpni.png"

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to replace extension of a filename stored in variable?

#4 Post by aGerman » 28 Feb 2020 02:14

Sorry I wrote the code straight into the browser without testing. The error message is clear, I forgot to double the percent sign as it is required in a batch script. Corrected above.

Steffen

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: How to replace extension of a filename stored in variable?

#5 Post by pstein » 28 Feb 2020 03:14

Ok, it works now, but only in the simplest scenario.
My original problem is derived from a more complex script:

Code: Select all

@echo off &setlocal DisableDelayedExpansion
setlocal EnableDelayedExpansion
....
set "file1=C:\somewhere\foo.bar"
for %%i in ("%file1%") do set "newfile1=%%~dpni.png"
echo newfile1=%newfile1%

for %%i in (!params!) do (
echo iii=%%~i
set "file2=%%~i"
echo file2=%file2%
for %%j in ("%file2%") do set "newfile2=%%~dpnj.png"
echo newfile2=%newfile2%
....
)
So the first replacement is successul but NOT the second.
%%i contains a valid file name. Output is e.g

iii="D:\archive\2019\test.jpg"

Now I performed an inner replacement according to your suggestion (with different "FOR" variable "j")
But (see above)

file2=

is already empty. Why does the assignment
set "file2=%%~i"
Not work?
So second replacement could not take place

Is this because of

setlocal EnableDelayedExpansion

?

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: How to replace extension of a filename stored in variable?

#6 Post by OJBakker » 28 Feb 2020 06:03

The 'set "file2=%%~i"' does work, but is done inside the for-commandblock enclosed in ()
Use delayed expansion to echo and use the uptodate value of %file2%.
Change %file2% in !file2! and %newfile2% in !newfile2! and your code will work as expected.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to replace extension of a filename stored in variable?

#7 Post by aGerman » 28 Feb 2020 11:12

If I see this kind of code I'm always wondering what the advantage of defining an additional environment variable actually is. You already have the value in the FOR variable which keeps being valid as long as you use it inside of the body of the loop.

Steffen

Post Reply