批处理(Windows)将变量设置为第一个参数不起作用 [英] Batch (Windows) set variable to first argument doesn't work

查看:171
本文介绍了批处理(Windows)将变量设置为第一个参数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理mybatch,我尝试将第一个用户参数存储在名为FILE的变量中

I have a batchscript mybatch in which I try to store the first user argument in a variable called FILE

set FILE = %1 
if defined FILE (
  echo defined
  echo do something with %1
  ) else (
   echo not defined %1 
  )

如果我通过mybatch test1执行我的批处理,则总是得到未定义的test1。为什么未定义变量FILE?

If I execute my batch via mybatch test1 I get always not defined test1. Why is variable FILE not defined?

推荐答案

变量分配中有多余的空格,因此定义了带空格的变量在名称中,其值始终以空格开头。您的IF语句正在检查是否存在没有空格的变量。

You have unwanted spaces in your variable assignment, so you have defined a variable with a space in the name that always has a value beginning with a space. Your IF statement is checking if a variable without a space exists.

请参见声明并在Windows批处理文件(.BAT)中使用变量

我建议您的第一行应为:

I recommend your first line should be:

set "FILE=%~1"

这篇关于批处理(Windows)将变量设置为第一个参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆