how to skip a line in a batch file

October 10, 2011 at 07:54:39
Specs: Windows 7
I'm currently using a script made by someone else from this forum (thank you whoever you are) and am in need of help. I'm using the script to point psshutdown to shutdown multiple computers by using the ip address that the script creates. For some reason though, if the ping fails to communicate with the computer, it will list the local ip address in the list. Then when psshutdown uses that list to send the shutdown command, it shuts down the local machine. How can I skip the lines that contain said ip address, or how can I go about not adding that address to the output file?
This is the script:
@echo off
if exist X:\output.txt del X:\output.txt else continue
set ComputerList=X:\FQDNs.txt

setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%A in ("%X:\FQDNs.txt%") do (
for /f "tokens=3" %%B in ('ping -4 -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo !IPadd:~0,-1!>>X:\output.txt

X:\psshutdown.exe -c -t 900 @X:\output.txt

See More: how to skip a line in a batch file

Report •

October 12, 2011 at 22:49:26
One option would be to not use ping to check for online hosts. You could check for the existence of any file under C: on the remote host instead, and then write those hosts to a file. I HAVE NOT tested this so please test first.

@echo off
setlocal enabledelayedexpansion

REM checking for existence of output.txt and deleting if it is present
if not exist X:\output.txt goto nofile
del X:\output.txt

for /f "tokens=*" %%A in ("X:\FQDNs.txt") do (
REM checking for online status by looking for any file under c drive and output to list of online hosts
if exist \\%%A\c$\*.* echo %%A >> online.txt

REM using list of online hosts to set IPadd variable
for /f "tokens=*" %%A in ("online.txt") do (
set IPadd=%%A
echo !IPadd:~0,-1!>>X:\output.txt

X:\psshutdown.exe -c -t 900 @X:\output.txt

Report •

October 13, 2011 at 14:09:46
Thank you for the reply. I became antsy, and so I looked around for alternatives. For the sake of anyone looking for the answer, I created (with the help of another blogger on a different site) a vbscript that would read the file with the ip addresses, locate the specific address I need removed, write a blank to that spot, then another part that would read the script for all blank spaces, and then remove the blanks. All I did to run this was pointed my original batch file to run the vbscript after the batch file was done pinging the machines. At the end of the script, I make the script wait for 15000 milliseconds (15 seconds) to ensure that the removal is complete before psshutdown runs. The vbscript is as follows:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("X:\output.txt", ForReading)

REM reads the file

strText = objFile.ReadAll
strNewText = Replace(strText, "", "")

REM singles out the ip that needs removed

Set objFile = objFSO.OpenTextFile("X:\output.txt", ForWriting)
objFile.WriteLine strNewText

REM writes empty line where the singled out ip's were

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("X:\output.txt", ForReading)

Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If

REM looks for lines that have 0 characters


Set objFile = objFSO.OpenTextFile("X:\output.txt", ForWriting)
objFile.Write strNewContents

REM fills in the holes where the 0 character lines were

wscript.sleep (15000)

REM waits 15 seconds to complete the operation

Report •

Related Solutions

Ask Question