Solved In need of help doing a FOR /F Do loop in a CMD Batch

psytae

New member
Local time
7:40 PM
Messages
5
I am in need of help doing a FOR /F Do loop in a CMD Batch script.

I am trying to write a batch script that will ping all IP addresses in a host file except for an IP address that starts with 127 and skip all commented lines in the hostfile or that start with a #.

my current code will ping the first word/string in every uncommented line, so I am halfway there. However I can't figure out how to compare the first 4 characters in the word/sting to 127. and have it skip it if starts with those characters.

Here is my working code. Any help will be appreciated.

Code:
@ECHO off
setlocal EnableExtensions EnableDelayedExpansion

DEL C:\temp\Pings.txt

ECHO. > C:\temp\Pings.txt

For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do ECHO PING -a "%%A" && ECHO. >> C:\temp\Pings.txt && ECHO. >> C:\temp\Pings.txt && ECHO PING -a "%%A" >> C:\temp\Pings.txt && PING -a "%%A" >> C:\temp\Pings.txt && ECHO ---------------------------------------------------------------- >> C:\temp\Pings.txt
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
OS
Windows 7 Ultimate x64
Do you mean 3 characters or 4?

/* first, cast the %% variable to a non %% so we can work with it */
SET var = %%A
/* parse relative bytes 0 thru 2 into var2 */
SET var2 = !var:~0,2!
/* compare 1st 3 bytes to number 127
IF !var2! == 127 (
insert more code here
)
 

My Computer My Computer

At a glance

Windows 7AMD Phenom II X2 (dual-core)4Gintegrated ATI HD 4200
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom
OS
Windows 7
CPU
AMD Phenom II X2 (dual-core)
Motherboard
GA-MA785GM-US2H
Memory
4G
Graphics Card(s)
integrated ATI HD 4200
Sound Card
integrated
Monitor(s) Displays
Samsung 24"
Screen Resolution
1920x1080
Hard Drives
1 SSD - Samsung 840 - 500 GB - OS and DATA partitions
1 SSD - Intel 320 - 120 GB (used for backups) - Misc/BACKUP
1 SATA HD - WD, 500 GB - BACKUP
PSU
Ultra X4 500W
Case
Ultra X-blaster
Keyboard
Microsoft Digital Media Pro
Mouse
Logitech WIRED!
Internet Speed
15 Mbps FIOS
So you are suggesting something like this?

Code:
@ECHO off
setlocal EnableExtensions EnableDelayedExpansion
 
DEL C:\temp\Pings.txt
 
ECHO. > C:\temp\Pings.txt
 
For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do (
SET var = %%A
SET var2 = !var:~0,2!
IF !var2! == 127 (
ECHO PING -a "%%A" && ECHO. >> C:\temp\Pings.txt && ECHO. >> C:\temp\Pings.txt && ECHO PING -a "%%A" >> C:\temp\Pings.txt && PING -a "%%A" >> C:\temp\Pings.txt && ECHO ---------------------------------------------------------------- >> C:\temp\Pings.txt))
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
OS
Windows 7 Ultimate x64
This does not seem to work when I run this cmd file at the cmd prompt

Code:
@ECHO off
setlocal EnableExtensions EnableDelayedExpansion
DEL C:\temp\Pings.txt
ECHO. > C:\temp\Pings.txt
For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do (
  Set var=%%A
  echo %%A
  echo %var%
  )

I get this output

Code:
I get this output
C:\temp>Test.cmd
127.0.0.1
ECHO is off.
192.168.0.1
ECHO is off.
192.168.0.2
ECHO is off.
C:\temp>

Best I can tell it is not setting the variable correctly because when it is suppsoed to be echoing the value of %var% it echos empty.
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
OS
Windows 7 Ultimate x64
I assume the 127. and 192. output echo lines above are from the echo %%A statement. So, %%A is getting loaded/set.

What if you change:

echo %var%

to

echo !var!
 

My Computer My Computer

At a glance

Windows 7AMD Phenom II X2 (dual-core)4Gintegrated ATI HD 4200
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom
OS
Windows 7
CPU
AMD Phenom II X2 (dual-core)
Motherboard
GA-MA785GM-US2H
Memory
4G
Graphics Card(s)
integrated ATI HD 4200
Sound Card
integrated
Monitor(s) Displays
Samsung 24"
Screen Resolution
1920x1080
Hard Drives
1 SSD - Samsung 840 - 500 GB - OS and DATA partitions
1 SSD - Intel 320 - 120 GB (used for backups) - Misc/BACKUP
1 SATA HD - WD, 500 GB - BACKUP
PSU
Ultra X4 500W
Case
Ultra X-blaster
Keyboard
Microsoft Digital Media Pro
Mouse
Logitech WIRED!
Internet Speed
15 Mbps FIOS
that seems to work

Using this code

Code:
@ECHO off
setlocal EnableExtensions EnableDelayedExpansion
DEL C:\temp\Pings.txt
ECHO. > C:\temp\Pings.txt
For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do (
  Set var=%%A
  echo %%A
  echo !var!
  )

I get this result

Code:
C:\temp>temp.cmd
127.0.0.1
127.0.0.1
192.168.0.1
192.168.0.1
192.168.0.2
192.168.0.2
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
OS
Windows 7 Ultimate x64
Final code that does what I want it do do turns out like

Code:
@ECHO off
setlocal EnableExtensions EnableDelayedExpansion
DEL C:\temp\Pings.txt
ECHO. > C:\temp\Pings.txt
For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do (
  Set var=%%A
  set var2=!var:~0,3!
  IF NOT !var2! == 127 (ECHO PING -a "%%A" && ECHO. >> C:\temp\Pings.txt && ECHO. >> C:\temp\Pings.txt && ECHO PING -a "%%A" >> C:\temp\Pings.txt && PING -a "%%A" >> C:\temp\Pings.txt && ECHO ---------------------------------------------------------------- >> C:\temp\Pings.txt)
  )
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
OS
Windows 7 Ultimate x64
Back
Top