Page 1 of 1

String Manipulation/String Tokenization in DOS

Posted: 06 Aug 2008 06:47
by sri
Hi

I am trying to extract substring using tokens but i couldn't extract the second token in the string

Here is an example what i am trying to do
---------------------------------------------------
i have a list of files in a folder, the filenames are something like file_somenamev1.3.zip
i need to extract the first 4 characters from the second token ('some' from 'somenamev1.3.zip')

Can anyone please suggest how to do this. i have copied my code below. which isn't working. i couldn't retrieve the value into a variable

Code: Select all


FOR /F %%x in ('dir /b D:\temp\*.zip') do (      
   FOR /F "TOKENS=1,2 DELIMS=_ " %%C IN ('echo %%x') do (
      echo C:%%C D:%%D
      set str1 = %%C
                set str2 = %%D
REM The value in str1 & str2 is always null, couldn't initialize the values of %%C & %%D to str1 & str2
      echo str1 %str1%
      echo str2 %str2%
   )   
)   



thanks for your help
Sri

Posted: 13 Aug 2008 10:56
by greenfinch
Does this help?:

Code: Select all

FOR /F "TOKENS=1,2 DELIMS=_." %%a IN ("file_somenamev1.3.zip") do (set token2=%%b)
set fourchars=%token2:~0,4%
echo %fourchars%


This page is very useful:
http://www.dostips.com/DtTipsStringMani ... LeftString

thanks

Posted: 14 Aug 2008 06:41
by sri
thanks very much for your help. it worked

Posted: 14 Aug 2008 23:38
by sri
Hi greenfich

Thanks for your help. i am trying to incorporate the same logic into my bat script that i am working on but i am getting an error, "( was unexpected at this time."

Code: Select all

@Echo off

set checkfilename=filename
set find1=same
set find2=diff

REM    I have two files in temp directory
REM     filename_samenamev1.2.3.zip
REM     filename_diffnamev2.1.3.zip

FOR /F %%x in ('dir /b C:\temp\*.zip') do (
   FOR /F "TOKENS=1,2 DELIMS=_." %%A IN ('echo %%x') do (   
      set token1=%%A
      set token2=%%B
      echo token1 %token1%  token2 %token2%

      set threechars = %token2:~0,3%       
      
      if %%A == %checkfilename% (
      
         if %threechars% == %find1% (
            echo =============================================================================         
            echo Found %find1% file
            echo =============================================================================         
            goto :eof   
         )
         
         if %threechars% == %find2% (
            
            echo =============================================================================         
            echo Found %find2% file
            echo =============================================================================
            goto :eof      
         )
      )
   )
)   


Can you please help me to resolve the error.

Thanks very much for your help

Posted: 17 Aug 2008 10:27
by greenfinch
You can't quite format syntax that way, with line breaks to separate commands, if you are within a FOR command

Commands can be separated instead using && but this is hard to read

Instead, break things into subroutines and call them like this:

Code: Select all

setlocal
@Echo off

set checkfilename=filename
set find1=sam
set find2=dif

REM    I have two files in temp directory
REM     filename_samenamev1.2.3.zip
REM     filename_diffnamev2.1.3.zip

FOR /F %%x in ('dir /b c:\temp\*.zip') do (set file=%%x && call :sub1)

GOTO :end


:sub1
FOR /F "TOKENS=1,2 DELIMS=_." %%A IN ("%file%") do (set token1=%%A && set token2=%%B)
echo token1 %token1%  token2 %token2%
set threechars=%token2:~0,3%
if %token1% == %checkfilename% call :filenamematches
GOTO :EOF



:filenamematches
if [%threechars%] == [%find1%] (call :found1)
if [%threechars%] == [%find2%] (call :found2)
GOTO :EOF


:found1

echo =============================================================================
echo Found %find1% file
echo =============================================================================
GOTO :EOF   



:found2

echo =============================================================================
echo Found %find2% file
echo =============================================================================
GOTO :EOF     


:end


Note that since you used 3 chars instead of 4, I changed same and diff to sam and dif

Thanks

Posted: 17 Aug 2008 21:56
by sri
Thanks very much for both the replies. Appreciate your help