Page 1 of 1
How to replace extension of a filename stored in variable?
Posted: 28 Feb 2020 01:10
by pstein
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
Re: How to replace extension of a filename stored in variable?
Posted: 28 Feb 2020 01:20
by aGerman
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
Re: How to replace extension of a filename stored in variable?
Posted: 28 Feb 2020 02:02
by pstein
Hmm, dann bekomme ich ein Abort mit:
The following usage of the path operator in batch-parameter
substitution is invalid: %~dpni.png"
Re: How to replace extension of a filename stored in variable?
Posted: 28 Feb 2020 02:14
by aGerman
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
Re: How to replace extension of a filename stored in variable?
Posted: 28 Feb 2020 03:14
by pstein
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
?
Re: How to replace extension of a filename stored in variable?
Posted: 28 Feb 2020 06:03
by OJBakker
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.
Re: How to replace extension of a filename stored in variable?
Posted: 28 Feb 2020 11:12
by aGerman
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