【问题标题】:Error when checking if a comma separated list is empty (blank)检查逗号分隔列表是否为空(空白)时出错
【发布时间】: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


【解决方案1】:

这是另一个选择:

@Echo Off
Set "VendorStores="
For /F "EOL=, Delims=, " %%A In ("%~1") Do Set "VendorStores=%%A"
If Not Defined VendorStores (Echo VendorStores is blank
) Else Echo VendorStores has one or more values

【讨论】:

    【解决方案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: =%
    
    ::::If there is any value in StoreCSV, then remove any comma delimiters from it as the "if" statement does not like commas
    if defined vendorstores (
    set "vendorstores=%vendorstores:,=%"
    )
    
    SETLOCAL EnableDelayedExpansion
    
    if [%vendorstores%] EQU [] (
        echo VendorStores is blank
    ) else (
        echo VendorStores has one or more values
    )
    
    endlocal
    

    【讨论】:

    • 不建议使用if [%vendorstores%] EQU [][] 没有特殊含义。它们只是两个字符,在比较运算符EQU 的左右两个字符串时也需要比较。更好的是使用if "%vendorstores%" EQU ""。双引号也包含在 IF 命令完成的字符串比较中。但在双引号参数中,&|<> 等命令行运算符被解释为文字字符而不是运算符。这通常会产生重要影响。
    【解决方案3】:

    错误的结果来自 FOR 命令行:

    for %%a in ("%vendorstores:,=" "%") do (
    

    在此命令行上方插入一行echo on,并在FOR的右括号后插入一行@echo off,很容易看出它失败的原因。

    正确的命令行是:

    for %%a in (%vendorstores%) do (
    

    但是在环境变量vendorstores根本没有定义的情况下使用这个FOR循环并不好。

    这是一个名为 Test.bat 的批处理文件的简化示例:

    @echo off
    set "VendorStores=%~1"
    if not defined VendorStores  goto EmptyList
    set "VendorStores=%VendorStores: =%"
    if not defined VendorStores  goto EmptyList
    if "%VendorStores:,=%" == "" goto EmptyList
    echo List is: %VendorStores%
    goto :EOF
    
    :EmptyList
    echo The list is empty.
    

    这个批处理文件可以在命令提示符窗口中执行,如下所示:

    Test.bat
    Test.bat ,,,,
    Test.bat ", , , ,"
    Test.bat " 123, 234, 345 "
    Test.bat "376,983,305,253"
    

    这 5 个批处理文件执行的输出是预期输出的 5 倍。

    【讨论】:

    • 莫菲,非常感谢!我缺少的是“如果没有定义 VendorStores”。从分隔字符串中删除空格后,我在删除逗号之前添加了此语句。所以这就是我所做的。
    猜你喜欢
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多