quick batch file help?

fly

omg
Oct 1, 2004
79,197
27,246
1,323
Marklar
₥84,726
Steam
mattressfish
I need a batch file to read a txt file and put the contents into a variable. Im having a brain fart on how to do it.

I know that 'type' will echo the contents, but how do i get it into a variable?:tard:
 
behold the powers of google

From [email protected] Fri Nov 11 01:01:59 2005
Subject: 118. How to put the content of text file into a variable?
Date: Fri, 11 Nov 2005 01:01:59
From: [email protected] (Timo Salmi)

Q: I want to put the contents of text file into a variable. I tried
TYPE file.txt > %TEST% but that did not work.

A: This is how to solve it
@echo off
echon set test=>tmp$$$.bat
type file.txt>>tmp$$$.bat
call tmp$$$.bat
echo test=%test%
del tmp$$$.bat

But, it is not usually quite that simple. The above requires 1) that
the text is on the first line and 2) that there is only one line.
However, this situation is solvable. Also multiple lines can be
handled relatively easily by concatenation using a "paste" Unix port
like CONCAT.EXE "Join two text-files side by side". First, say there
are exactly _two_ lines. Then
@echo off
echo set first_=>tmp$$$.1
echo set second_=>>tmp$$$.1
concat tmp$$$.1 file.txt>tmp$$$.bat
call tmp$$$
echo first_=%first_%
echo second_=%second_%
for %%v in (first_ second_) do set %%v=
for %%f in (tmp$$$.*) do if exist %%f del %%f

If there are more than two lines in file.txt one can ensure the
exact two lines using SED.EXE
type file.txt|sed -n 1,2p>tmp$$$.2
concat tmp$$$.1 tmp$$$.2>tmp$$$.bat

The same, naturally, goes for ensuring that there is just the one
line, which we originally started the discussion from. So voila :)
sed -n 1p

If you first wish to delete all the potential empty and rem lines
from your file.txt file run it through
sed "/^$/d;/^.*[rR][eE][mM]/d"
or
sed "/^ *$/d;/^.*[rR][eE][mM]/d"
if you consider lines with spaces only as blanks.

A GAWK-based solution for putting say the first line of a file into
an environment variable is
@echo off
type myfile.txt|gawk 'NR==1{printf "@set first_=%%s\n",$0}'>tmp$$$.bat
for %%c in (call del) do %%c tmp$$$.bat
:: Test it
echo The first line is = %first_%
:: Clean up
set first_=
Alternatively
@echo off
<myfile.txt gawk '{printf"@set first_=%%s\n",$0;exit}'>tmp$$$.bat
for %%c in (call del) do %%c tmp$$$.bat
:: Test it
echo The first line is = %first_%
:: Clean up
set first_=

What about the last line?
@echo off
type myfile.txt|gawk '{printf "@set last_=%%s\n",$0}'>tmp$$$.bat
for %%c in (call del) do %%c tmp$$$.bat
:: Test it
echo The last line is = %last_%
:: Clean up
set last_=

What about the last but one?
@echo off
type myfile.txt|gawk 'END{printf "@set lines_=%%s\n",NR-1}'>tmp$$$.bat
for %%c in (call del) do %%c tmp$$$.bat
::
type myfile.txt|gawk 'NR==%lines_%{printf "@set line_=%%s\n",$0}'>tmp$$$.bat
for %%c in (call del) do %%c tmp$$$.bat
echo The desired line is = %line_%
::
:: Clean up
for %%v in (lines_ line_) do set %%v=
If you want just, say, the second word on the line last but one,
then replace the $0 with $2 in the above.

There is another way of getting the last line but one with G(nu)AWK.
type myfile.txt|gawk '{prev=last}{last=$0}END{printf "%%s\n",prev}'
 
FOR /F > THAT

Code:
IF EXIST "C:\tcs\unattendServer.txt" (
FOR /F %%I IN (C:\tcs\unattendServer.txt) DO SET SERVER=%%I
) ELSE (
SET SERVER=USMTSERV1
)