简体   繁体   English

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

[英]Batch file runs fine in command prompt but gives error when double-clicked

I have a batch file that accepts store numbers and sets them to an array and then iterates through the array. 我有一个批处理文件,它接受商店编号并将其设置为数组,然后遍历该数组。 When I double click my batch file it gives the error: 当我双击批处理文件时,出现错误:

256 was unexpected at this time.

But when I run it from the console/command prompt it runs fine. 但是,当我从控制台/命令提示符运行它时,它运行良好。

Here is my code. 这是我的代码。

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

When you double click a batch file, a new cmd instance will be executed, one instance where store has not been initialized, so the line 双击批处理文件时,将执行一个新的cmd实例,其中一个实例尚未初始化store ,因此该行

if %store% LSS 256 (

fails as %store% is empty. 由于%store%为空而失败。

Thanks MC ND! 谢谢MC ND!

Your comment got me thinking. 您的评论让我开始思考。

When I was calling store in the if statement I needed to use the !! 当我在if语句中调用store时,我需要使用!! and also the index. 还有索引

This is my edited (now working) code: 这是我编辑过的代码(现在可以正常使用):

: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"
    )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 双击时如何使Windows批处理文件暂停? - How to make windows batch file pause when double-clicked? 计划任务调用的批处理文件在计划时抛出错误,双击运行良好 - Batch file called by scheduled task throws error when scheduled, runs fine when double clicked 双击时暂停批处理文件,但从控制台窗口运行时不暂停? - Pausing a batch file when double-clicked but not when run from a console window? 批处理文件在双击时不会运行,但可以在命令提示符下完美运行 - batch file does not run when double click on it, but runs perfectly in command prompt 执行双击的Java jar文件时的输出流 - Output stream while executing a double-clicked java jar file 制作一个在 WinPython 命令提示符中运行 commando 的批处理文件 - Make an batch file that runs commando in WinPython Command Prompt 双击时批处理文件闪烁并关闭 - Batch file flashes and closes when double Clicked 批处理文件引发错误,但是当每个命令在命令行中单独运行时,它运行正常 - Batch file throws error but when each command is run individually in command line it works fine 是否可以构建一个双击时不显示控制台窗口的控制台应用程序? - Is it possible to build a Console app that does not display a console Window when double-clicked? 在 Window OS 中双击时如何在空闲 shell 中运行脚本? - How to run a script in Idle shell when double-clicked in Window OS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM