Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have read some of the posts on this forum, so I know there are some batch file masters around. I look forward to seeing what kind of solutions to this 'puzzle' they can come up with.
I am writing a batch script, part of which extracts a string from an index file, given part of the string has been supplied by user imput. Here is the code I have so far for that section of the file:
:D
rem Cls
echo -EMPL DB LOOKUP-
echo -Type "exit" to terminate, "new" to return to previous menu-
set /p m=Enter employee number:
if %m%==exit goto E
if %m%==new goto A
@find /N "%m%" J:\common\s_empl1.idx
pause
goto DSo, for example, when the user enters "03845" the script spits out:
---------- J:\COMMON\S_EMPL1.IDX
[3855]☻§03845
Press any key to continue . . .The only characters in the string I am concerned with are "☻§", which are index characters required to lookup information in another file. Regardless of the employee number fed by the user, the two important characters are always found in the same location in the string, just before.
Any ideas on how to extract something like this? :)

Start your batch by coding
@Echo Off
SetLocal EnableDelayedExpansionthen
:D
rem Cls
echo -EMPL DB LOOKUP-
echo -Type "exit" to terminate, "new" to return to previous menu-
set /p m=Enter employee number^>
if /I %m%.==exit. goto E
if /I %m%.==new. goto A
Echo.
For /F "tokens=2 delims=]" %%j in ('Type J:\common\s_empl1.idx ^| Find /N "%m%"') Do (
Set key=%%j
Echo -- !key:~0,2!
)
pause
goto D

Thanks, IVO. That works fantastically.
The first time I executed it I got some unusual output, but I was unable to replicate the error (which was probably just caused by me feeding mis-typed data, anyhow).
Regardless, I really appreciate the help.
But now I have the same type of problem, only I am trying to use the two characters we just generated to find a series of five digits from another file, which are all numbers. So, going back to my original example using ☻§:
H:\>find /N "^B^U" J:\common\s_chrdp1.idx
---------- J:\COMMON\S_CHRDP1.IDX
[4827]☻§43954 7294So now I am trying to pull the five characters directly following ☻§, which should all be numbers.
Here is what I got when I tried to adapt your code, but I couldn't figure out the syntax of some of your magic doo-dads:
For /F "tokens=5 delims=]" %%h in ('Type G:\tsheets\main\common\s_chrdp1.idx ^| Find /N "%%g"') Do (
Set key2=%%h
Echo !key2:~0,5!
)
pauseI couldn't figure out the syntax for Find, and I think I got the interval swapped with something else after the !key2:~ part. In any case it failed to do anything. Now that I am humbled pity would be appropriate.

:D
rem Cls
echo -EMPL DB LOOKUP-
echo -Type "exit" to terminate, "new" to return to previous menu-
set /p m=Enter employee number^>
if /I %m%.==exit. goto E
if /I %m%.==new. goto A
Echo.
For /F "tokens=2 delims=]" %%j in ('Type J:\common\s_empl1.idx ^| Find /N "%m%"') Do (
Set key=%%j
Set key=!key:~0,2!
Call :SCAN
)
pause
GoTo :DONE:SCAN [from 2 chars key to 5 digits]
For /F "tokens=2 delims=]" %%m in ('Type G:\tsheets\main\common\s_chrdp1.idx ^| Find /N "%key%"') Do (
Set key2=%%m
Set key2=!key2:~2,5!
Echo %m% --^> key1=%key% --^> key2=!key2!
)
GoTo :EOF:DONE
Note: :EOF is the NT return built-in label, it doesn't need to be declared.

I tried that last bit of code, line for line, but it seems there is something about the :SCAN section that doesn't want to work for me. Everything else works well, but when the :SCAN section is called I get the following output without the desired 5 digit numbers:
Set key=%j
Set key=!key:~0,2!
Call :SCAN
)(
Set key=☻§03845
Set key=!key:~0,2!
Call :SCAN
)For /F "tokens=2 delims=]" %m in ('Type G:\tsheets\main\
common\s_chrdp1.idx | Find /N "!key:~0,2!"') Do (
Set key2=%m
Set key2=!key2:~2,5!
Echo 03845 --> key1=!key:~0,2! --> key2=!key2!
)GoTo :EOF
pause
Press any key to continue . . .
I'm not sure what is going wrong here...any ideas?

Did you start your batch by coding
SetLocal EnableDelayedExpansion
?
And why do you refer in your post to %j or %m?
In a batch internal variables must be prefixed by double %, so %j becomes %%j and %m %%m.By the way there are three types of variables:
- external, e.g. %1 to %9, referring to command tail parameters;
- internal to be used as index in For loops;
- environmental to be embraced by % or !, e.g. %key% or !key!

You guessed it again. I had the SetLocal EnableDelayedExpansion set in the 'actual file' I was building, but I had forgotten to add it to the file I was using to test code with. Good catch.
Thanks to you it works like a charm now. :)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |