Page 1 of 1

String Manipulation Error

Posted: 14 Aug 2008 23:23
by sri
Hi

Can you please help with the below problem

I am trying to do a string compare but i am getting "The syntax of the command is incorrect"

Code: Select all

@Echo off
setlocal enabledelayedexpansion
set check=some
set fourchars=
FOR /F "TOKENS=1,2 DELIMS=_." %%a IN ("file_somenamev1.3.zip") do (set token1=%%a&set token2=%%b)    
   echo token1 %token1%
   set fourchars=%token2:~0,4%    
   echo %fourchars%
   if %fourchars% == %check%(
      echo found
   )


Thanks in advance

Posted: 17 Aug 2008 10:40
by greenfinch
Same problem as the main topic you posted - line breaks confuse DOS/CMD processor:

Code: Select all

@Echo off
setlocal enabledelayedexpansion
set check=some
set fourchars=
FOR /F "TOKENS=1,2 DELIMS=_." %%a IN ("file_somenamev1.3.zip") do (set token1=%%a&set token2=%%b)   
   echo token1 %token1%
   set fourchars=%token2:~0,4%
   echo '%fourchars%'
   if %fourchars% == %check% (echo found)

Posted: 28 Aug 2008 20:57
by DosItHelp
I suggest to always put quotes around the variable expansions. It helps to remove confusion for the command interpreter, i.e. the variable name check can now contain pretty much any character without breaking the if statement. It may still break when it contains quotes, haven't tried, but other characters like ><(@^ should all be fine.

if "%fourchars%"=="%check%" (
echo found
)