View unanswered posts | View active topics It is currently 22 Sep 2014 16:17



Post new topic Reply to topic  [ 7 posts ] 
Create a loop to set variable from text file and run batch 
Author Message

Joined: 04 Apr 2012 03:11
Posts: 38
Post Create a loop to set variable from text file and run batch
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


25 Jul 2012 15:32
Profile

Joined: 23 Dec 2011 13:59
Posts: 1842
Post Re: Create a loop to set variable from text file and run bat
You don't need to use the type command. That is what the /F switch is for.
Code:
for /f "delims=" %%i in (c:\server_audit\serverlist.txt) do call :start %%i


25 Jul 2012 15:40
Profile

Joined: 04 Apr 2012 03:11
Posts: 38
Post Re: Create a loop to set variable from text file and run bat
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


25 Jul 2012 18:23
Profile

Joined: 23 Dec 2011 13:59
Posts: 1842
Post Re: Create a loop to set variable from text file and run bat
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


25 Jul 2012 18:39
Profile

Joined: 04 Apr 2012 03:11
Posts: 38
Post Re: Create a loop to set variable from text file and run bat
I see what you mean -- That seems to have worked!

Thanks so much!


26 Jul 2012 01:56
Profile

Joined: 23 Jul 2012 12:09
Posts: 8
Post Re: Create a loop to set variable from text file and run bat
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?


05 Aug 2012 23:37
Profile

Joined: 10 Feb 2012 02:20
Posts: 4194
Post Re: Create a loop to set variable from text file and run bat
This is a skeleton to show you the sequence and where to continue your batch file

Code:
@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


06 Aug 2012 03:30
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.