Tech Support > Operating Systems > MS-DOS > Getting input from a text file
Getting input from a text file
Posted by Ernesto on July 27th, 2005


Using WinXP, how can you get input from a text file from a batch? The
text file I will be using will have 3 different directories that I will
use at different times throughout the batch file. If I seperate them
with endlines

(i.e.

/////////////dir.txt/////////////////////////////
C:\dir1
C:\dir2
C:\dir3
/////////////////////////////////////////////////
)

How can I get the strings one at a time from the txt file?
Thanks!

Posted by William Allen on July 27th, 2005


"Ernesto" wrote in message
Use the FOR /f command. Read the online /? help under the FOR
command, in particular the syntax examples under the FOR /f switch:

for /?

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/



Posted by Ernesto on July 27th, 2005


Since the number of directories is small and bounded (3), is there
another way to get the strings from the text file. Here's some
pseudo-code.

GET DIRECTORY 1 STRING
myVar1 = directory 1 string

GET DIRECTORY 2 STRING
myVar2 = directory 2 string

GET DIRECTORY 1 STRING
myVar3 = directory 3 string

If I use the for loop, there's no way (without arrays) to set variables
= to directory string from text file.

Posted by Ernesto on July 27th, 2005


Using the FOR loop approach, I came up with this:

FOR /F "eol=; tokens= delims=" %a in (temp.txt) do (
@echo %a %b %c

)

I'm hoping this will get my directory strings and just print them out.
Unfortunately, I get a message that says "a was unexpected." All I
want to do is put some directory strings in three variables. What am I
doing wrong?

Posted by Ernesto on July 27th, 2005


OK. I just need one more small piece of help. I get the strings to
print with this command:

FOR /F "eol=;" %%i in (temp.txt) do @echo %%i

But how do I store my 3 strings into different variables during loop
iteration? I want to end up with something like that described in the
#3 post. Thanks!

Posted by William Allen on July 27th, 2005


"Ernesto" wrote in message
Assuming the file is called DIR.TXT as in your previous post, you could
write the three strings into a small three-element stack of environment
variables, something like this:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL
FOR /f "tokens=*" %%A IN (dir.txt) DO CALL :STACK %%A

ECHO. VAR1=%VAR1%
ECHO. VAR2=%VAR2%
ECHO. VAR3=%VAR3%
GOTO :EOF

:STACK
SET VAR1=%VAR2%
SET VAR2=%VAR3%
SET VAR3=%1

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

There are other ways to approach the issue. Which way is best depends
on why you want to put the strings in variables and what you want to do
with them when you do.

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/



Posted by Todd Vargo on July 27th, 2005



"Ernesto" <erniedude@gmail.com> wrote in message
news:1122487785.166298.216940@g43g2000cwa.googlegr oups.com...
Need some details. From the information above, it is not possible to
determine if the three directories are on the same line or on separate lines
in the text file. If you posted a sample listing of the text file, we would
not need to guess. As well, your meaning of "bounded" is not clear either. A
little detail on that would be great too.

BTW, for anyone not aware, there are groups specifically for NT/2K/XP
related batch discussions. Here are 2 such groups...
<news:alt.msdos.batch.nt>
<news://news.microsoft.com/microsoft.public.win2000.cmdprompt.admin>

And if you don't have access to those, try...
<http://groups.google.com/groups?as_ugroup=alt.msdos.batch.nt>

--
Todd Vargo (double "L" to reply by email)



Similar Posts