Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: In need of help doing a FOR /F Do loop in a CMD Batch

14 Mar 2012   #1
psytae

Windows 7 Ultimate x64
 
 
In need of help doing a FOR /F Do loop in a CMD Batch

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 System SpecsSystem Spec
.
14 Mar 2012   #2
JimLewandowski

 
 

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 System SpecsSystem Spec
14 Mar 2012   #3
psytae

Windows 7 Ultimate x64
 
 

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 System SpecsSystem Spec
.

14 Mar 2012   #4
psytae

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 System SpecsSystem Spec
14 Mar 2012   #5
JimLewandowski

 
 

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 System SpecsSystem Spec
15 Mar 2012   #6
psytae

Windows 7 Ultimate x64
 
 

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 System SpecsSystem Spec
15 Mar 2012   #7
psytae

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 System SpecsSystem Spec
Reply

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




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Batch file help. For loop not working...
What im trying to accomplish: -I want to REG QUERY a registry entry that will output the value of a (*SpeedDuplex) string value -I then want to store that value in a variable The command im using and the output: REG QUERY...
General Discussion
DBF to CSV in batch: how to do?
Dear Community, I need to convert every minute (1 minute) a file generate in format DBF into the CSV format. I looked around some script, I found one script in Python but unfortunately it doesn't work. Can you help me? Cheers,
Software
How to run a batch file at logoff & some batch scripting help please
Hiya, I'd just like to run a simple batch file at logoff to backup some settings. I plan to use Robocopy, which is what i use to more thorough backups. Two questions: 1. How make it run by clicking Start > Shutdown? (i realise i could make a shutdown batch file and run it instead, but i'd...
Backup and Restore
BSOD Loop, nothing works and is in a constant loop
Hi everyone, I have a Sony VAIO vpcEB4L1E.chk Laptop, with a 500 GB HDD , with a Intel core i3-380m processor / 2.53 GHz,with 4GB RAM, running on Windows 7 home premium 32 bit. Until recently the laptop has been working fine, but all of a sudden it's stopped working. In its simpliest form, the...
BSOD Help and Support
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion
Help me with Batch script!
Hi, Can somebody assist me with a .bat file I am trying to create? This is what I have now: start /d "C:\Users\Me\Desktop\ghost" ghost.exe start /d "C:\Users\Me\Desktop" Poweroff.exe C:\Windows\System32\powercfg.exe /setactive e428f8a5-615e-488f-b7f7-1ed39450e522The first 2 lines start...
Software


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 09:03.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App