If code does not execute because of first line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
martin11ph
Posts: 5
Joined: 29 Jan 2013 07:09

If code does not execute because of first line

#1 Post by martin11ph » 29 Jan 2013 07:16

Hi guys,

I am following the code here at http://serverfault.com/questions/203366 ... t-question because I need a batch script that checks a list of pcs in a txt file if they are on. If they are on, pslogged on is called.

Start.cmd

Code: Select all

FOR /F %%x in (c:\temp\pclist.txt) do call ping_psloggedon.cmd %%x


ping_psloggedon.cmd

Code: Select all

ping -n 1 -i 200 -w 130 %1
if errorlevel 1 goto exit
c:\temp\pstools\psloggedon.exe \\%1
:exit


start.cmd works fine because I see that it calls ping_psloggedon.cmd

When ping_psloggedon.cmd is running, it just keeps pinging the same PC causing it to just run the for loop once. Any ideas? :cry:

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: If code does not execute because of first line

#2 Post by shirulkar » 29 Jan 2013 07:27

Following will work

Code: Select all

@echo off
FOR /F "usebackq tokens=* delims=" %%x in ("pclist.txt") do call :ping_psloggedon.cmd %%x

EXIT /b

:ping_psloggedon.cmd
set str1=%~1
echo %str1%
ping -n 1 -i 200 -w 130 %str1%
if errorlevel 1 goto exit
c:\temp\pstools\psloggedon.exe %str1%

exit /b

:exit

martin11ph
Posts: 5
Joined: 29 Jan 2013 07:09

Re: If code does not execute because of first line

#3 Post by martin11ph » 29 Jan 2013 07:37

Hi thanks for the quick response. :D

I'm getting
The system cannot find the batch label specified - ping_psloggedon.cmd

I tried changing do call to do goto but it doesn't seem to jump to the label.

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: If code does not execute because of first line

#4 Post by shirulkar » 29 Jan 2013 07:40

I tried my code it is working fine.

martin11ph
Posts: 5
Joined: 29 Jan 2013 07:09

Re: If code does not execute because of first line

#5 Post by martin11ph » 29 Jan 2013 07:58

I see. I tried removing the echo to see where it stops. It stops at the ping command and then the error appears. I tried removing
if errorlevel 1 goto exit
but it still does not execute the next line.

Last lines in cmd prompt would be

Code: Select all

C:\>ping -n 1 -i 200 -w pcnamehere
The system cannot find the batch label specified - ping_psloggedon.cmd
C:\>EXIT /b

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: If code does not execute because of first line

#6 Post by shirulkar » 29 Jan 2013 08:17

I edited my code it is working for me. I dont know why it is not working for you.

Code: Select all

@echo off
FOR /F "usebackq tokens=* delims=" %%x in ("pclist.txt") do call :ping %%x

EXIT /b

:ping
set str1=%~1
echo %str1%
ping -n 1 -i 200 -w 130 %str1%
if errorlevel 1 ( goto exit )
C:\Windows\System32\ping.exe %str1%

exit /b

:exit
echo it is working

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

Re: If code does not execute because of first line

#7 Post by Squashman » 29 Jan 2013 08:35

I am so not a big fan of using predefined commands as labels or batch file names. Too me it is not a good coding practice.

martin11ph
Posts: 5
Joined: 29 Jan 2013 07:09

Re: If code does not execute because of first line

#8 Post by martin11ph » 29 Jan 2013 08:36

Hi Squashman,

Even with a different label it still does not work though.

Hi shirulkar,

I tried it on a Windows 7 machine and it runs fine. However, I need this to run on an XP machine. Do I need different codes?

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: If code does not execute because of first line

#9 Post by shirulkar » 29 Jan 2013 09:08

I dont think so. It should work for xp also. Just check the location of ping.exe in your system.
and try one change .
change
ping -n 1 -i 200 -w 130 %str1%

to

ping %str1% -n 1 -i 200 -w 130

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

Re: If code does not execute because of first line

#10 Post by foxidrive » 29 Jan 2013 09:11

@OP: Don't call your batch file 'ping.bat or ping.cmd'

I think you'll find that there is one on the XP machine.

martin11ph
Posts: 5
Joined: 29 Jan 2013 07:09

Re: If code does not execute because of first line

#11 Post by martin11ph » 29 Jan 2013 09:16

Squashman wrote:I am so not a big fan of using predefined commands as labels or batch file names. Too me it is not a good coding practice.


So that is what you meant. I changed the :ping label on shirulkar's suggested code. I didn't recall the original file I had was a ping_psloggedon.cmd :)

foxidrive wrote:@OP: Don't call your batch file 'ping.bat or ping.cmd'

I think you'll find that there is one on the XP machine.


Going back to the original code, renaming it did work. Thanks! :)


shirulkar wrote:I dont think so. It should work for xp also. Just check the location of ping.exe in your system.
and try one change .
change
ping -n 1 -i 200 -w 130 %str1%

to

ping %str1% -n 1 -i 200 -w 130


Thanks for the effort too shirulkar. :)

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

Re: If code does not execute because of first line

#12 Post by foxidrive » 29 Jan 2013 09:18

shirulkar wrote:Following will work

FOR /F "usebackq tokens=* delims=" %%x in ("pclist.txt") do call :ping_psloggedon.cmd %%x



Just a tip: use either tokens=* or delims=" but avoid using them both.

Another point to note is that tokens=* will strip leading whitespace so always use delims=" if you expect to use the exact input string.

I always use delims=" as it will give predictable results and I use other variations and tokens when it is appropriate.

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: If code does not execute because of first line

#13 Post by shirulkar » 29 Jan 2013 09:24

Thank you very much Foxidrive.

Post Reply