使用变量的字符串比较始终为 false(批处理)

Comparison of strings using variables always false (in Batch)

在我当前的文件夹中,我有一个名为 backup(yyyy-mm-dd).7z
的文件 我需要保留那个 length 变量,即使它在这里是一个常量。

@echo off
setlocal EnableDelayedExpansion

set "a=.\backup"
set "b=yyyy-mm-dd"
set "length=19"

for %%f in (.\*) do (

    set "fullpath=%%f"
    set "trimpath=!fullpath:~0,%length%!"

    set trimpath
    echo trimpath=%trimpath%

    if %trimpath% == %a%(%b% echo this is equal
)

关于该代码我有 2 个问题:

  1. 为什么我在调用set trimpath时可以看到trimpath的值,而不是直接调用%trimpath%
  2. 为什么我的条件是错误的?我应该怎么做才能让它成为现实?

这是遵循@foxidrive 和@JosefZ 的建议后的解决方案!

@echo off
setlocal EnableDelayedExpansion

set "a=.\backup"
set "b=yyyy-mm-dd"
set "length=19"

for %%f in (.\*) do (

    set "fullpath=%%f"
    set "trimpath=!fullpath:~0,%length%!"

    echo trimpath=!trimpath!

    if "!trimpath!" == "%a%(%b%" echo this is equal
)

如果您需要测试解决方案,只需在当前文件夹中创建一个名为 backup(yyyy-mm-dd) 的文件。