Computing.Net > Forums > Programming > DOS - File Read for environment var

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.

DOS - File Read for environment var

Reply to Message Icon

Name: trusp
Date: February 11, 2009 at 01:02:11 Pacific
OS: DOS
CPU/Ram: Intel DUO
Product: Ms / Ms/e6550
Subcategory: Batch
Comment:

Using Batch file(.bat Dos based)
I have to read a text file(temp.txt) containing data as the following:

#,User,Password,Feed,Capacity
Trusp,doit123,DNR,25
#,Thamba,ert23,IRR,30
#,Andrew,,,,

Using batch file I have to read the contents of the file and have to set variables as

User=Trusp
Pass=doit123
Feed=DNR
Capacity=25


So for executing this script the user has to simply edit the values in temp.txt

This is the script I created for this function
for /f "eol=; tokens=1-4 delims=," %%i in ("temp.txt") do (
if NOT %%i==#(
set User=%%i
set Pass=%%j
set Feed=%%k
set Capacity=%%l
)
)

Here I need to read the text file and set the environment variables.
So please help me out in this issue.




Sponsored Link
Ads by Google

Response Number 1
Name: trusp
Date: February 11, 2009 at 04:28:31 Pacific
Reply:

Hi I modified the script as below

for /f "usebackq eol= tokens=* delims= " %%i in ("temp.txt") do (
set STL_INFO=%%i
for /f "usebackq eol= tokens=* delims=, " %%a in (%STL_INFO%) do (
if NOT %%a=='#'(
set User=%%a
set Pass=%%b
set Feed=%%c
set Capacity=%%d
------
------
)
)
)

But still I could not get a solution.
Can any one help me out in this issue ????

Trusp


0

Response Number 2
Name: trusp
Date: February 11, 2009 at 21:20:36 Pacific
Reply:

I can access when I use %%a.
But I cant assign the value to a variable and access the values.
Using like this : set User=%%a
set Pass=%%b
Is it a wrong solution.

This is the script I am using:
for /f "usebackq eol= tokens=* delims= " %%i in ("temp.txt") do (
for /f "usebackq eol= tokens=1-5 delims=, " %%a in ('%%i') do (
if NOT %%a==# (
echo ---------------
echo %%a
echo %%b
echo %%c
echo %%d
echo ---------------
set User=%%a
set Pass=%%b
set Feed=%%c
set Capacity=%%d
echo ---------------
echo %user%
echo %pass%
echo %Feed%
echo %Capacity%
echo ---------------
)
)
)

Thanks.

Trusp


0

Response Number 3
Name: Holla
Date: February 11, 2009 at 21:36:20 Pacific
Reply:

Trusp,
You are almost there:
When you say DOS, does it mean real DOS (the OS of olden days or the command window under XP, Vista etc?

for /f "eol=# tokens=1-4 delims=," %%i in (temp.txt) do (
set User=%%i
set Pass=%%j
set Feed=%%k
set Capacity=%%l
)

--
Holla.


0

Response Number 4
Name: trusp
Date: February 11, 2009 at 21:57:53 Pacific
Reply:

Thanks Holla. Its working fine.


0

Response Number 5
Name: trusp
Date: February 12, 2009 at 22:17:16 Pacific
Reply:

Holla
This is not working when I have many arguments as below in Temp.txt file:

#,User,Password,Feed,Capacity,
Trusp,doit123,DNR,25,
#,Anderson,,,,
Thamba,ert23,IRR,30,
#,Andrew,,,,

Please help me out in this issue

Trusp


0

Related Posts

See More



Response Number 6
Name: Holla
Date: February 15, 2009 at 22:09:55 Pacific
Reply:

Trusp,

You have two lines in the above file with no # in the beginning of the line.
In such cases, how do you want to handle them?
Do you want the first line to be read or the last line to be read?

--
Holla.


0

Response Number 7
Name: trusp
Date: February 15, 2009 at 22:34:37 Pacific
Reply:

Thanks Holla for your response

Setlocal EnableDelayedExpansion
for /f "usebackq eol= tokens=* delims= " %%f in (temp.txt) do (
for /f "usebackq eol= tokens=1-4 delims=, " %%i in ('%%a') do (
if NOT %%i==# (
....
)))

By using this its working fine.


Trusp


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: DOS - File Read for environment var

DOS - File Read www.computing.net/answers/programming/dos-file-read/17489.html

C++ File Reading www.computing.net/answers/programming/c-file-reading/16213.html

DOS File Creation www.computing.net/answers/programming/dos-file-creation/17027.html