DOS - File Read for environment var

Ms / Ms/e6550
February 11, 2009 at 01:02:11
Specs: DOS, Intel DUO
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.



See More: DOS - File Read for environment var

Report •


#1
February 11, 2009 at 04:28:31
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


Report •

#2
February 11, 2009 at 21:20:36
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


Report •

#3
February 11, 2009 at 21:36:20
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.


Report •

Related Solutions

#4
February 11, 2009 at 21:57:53
Thanks Holla. Its working fine.

Report •

#5
February 12, 2009 at 22:17:16
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


Report •

#6
February 15, 2009 at 22:09:55
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.


Report •

#7
February 15, 2009 at 22:34:37
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


Report •


Ask Question