1.

Solve : Text file as input??

Answer»

Hi folks.

I really want to know how i make a batch file that takes input from a text file and puts it in a variable.

Its because im going to make a game with database technology.

ps. How do i make batch do math?file.txt
Code: [Select]SomeTextHere
batfile.bat

@echo off
Code: [Select]for /f %%a in (file.txt) do set var=%%a
echo %var%
pause

OR


file.txt
Code: [Select]life=100
batfile.bat
Code: [Select]@echo off
for /f "tokens=1,2 delims==" %%a in (file.txt) do set life=%%b
echo %life%
pauseJoedavis,

That is completely irrelevant to what he is needing. Please read the post first before replying.Quote from: macdad- on April 08, 2009, 12:50:22 PM

Joedavis,

That is completely irrelevant to what he is needing. Please read the post first before replying.

Relevance doesn't worry joedavis.
it's irrelevant because they asked how to get a batch to do math, not for a batch to do math.Quote from: joedavis on April 08, 2009, 02:01:35 PM

I'm sure BC is correct. Would BC explain "How to get a Batch file to do floating point math?


billrich, we know it's you! We covered FP maths in batch AGES ago.
Quote from: On40 on April 08, 2009, 09:52:32 AM
Hi folks.

I really want to know how i make a batch file that takes input from a text file and puts it in a variable.

Its because im going to make a game with database technology.

ps. How do i make batch do math?

see that last part? PS? that doesn't mean, "devote all your time and resources to solving this one, simple problem with various ways of creating one line batch programs that often shell external interactive windows applications"


Devcom cliched the CORE of the question- your attempt to address the PS would have been better served with an explanation of the /a switch, rather then the PROVISION of multiple samples that include references to the google calculator and shell the windows calculator.


From Set /?:

Quote
Two new switches have been added to the SET command:

SET /A expression
SET /P variable=[promptString]

The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated. The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:

() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator

If you use any of the logical or modulus operators, you will need to
enclose the expression string in quotes. Any non-numeric strings in the
expression are treated as environment variable names whose values are
converted to numbers before using them. If an environment variable name
is specified but is not defined in the current environment, then a value
of zero is used. This allows you to do arithmetic with environment
variable values WITHOUT having to type all those % signs to get their
values. If SET /A is executed from the command line outside of a
command script, then it displays the final value of the expression. The
assignment operator requires an environment variable name to the left of
the assignment operator. Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.
Quote from: Dias de verano on April 08, 2009, 02:20:00 PM
billrich, we know it's you! We covered FP maths in batch ages ago.


LOL, my first draft of my post started with, "billrich... I mean, joedavis..."


BC,


I hope the Dandy from England with the Spanish name continues to help all the new people.

Best of Luck to everyone.

Quote from: On40 on April 08, 2009, 09:52:32 AM
Its because im going to make a game with database technology.
here's a more fun way to make games.Here's some code for crying out loud: This is assuming that the info will only be 1 line in the file on the first line:

Code: [Select]for /f %%Z in ('type [path to file]') do set variable=%%Z

You should be able to echo %variable%Quote from: devcom on April 08, 2009, 10:12:14 AM
file.txt
Code: [Select]life=100
batfile.bat
Code: [Select]@echo off
for /f "tokens=1,2 delims==" %%a in (file.txt) do set life=%%b
echo %life%
pause

Code: [Select]@echo off
for /f "tokens=*" %%a in (file.txt) do set %%a
echo %life%
pauseOh thanks for the help, but can you please explain what happens and how i type the syntax for the file reading thing? I really want to learn it in and out so i modify and tweak it in alot of directions.try for /? in cmd, for better explanation someone will explain, coz i dont speak Eng so good Thanks people.

Especiallly from you devcom! I found out that it could read multi-line info making it more effective. Im going to make that game now!


Discussion

No Comment Found