Computing.Net > Forums > Programming > Batch Extracting part of a string

Batch Extracting part of a string

Reply to Message Icon

Original Message
Name: eixuian
Date: August 9, 2007 at 09:22:21 Pacific
Subject: Batch Extracting part of a string
OS: 2K SP4
CPU/Ram: 512MB
Model/Manufacturer: Dell
Comment:

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 D

So, 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? :)


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: August 9, 2007 at 10:20:20 Pacific
Reply: (edit)

Start your batch by coding

@Echo Off
SetLocal EnableDelayedExpansion

then

: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



Report Offensive Follow Up For Removal

Response Number 2
Name: eixuian
Date: August 9, 2007 at 13:27:27 Pacific
Reply: (edit)

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 7294

So 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!
)
pause

I 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.


Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: August 9, 2007 at 14:08:12 Pacific
Reply: (edit)

: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.


Report Offensive Follow Up For Removal

Response Number 4
Name: eixuian
Date: August 14, 2007 at 05:33:34 Pacific
Reply: (edit)

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?


Report Offensive Follow Up For Removal

Response Number 5
Name: IVO
Date: August 14, 2007 at 08:58:21 Pacific
Reply: (edit)

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!


Report Offensive Follow Up For Removal


Response Number 6
Name: eixuian
Date: August 15, 2007 at 11:56:42 Pacific
Reply: (edit)

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. :)


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch Extracting part of a string

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge