Computing.Net > Forums > Disk Operating System > Batch arguments from a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch arguments from a file

Reply to Message Icon

Name: Thierry
Date: January 8, 2003 at 01:44:32 Pacific
OS: win2000
CPU/Ram: P4 256MB
Comment:

Why doesn't this work ?

[batch.bat]
echo %1

[tmp.txt]
dummy

c:\batch.bat This does not print "dummy" as I would expect.

Can one tell me how to do so ?

/T



Sponsored Link
Ads by Google

Response Number 1
Name: Thierry@accellent
Date: January 8, 2003 at 01:48:54 Pacific
Reply:

I meant :
c:\batch.bat '<'tmp.txt does not work.
('<' otherwize the '<' is not printed out.)


0

Response Number 2
Name: Miskva3
Date: January 8, 2003 at 06:22:50 Pacific
Reply:

What are you trying to do exactly ?


0

Response Number 3
Name: Secret_Doom
Date: January 8, 2003 at 09:44:34 Pacific
Reply:

I think he created those two files discribed on his first message, then tryed:

batch.bat < tmp.txt

And he expected that the batch script got the string in the text file as first parameter.

Why it doesn't work? Well... because it's just not supposed to hehe... Batch files don't read input from stdin, like some .exe and .COM applications. Instead, do this:

[batch.bat]
for /F %%A in (tmp.txt) do echo %%A

[tmp.txt]
dummy

For more information, type "FOR/?" on the command prompt. Pay attention to the part talking about the /F switch.

Keep in mind that this FOR/F thing will only work on NT systems, not on Win9x nor DOS (there are other methods for those OS's).

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


0

Response Number 4
Name: Chad
Date: January 8, 2003 at 09:54:22 Pacific
Reply:

my my....no need go through all those crazy solutions...your problem is simple.

Problem: used "echo %1" in batch file.
reason: This causes it to "echo tmp.txt" and prints the name of the file, if anything. What you want is the contects of the file.

Solution: replace "echo %1" with "type %1". Type dumps the contents of the text file specified.


0

Response Number 5
Name: Secret_Doom
Date: January 8, 2003 at 13:51:47 Pacific
Reply:

Well, that prooves how UNclear the original poster were. If he just wanted to display the contents of the text file, Chad is right, it is simple as that. However, if he wants to use the string on the text file as a variable (and that's what I thought he meant), it takes other procedures (like the one I described). Just using TYPE won't do it.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


0

Related Posts

See More



Response Number 6
Name: Thierry@accellent
Date: January 9, 2003 at 02:48:47 Pacific
Reply:

Ok, my first request was probably not clear. Sorry for that.

What I want, is to have the batch file arguments taken from a file:

you usually do :
batch.dat %1 %2 %3

I want %1 %2 %3 to be taken from a file that contains %1 %2 %3.

The answer from Secret_Doom is good, but still not enough. I have to be able to SHIFT %1 %2 %3 and so on. Using the FOR command implies major limitation - as far as I could see, when trying to assign a variable in the loop (expansion pbs)-.

By the way, thank you all for helping me.


0

Response Number 7
Name: Secret_Doom
Date: January 9, 2003 at 13:34:26 Pacific
Reply:

That's much more clear, Tierry.

You see that you can get the strings to environment variables (like %ONE% or %TWO%, but not yet %1 or %2) with the FOR cmd, right? So, call a subroutine with those variable as parameters, so they will become parameters for the subroutine. Here's an example:

[temp.txt]
The dog is running
Why is the cat still?

[script.bat]
@echo off
if "%1"=="GoTo" goto %2
for /F "tokens=*" %%A in (temp.txt) do call %0 GoTo process %%A
goto eof
:process
if "%3"=="" goto eof
echo %3
shift
goto process
:eof

This subroutine thing is kinda difficult to understand on the begginning, but it's very useful. You could do the same thing with two batch scipts, calling the second from the first. But then, you need two files! With this subroutine thing, it's like you have both files on the same one.

Anyway, the output from that script would be:

The
dog
is
running
Why
is
the
cat
still?

But all those were once represented by %3.

Is that what you wanted?

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

_______________________________________________________


0

Response Number 8
Name: Thierry@accellent
Date: January 14, 2003 at 01:32:18 Pacific
Reply:

Hi Leonardo

It starts looking good...
Thanks again for your help.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Batch arguments from a file

Set number of lines from a file to a var www.computing.net/answers/dos/set-number-of-lines-from-a-file-to-a-var/6487.html

Batch to get from a list in a file. www.computing.net/answers/dos/batch-to-get-from-a-list-in-a-file/14009.html

A batch to read from a file? www.computing.net/answers/dos/a-batch-to-read-from-a-file/4566.html