Dmitrij Holkin
Dmitrij Holkin

Reputation: 2055

Batch file runfrom current folder

If i want run this script

@echo off
setlocal enabledelayedexpansion

set OUTPUT_FILE=results.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (servers.txt) do (
    set SERVER_ADDRESS=ADDRESS N/A
    for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
        if %%x==Pinging set SERVER_ADDRESS=%%y
        if %%x==Reply set SERVER_ADDRESS=%%z
        if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
    )
    echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE!
    echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
)

From folder

C:\Documents and Settings\Admin\Рабочий стол\

then cmd.exe is opening and inside i saw each line posting of code to cmd.exe and bat not work. or cmd.exe run and going black and script looping infinity times while cmd.exe is being closed.

If i put this script to c:\ and run it it work as requeried.

How to run bat file normaly from desktop?

Upvotes: 0

Views: 72

Answers (1)

JosefZ
JosefZ

Reputation: 30238

See this code snippet for analysing ping.exe output for IPv4 (saved and run below as 42046325_30852528.bat). Running it shows that the original script (saved and run below as 42046325old.bat) might return a totally false result e.g. for 192.168.1.12 IPv4 address Destination host unreachable.

Both script give the same results regardless if run

  • by double click from file explorer, or
  • by typing path to them from an open cmd window.

Note chcp command in cmd: I need to change active console Code Page to 1251 (Russian) or to 65001 (UTF-8) as my default system locale is 1250.

Output:

d:\bat> chcp 1251
Active code page: 1251

d:\bat> "D:\bat\SO\Рабочий стол\42046325_30852528.bat" b
       hostname OP IPv4_address    explanation
       -------- -- ------------    -----------
        foo.bar ## foo.bar         Ping request could not find host
     google.com == 216.58.201.110  Reply from 216.58.201.110: bytes=32 time=9ms TTL=55
  www.seznam.cz == 77.75.79.53     Reply from 77.75.79.53: bytes=32 time=9ms TTL=247
    192.168.1.1 == 192.168.1.1     Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
   192.168.1.12 ?= 192.168.1.12    Reply from 192.168.1.100: Destination host unreachable.
        bmw.com =? 160.46.244.131  Pinging bmw.com [160.46.244.131]: Request timed out.
 origin.bmw.com =? 160.46.244.131  Pinging origin.bmw.com [160.46.244.131]: Request timed out.
Press any key to continue . . .

d:\bat> "D:\bat\SO\Рабочий стол\42046325old.bat"
foo.bar [ADDRESS N/A] is DOWN
google.com [216.58.201.110] is UP
77.75.79.53 [77.75.79.53] is UP
192.168.1.1 [192.168.1.1] is UP
192.168.1.12 [192.168.1.100] is UP
bmw.com [bmw.com] is DOWN
160.46.244.131 [160.46.244.131] is DOWN
Press any key to continue . . .

d:\bat>

BTW, analysing ping.exe output for Internet Protocol version 6 (IPv6) would be completely different task.

Upvotes: 1

Related Questions