【发布时间】:2017-12-17 17:28:40
【问题描述】:
我得到一个逗号分隔的列表,其中包含来自数据库的字段值。这个列表有时是空白的。我需要检查列表是否为空白,然后做点什么。如果列表不为空,请执行其他操作。
问题是我在检查列表是否为空白时遇到错误。这是代码。请注意,我在代码开头模拟了值来自数据库。
这是我之前尝试过的:如果VendorStores 的列表为空(空白),请执行一项操作。否则采取其他行动。当VendorStores 有多个值(逗号分隔)并且与空白进行比较时,我遇到了错误。所以现在我正在尝试计算这些值并使用该计数来做出决定。但我遇到了同样的问题。当VendorStores 为空时,for 循环仍然认为它有 2 个值 - 1) ",=" 2) ""。不知道为什么会这样。
:: Intention of the script is as follows:
:: If VendorStores list is blank, take one action
:: If VendorStores list has values, take another action
@echo off
:: Set values for VendorStores
:: set vendorstores=123,234,345
:: Set VendorStores to blank
set vendorstores=
echo list = "%vendorstores%"
:: Remove any blank spaces from VendorStores
set vendorstores=%vendorstores: =%
set /a c=0
echo c=%c%
SETLOCAL EnableDelayedExpansion
for %%a in ("%vendorstores:,=" "%") do (
if [%%a] NEQ [] (
set /a c=c + 1
echo VendorStore is %%a
)
echo c=!c!
)
echo c=!c!
if [!c!] EQU [0] (
echo c is equal to Zero
) else (
echo c is greater than Zero
)
echo c=!c!
endlocal
【问题讨论】:
-
是php还是bush,请加标签?
-
是批处理文件。
-
批处理文件 - 不是编程语言或脚本。如果您指定标签,则根据专家领域过滤您的问题会更有用。
-
添加了批处理文件标签。对此感到抱歉。
标签: arrays batch-file string-comparison