窗口批处理文件错误:(目前无法预料

时间:2019-01-23 05:58:49

标签: batch-file

这是我的run.bat文件:

set var1 = %1
IF %var1%=="Yes" (
    Echo deleting filename.txt
 ) ELSE ( 
    Echo The file was not found.
 )

我将其运行为:run.bat -h,并且出现此错误:

C:\Users\admin\Desktop\test>run.bat -h

C:\Users\admin\Desktop\test>set var1 = -h
( was unexpected at this time.

C:\Users\admin\Desktop\test>IF =="Yes" (

2 个答案:

答案 0 :(得分:1)

您在没有%1参数的情况下运行了它。

在命令行中运行:

run.bat something

而不仅仅是

run.bat

还要避免这种情况,您必须对%var1%使用引号,以使IF正常工作:

set "var1=%1"
IF "%var1%"=="Yes" (
    Echo deleting filename.txt
 ) ELSE ( 
    Echo The file was not found.
 )

然后您可以在不使用参数的情况下运行它,并且不会抱怨。

答案 1 :(得分:1)

根据我的评论,您需要将值传递给脚本进行分配,以便%1可以使用它。还要在变量集周围用双引号引起来,最好检查是否传递了值:

@echo off
set "var1=%1"
if not defined var1 echo you did not pass a value. usage "batch.cmd parameter" & pause & exit
if /I "%var1%"=="Yes" (
   echo deleting filename.txt
 ) else ( 
   echo The file was not found.
)