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
How to replace extension of a filename stored in variable?
Moderator: DosItHelp
Re: How to replace extension of a filename stored in variable?
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
Steffen
Code: Select all
set "file=C:\somewhere\foo.bar"
for %%i in ("%file%") do set "newfile=%%~dpni.png"
Re: How to replace extension of a filename stored in variable?
Hmm, dann bekomme ich ein Abort mit:
The following usage of the path operator in batch-parameter
substitution is invalid: %~dpni.png"
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?
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
Steffen
Re: How to replace extension of a filename stored in variable?
Ok, it works now, but only in the simplest scenario.
My original problem is derived from a more complex script:
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
?
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%
....
)
%%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?
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.
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?
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
Steffen