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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

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

#1 Post by phoenix_Rising » 25 Jul 2012 15:32

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 25 Jul 2012 15:40

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

phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

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

#3 Post by phoenix_Rising » 25 Jul 2012 18:23

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#4 Post by Squashman » 25 Jul 2012 18:39

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

phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

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

#5 Post by phoenix_Rising » 26 Jul 2012 01:56

I see what you mean -- That seems to have worked!

Thanks so much!

skaliam
Posts: 8
Joined: 23 Jul 2012 12:09

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

#6 Post by skaliam » 05 Aug 2012 23:37

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#7 Post by foxidrive » 06 Aug 2012 03:30

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

Post Reply