批处理文件从当前文件夹运行

时间:2017-02-04 22:24:58

标签: windows batch-file cmd

如果我想运行此脚本

@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%
)

从文件夹

  

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

然后cmd.exe打开并在里面我看到每行代码发布到cmd.exe并且bat不起作用。或cmd.exe在关闭cmd.exe时运行并变黑并且脚本循环无限次。

如果我把这个脚本放到c:\并运行它,那就像requeried一样。

如何从桌面运行bat文件?

1 个答案:

答案 0 :(得分:1)

请参阅this code snippet for analysing ping.exe output for IPv4(保存并在下面以42046325_30852528.bat运行)。运行它会显示原始脚本(保存并在42046325old.bat下面运行)可能会返回完全错误的结果,例如192.168.1.12 IPv4地址Destination host unreachable

无论是否运行

,两个脚本都会给出相同的结果
  • 通过双击文件浏览器中的
  • 从打开的cmd窗口输入路径。

注意cmd中的chcp command:我需要更改活动控制台Code Page to 1251 (Russian)或更改为65001(UTF-8),因为我的默认系统区域设置为1250

<强>输出

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>

顺便说一句,分析Internet Protocol version 6 (IPv6)ping.exe输出将是完全不同的任务。