Read a file in DOS script
Moderator: DosItHelp
Read a file in DOS script
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
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
Re: Read a file in DOS script
How many lines?
What do you need to do with them?
What do you need to do with them?
Re: Read a file in DOS script
Hi
There are 2 lines in the file
One contains a value that I want to use to set an environment variable
There are 2 lines in the file
One contains a value that I want to use to set an environment variable
Re: Read a file in DOS script
Ok - that's pretty straight forward.
You want to set a variable to the last line?
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"
Re: Read a file in DOS script
Thanks for that
would you mind explaining what the script is doing?
I am very new to dos programming!
would you mind explaining what the script is doing?
I am very new to dos programming!
Re: Read a file in DOS script
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.
It only remembers that last one, so effectively it sets the variable to the last line in the file.
Re: Read a file in DOS script
Thanks
What does /f "delims" mean?
What does /f "delims" mean?
Re: Read a file in DOS script
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.
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.
Re: Read a file in DOS script
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
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
but variable still contains the equals sign
Any help you can give is greatly appreciated
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
Re: Read a file in DOS script
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)
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
Re: Read a file in DOS script
hi
when i run this script I get
\Common was unexpected at this time?
any ideas?
when i run this script I get
\Common was unexpected at this time?
any ideas?
Re: Read a file in DOS script
hi i tested the batch i get this :
is that what supposed to get
i run windows 7 32bit
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
Re: Read a file in DOS script
yes
I can make it work as well
Could you please explain the syntax of
Thanks for your patience
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
Re: Read a file in DOS script
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
Re: Read a file in DOS script
here is a better explaination for the delims and tokens
http://www.dostips.com/forum/viewtopic.php?f=3&t=2319
http://www.dostips.com/forum/viewtopic.php?f=3&t=2319