[英]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.