batch-file - Check if dynamic variable is empty - Batch
问题描述
I want to check, if a dynamic variable is empty. I have the variables f1-f9 and now I want to check like this:
IF f%ERRORLEVEL%=" " //do something
But this doesnt work. I also tried:
IF !f%ERRORLEVEL%!
But this doesn't work either.
EDIT:
The variables f1-f9
are created like this:
set "f1= "
set "f2= "
...
%ERRORLEVEL%
contains a number between 1-9 (comes from a choice command)
解决方案
You are trying to match the value of f1 which is with
" "
So simply fix it by adding souble quotes around %f1% see example:
IF %ERRORLEVEL%==1 (
IF "%f1%"==" " (
set "f1=%turn%"
) ELSE (
goto draw_screen
)
But as per you question to simply your script, you firstly need delayedexpansion
and then some straighforward if testing:
@echo off
setlocal enabledelayedexpansion
set "f1= "
set "f2= "
set "f3= "
set "errorlevel=2"
if "!f%errorlevel%!"==" " (
set "f%errorlevel%=%turn%"
) else (
goto draw_screen
)
I use f1-3 only for simplifying the answer and obvisouly mimicing errorlevel
as a test. It is a single if statement which will match any errorlevel, you can test it by changing errorlevel value above.
推荐阅读
- linkedin - 如何通过 API 访问 LinkedIn 事件?
- excel - 为整行着色不适用于 VBA
- python - 如何从 Python regexp 中的可选组中获取数据?
- ios - 如何以编程方式设置过渡样式并连接到情节提要 UIViewController
- javascript - 匹配日期时无法理解 Javascript 中的正则表达式
- docker - 无法通过 ssh 重启 apache
- c - C 中预处理器指令语句的值
- amazon-web-services - 可以快速启动具有 200GB AMI 的 AWS Spot 实例吗?
- python - 如何在 Matplotlib 中更改刻度线的权重和颜色条的标签?
- azure - 使用自托管集成运行时将 SSIS 项目部署到 Azure 数据工厂时出错