Comparing strings or characters with quotes.

Dell INSPIRON 1520
June 25, 2009 at 06:57:32
Specs: Microsoft Windows 7 Ultimate, 2.001 GHz / 2046 MB
I'm trying to make a batch file to decode several audio files to wav by typing:
decode file
decode "file"

The problem I'm having is detecting the first " character in the second case, I can't get it to work. I tried to compare the first character with the last one too (%DecoderTemp:~-1%) without success.

Here is an example for mp3 and flac files:

echo off
set DecoderTemp=%1
:: The problem seems to be here, comparing the " char.
if %DecoderTemp:~0,1%==" (
   :: Get the extension:
   set FileExtension=%DecoderTemp:~-5%
   :: Delete the " final character:
   set FileExtension=%FileExtension:~0,-1%
   :: In DecoderTemp place the filename of the file:
   set DecoderTemp=%DecoderTemp:~1,-4%
) else (
   :: Get the extension:
   set FileExtension=%DecoderTemp:~-4%
   :: In DecoderTemp place the filename of the file:
   set DecoderTemp=%DecoderTemp:~0,-4%
if /I %FileExtension%==.mp3 (
   echo Decoding a MPEG-X Layer 3 file:
   lame --decode %1
   :: Rename the output file:
   rename %DecoderTemp%.mp3.wav %DecoderTemp%.wav
   goto end
if /I %FileExtension%)==flac (
   echo Decoding a FLAC file:
   flac --decode %1
   goto end
:: Decoder not available:
echo Decoder not available for %FileExtension:~1%.
goto end


See More: Comparing strings or characters with quotes.

Report •

June 25, 2009 at 09:51:29
set DecoderTemp=%1
If you write this as
set DecoderTemp=%~1
CMD will strip off the surrounding double quote characters, if they exist.

Report •

June 25, 2009 at 10:54:54
Thank you very much!

This way I don't even have to compare.

EDIT: It doesn't work in all cases. If the filename contains the ampersand symbol it doesn't work that way. :(

For example:

decode "complex&filename.mp3"

Thanks for the answer and your time anyway.

Report •

June 25, 2009 at 12:31:05
It does, but then we get into different issues. Command parser issues. You should be able to avoid most of the issues if you use delayed expansion. See
setlocal /?
cmd /?
for more information.

Report •

Related Solutions

Ask Question