Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.txtThis 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.

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

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

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.

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

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.

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

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

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