批处理文件在命令提示符下运行正常但双击时出错

时间:2015-07-23 21:12:56

标签: windows batch-file cmd

我有一个批处理文件,它接受商店编号并将它们设置为一个数组,然后遍历数组。当我双击我的批处理文件时,它会给出错误:

256 was unexpected at this time.

但是当我从控制台/命令提示符运行它时运行正常。

这是我的代码。

@echo off

setlocal enabledelayedexpansion

set index=0

:getstore
set /a index=index + 1
set /P store[%index%]=Enter SLC store number: 

:ask
set /P answer=Do you want to enter another store number (Y/N): 


if /i "%answer%" == "n" (
    set length=%index%
    goto next
)

if /i "%answer%" == "y" (
    goto getstore 
) else goto ask

:next
for /L %%i in (1,1,%length%) do (
    if %store% LSS 256 (
        for /L %%k in (1,1,5) do ping 192.168.!store[%%i]!.%%k -n 1 |find "TTL"
    )
    if %store% GTR 255 (    
        set /a store=%store% - 255
        for /L %%k in (1,1,5) do ping 10.0.!store[%%i]!.%%k -n 1 |find "TTL"
    )
)

2 个答案:

答案 0 :(得分:1)

当您双击批处理文件时,将执行一个新的cmd实例,一个尚未初始化store的实例,所以该行

if %store% LSS 256 (

失败,因为%store%为空。

答案 1 :(得分:0)

感谢MC ND!

你的评论让我思考。

当我在if语句中调用store时,我需要使用!!还有索引。

这是我编辑过的(现在正在工作的)代码:

:next
for /L %%i in (1,1,%length%) do (
    for /f "tokens=* delims=0" %%j in ("%store[%%i]%") do set store[%%i]=%%j
    if !store[%%i]! LSS 256 (
        for /L %%k in (1,1,5) do ping 192.168.!store[%%i]!.%%k -n 1 |find "TTL"
    )
    if !store[%%i]! GTR 255 (   
        set /a store=%store% - 255
        for /L %%k in (1,1,5) do ping 10.0.!store[%%i]!.%%k -n 1 |find "TTL"
    )
)