我有一个批处理用于映射网络驱动器。 我决定添加到脚本设置图标变量。如果变量设置为“y”,那么稍后在脚本中我想添加基于变量的快捷方式。 目前,在映射驱动器后,我有这段代码来检查驱动器是否可用,然后设置变量 UserIco=y 或 n 以便稍后在脚本中使用。
rem This line is used to check if the drive letter works. And this line of code does work.
cd U: && (Echo Mapped Drive is accissble) && set UserIco=y || (net use U: /d) && set UserIco=n
后来在if语句中发现变量UserIco不起作用。 我加入了 echo,并将声明精简为这个基本格式。
Echo %UserIco%
IF "%UserIco%"=="y" (
Echo UserIco is %UserIco%
pause
) Else (
Echo Went to Else
pause
)
变量 %UserIco% 回显为“y”,但 if 语句转到 else。
set UserIco=y || (net use U: /d)
这会将
UserIco
设置为 y
,而不是 y
。
语法
SET "var=value"
(其中值可能为空;在这种情况下 var
变为 undefined)用于确保源行上的任何杂散尾随空格不包含在分配的值中。
如果您在
Echo UserIco is +%UserIco%+
例程中添加 else
以在例程失败时显示 userico
的 实际设置,这可能在您的代码中很明显。请注意主题字符串周围的
+
…+
“栅栏”,这使得更容易看到不可见的字符,例如 Space