Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
sri
- Posts: 6
- Joined: 05 Aug 2008 19:11
#1
Post
by sri » 06 Aug 2008 06:47
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
-
sri
- Posts: 6
- Joined: 05 Aug 2008 19:11
#3
Post
by sri » 14 Aug 2008 06:41
thanks very much for your help. it worked
-
sri
- Posts: 6
- Joined: 05 Aug 2008 19:11
#4
Post
by sri » 14 Aug 2008 23:38
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
-
greenfinch
- Posts: 36
- Joined: 17 Jul 2008 07:37
#5
Post
by greenfinch » 17 Aug 2008 10:27
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
-
sri
- Posts: 6
- Joined: 05 Aug 2008 19:11
#6
Post
by sri » 17 Aug 2008 21:56
Thanks very much for both the replies. Appreciate your help