Computing.Net > Forums > Programming > Extracting String data from a line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Extracting String data from a line

Reply to Message Icon

Name: deviprasad
Date: December 11, 2008 at 02:29:17 Pacific
OS: XP
CPU/Ram: P4 and 0.99GB
Product: Acer / 7600G
Comment:

Hi,
I am new to Windows batch programming. I have a requirement of extracting a string data from a line.

I have a file let say (doctype.txt) containing the following values in separate lines.

VESPRO/B
CHPOI13A/B
CHSAE01A/F
SAEHO01/F
CHPPOI06/B

I want to get the String data before the forward slash (/) into a variable one by one.

Like: VESPRO, CHPOI13A etc...

Please help me to resolve the above requirement?

Thanks in Advance.
Deviprasad




Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: December 11, 2008 at 05:02:05 Pacific
Reply:


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set csvar=
for /f "tokens=1 delims=/" %%a in (doctype.txt) do set csvar=!csvar!%%a,
echo %csvar%
endlocal
pause

Just bear in mind there is a variable string length limit, and this sort of thing can max it out.

The below are taken from this page

"The maximum individual environment variable size is 8192bytes.

The maximum total environment variable size for all variables,
which includes variable names and the equal sign, is 65,536KB."

It is possibly to direct the output to file to circumvent this limit, but you asked for variables so I can only assume these files aren't too large.


0

Response Number 2
Name: lee123abc
Date: December 11, 2008 at 05:03:20 Pacific
Reply:

@echo off & setlocal EnableDelayedExpansion
type nul > filename.txt
for /f "tokens=* delims=" %%A in (doctype.txt) do (
set line=%%A
set line=!line:~0,-2!
echo.!line!>> temp.txt)
start temp.txt


0

Response Number 3
Name: lee123abc
Date: December 11, 2008 at 05:17:12 Pacific
Reply:

Ok, I think I read wrong, my result will give you:
VESPRO
CHPOI13A
CHSAE01A
SAEHO01
CHPPOI06

and not:
VESPRO,CHPOI13A,CHSAE01A,SAEHO01,CHPPOI06


0

Response Number 4
Name: Holla
Date: December 11, 2008 at 23:06:02 Pacific
Reply:

I think deviprasad wants
var1=VESPRO
var2=CHOPI13a
var3=...
etc.

In that case,


@echo off
setlocal EnableDelayedExpansion
set varcount=1
for /f "tokens=1 delims=/" %%a in (doctype.txt) do (
set var!varcount!=%%a
:Uncomment below line if you want the variables outside the batch file.
:echo set var!varcount!=%%a >> setvarslater.bat
set /a varcount+=1
)

--
Holla.


0

Response Number 5
Name: lee123abc
Date: December 12, 2008 at 01:41:34 Pacific
Reply:

Sorry!!!!

Enjoy your weekend guys!


0

Related Posts

See More



Response Number 6
Name: deviprasad
Date: December 12, 2008 at 05:04:00 Pacific
Reply:

Hi All,
Thanks for all your suggestion.

I tried the way suggested by Lee and got the solution to my problem.

I have done the below:

for /f %%a in (docType.txt) do (
call :CreateScript %%a
)

:CreateScript
set fn=%1
set docType=%fn:~0,-2%


Thanks for all your support.
Deviprasad


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Extracting String data from a line

C programming: extract data from a www.computing.net/answers/programming/c-programming-extract-data-from-a-/6224.html

Extracting data from file www.computing.net/answers/programming/extracting-data-from-file/11046.html

How to import data from a webpage www.computing.net/answers/programming/how-to-import-data-from-a-webpage-/8773.html