Page 1 of 1

Create a loop to set variable from text file and run batch

Posted: 25 Jul 2012 15:32
by phoenix_Rising
Hi guys,

Really trying to get to the bottom of this... I have written a short amateur batch file that simply pulls some basic system info and dumps it as a txt file... at the moment it prompts for human interaction with the user typing the name of the server and then the batch file referring to the info as a variable. What i want it to do it automatically read each line of the serverlist.txt file (which is full of server names that may have the "-" character in the name?) And then set this information as a variable to run the batch file

When it finishs i want it too loop through all the entries in the serverlist.txt and then stop!

Heres the code:

@ECHO OFF
CLS
mkdir C:\SERVER_AUDIT
c:
cd\SERVER_AUDIT
ECHO.
ECHO Server Info finder...
ECHO ======================
ECHO.
ECHO PRESS "1" TO READ A LIST OF SERVERS FROM SERVERLIST.TXT
ECHO.
ECHO PRESS "2" TO INPUT AN INDIVIDUAL SERVER
ECHO.
ECHO.
CHOICE /c:12
IF ERRORLEVEL 2 GOTO :SINGLE
IF ERRORLEVEL 1 GOTO :MULTIPLE

:SINGLE
:Start
set /p SERVER="Enter the name of the server - "
:pingtest
echo IP Address =>>%SERVER%.log
ping -n 1 %SERVER%>temp.log
findstr /i /c:"Reply from" temp.log>>%SERVER%.log
:ilotest
ping %SERVER%x>temp.log
findstr /i /c:"Reply From" temp.log
if %errorlevel% equ 1 goto notfound
echo iLO = Yes>>%SERVER%.log
goto done
:notfound
echo iLO = No >>%SERVER%.log
goto done
:done

:OSinfo
systeminfo /s %SERVER% /u .\Administrator /p somepassword /FO LIST>temp.log
findstr /i /c:"OS NAME" temp.log>>%SERVER%.log
findstr /i /c:"Domain" temp.log>>%SERVER%.log
findstr /i /c:"System Manafacturer" temp.log>>%SERVER%.log
findstr /i /c:"System Model" temp.log>>%SERVER%.log
findstr /i /c:"Host Name" temp.log>>%SERVER%.log

:serial
ECHO Server Serial Number:>>%SERVER%.log
wmic /USER:"%SERVER%\Administrator" /PASSWORD:"somepassword" /NODE:"%SERVER%" bios get serialnumber>>%SERVER%.log

Any ideas?

Although its not valid code... was sort of looking down this type of route...:

set SERVER=
for /f "delims=" %%i in ('type c:\server_audit\serverlist.txt') do call :start

Re: Create a loop to set variable from text file and run bat

Posted: 25 Jul 2012 15:40
by Squashman
You don't need to use the type command. That is what the /F switch is for.

Code: Select all

for /f "delims=" %%i in (c:\server_audit\serverlist.txt) do call :start %%i

Re: Create a loop to set variable from text file and run bat

Posted: 25 Jul 2012 18:23
by phoenix_Rising
Thanks Squashman, I dont believe that my line of code is right though reagardless of the correction as it seems that i need to set the variable %SERVER% to a line from the serverlist.txt file and then run through the whole batch file. When thats finished it needs to do the same thing with every line in the serverlist.txt

Re: Create a loop to set variable from text file and run bat

Posted: 25 Jul 2012 18:39
by Squashman
When you use CALL the way I am doing it, the sub routine inherits the %%I variable. You can then reference it as %1 in your CALLed sub routine.

So at the beginning of the START subroutine just do a set statement
SET server=%1

Re: Create a loop to set variable from text file and run bat

Posted: 26 Jul 2012 01:56
by phoenix_Rising
I see what you mean -- That seems to have worked!

Thanks so much!

Re: Create a loop to set variable from text file and run bat

Posted: 05 Aug 2012 23:37
by skaliam
Hey I'm trying to do this as well
It does everything fine.
It can read the first line of a text file and use that
But im not understanding how to go to the next line after i have done what I need to.
There are sub routines, does this make a difference?

Re: Create a loop to set variable from text file and run bat

Posted: 06 Aug 2012 03:30
by foxidrive
This is a skeleton to show you the sequence and where to continue your batch file

Code: Select all

@echo off
for /f "delims=" %%i in (c:\server_audit\serverlist.txt) do call :start %%i
rem do your next commands here
pause
goto :EOF

:start
rem the loop commands go here
end of batch file