Read a file in DOS script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
grahaml
Posts: 11
Joined: 15 Mar 2012 10:42

Read a file in DOS script

#1 Post by grahaml » 20 Mar 2012 06:23

Hi
Could someone please tell me the best way to read a text file in a DOS script.
I need to store each line in a variable that I can use in my script

Thanks in advance

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read a file in DOS script

#2 Post by foxidrive » 20 Mar 2012 07:16

How many lines?

What do you need to do with them?

grahaml
Posts: 11
Joined: 15 Mar 2012 10:42

Re: Read a file in DOS script

#3 Post by grahaml » 20 Mar 2012 07:31

Hi
There are 2 lines in the file
One contains a value that I want to use to set an environment variable

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read a file in DOS script

#4 Post by foxidrive » 20 Mar 2012 07:47

Ok - that's pretty straight forward.

You want to set a variable to the last line?

Code: Select all

@echo off
for /f "delims=" %%a in (file.txt) do set "variable=%%a"

grahaml
Posts: 11
Joined: 15 Mar 2012 10:42

Re: Read a file in DOS script

#5 Post by grahaml » 20 Mar 2012 07:49

Thanks for that
would you mind explaining what the script is doing?
I am very new to dos programming!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read a file in DOS script

#6 Post by foxidrive » 20 Mar 2012 07:50

It's setting a variable to each line in the file.txt in turn.

It only remembers that last one, so effectively it sets the variable to the last line in the file.

grahaml
Posts: 11
Joined: 15 Mar 2012 10:42

Re: Read a file in DOS script

#7 Post by grahaml » 20 Mar 2012 08:00

Thanks
What does /f "delims" mean?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read a file in DOS script

#8 Post by foxidrive » 20 Mar 2012 08:13

I'm happy to explain batch commands for a solution to a task but but I like to know the task - and then explanations of the working code are appropriate.

When people ask general questions it seems that they are students who are asking for information to solve their homework.
Mind you, students asking questions are ok too, when they are up front about it.

grahaml
Posts: 11
Joined: 15 Mar 2012 10:42

Re: Read a file in DOS script

#9 Post by grahaml » 20 Mar 2012 08:20

Hi
I'm an experienced software engineer and have been backed into a corner where I need to write a batch script!
The brief is to read a configuration file that has the following contents

Code: Select all

[Paths]
prefix = "C:\\Program Files (x86)\\CompanyName\\Ex"


What I need to do is read this file and extract the string C:\\Program Files (x86)\\CompanyName\\Ex
I then need to append \\lib to this string and add this string to the path environment variable
Finally I need to launch an application

So far I have

Code: Select all

@echo path is %path%
@echo off
for /f "delims=" %%a in (qt.conf) do set "variable=%%a"

@echo %variable%
set variable=%variable:prefix =%
@echo %variable%


but variable still contains the equals sign

Any help you can give is greatly appreciated

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read a file in DOS script

#10 Post by foxidrive » 20 Mar 2012 08:34

A mild scoff here - any experienced software engineer would know of the importance of outlining the task in the first place.

You may find some issues with the parenthesis in (x86)

Code: Select all

@echo off
for /f "skip=1 tokens=1,* delims== " %%a in (qt.conf) do (
for /f "delims=" %%c in ("%%~b") do set path=%path%;%%~c\\lib
)
echo %path%
pause
application.exe

grahaml
Posts: 11
Joined: 15 Mar 2012 10:42

Re: Read a file in DOS script

#11 Post by grahaml » 20 Mar 2012 09:14

hi
when i run this script I get
\Common was unexpected at this time?

any ideas?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Read a file in DOS script

#12 Post by abc0502 » 20 Mar 2012 09:23

hi i tested the batch i get this :

Code: Select all

C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;
C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\Windows Live\Shared;C:\Program Files\Microsoft SQL Server\90\To
ols\binn\;C:\\Program Files (x86)\\CompanyName\\Ex\\lib

is that what supposed to get
i run windows 7 32bit

grahaml
Posts: 11
Joined: 15 Mar 2012 10:42

Re: Read a file in DOS script

#13 Post by grahaml » 20 Mar 2012 09:32

yes
I can make it work as well
Could you please explain the syntax of

Code: Select all

for /f "skip=1 tokens=1,* delims== " %%a in (qt.conf) do (
for /f "delims=" %%c in ("%%~b") do set path=%path%;%%~c\\lib


Thanks for your patience

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Read a file in DOS script

#14 Post by abc0502 » 20 Mar 2012 09:47

Code: Select all

for /f "skip=1 tokens=1,* delims== " %%a in (qt.conf) do (

this line will search in the "qt.conf" file and the result will be set to tha variable %%a
the "skip=1" means that the command will skip the first line in the file "qt.conf"
the "tokens" mean which part of a line will look for like "my name abc0502"
every word count as a token when a space separate between them that mean "my" consider as token1
and name considre as token 2 and abc0502 is token 3

the "delims" used for making the search more Accurate it tell the search command to look for the tokens that is after the sign "=" and the "space" after it
after the search is done it do "the next line"

Code: Select all

for /f "delims=" %%c in ("%%~b") do set path=%path%;%%~c\\lib


same is here for the "delims" the %%~b and %%~c i don't really know the for command still confusing me but the bottom line (i think) is that it will search in the output line and add the "\\lib" to the end

i hope foxi explain that too
and for more information write for /? in the command prompt
i hope that help

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Read a file in DOS script

#15 Post by abc0502 » 20 Mar 2012 09:49

here is a better explaination for the delims and tokens
http://www.dostips.com/forum/viewtopic.php?f=3&t=2319

Post Reply