2

我正在尝试将整数文件夹名称与变量进行比较。文件夹名称都是整数值。例如,在文件夹“VersionedFolders”中存在子文件夹“95”、“96”,最多“100”。

我的脚本如下:

@echo off
setlocal ENABLEDELAYEDEXPANSION


SET %PATH_TO_VERSION_FOLDER=VersionedFolders
SET %CurrentVersion = 99

for /f %%a in ('dir /B "%PATH_TO_VERSION_FOLDER%"') do (        
    if %CurrentVersion% LEQ %%a (
        echo CurrentVersion !CurrentVersion! is less than or equal to %%a.      
        set CurrentVersion=%%a 
    ) else (
        echo CurrentVersion !CurrentVersion! is not less than or equal to %%a       
    )
)

输出如下:

CurrentVersion 99 is less than or equal to 100.
CurrentVersion 100 is not less than or equal to 95.
CurrentVersion 100 is not less than or equal to 96.
CurrentVersion 100 is not less than or equal to 97.
CurrentVersion 100 is not less than or equal to 98.
CurrentVersion 100 is less than or equal to 99.

最后一次迭代是自 100 > 99 以来存在问题的地方。

注意 dir /B "%PATH_TO_VERSION_FOLDER%" 的输出是

100
95
96
97
98
99

任何想法为什么“如果 100 LEQ 99”返回真实?

4

2 回答 2

3

问题是延迟扩展。

for ...%%a...解析 时,将当时的值代currentversion入代码中。

用于!currentversion!获取 RUN-TIME 值(调用延迟扩展,正如您所拥有的......)

(另外:分配一个值,使用SET VAR=VALUEnot SET %VAR=VALUE

于 2013-07-05T19:23:47.310 回答
1

这是你想要的吗?您在变量和变量名的前面/后面也有空格,这是需要注意的。

@echo off
setlocal ENABLEDELAYEDEXPANSION

SET PATH_TO_VERSION_FOLDER=VersionedFolders
SET CurrentVersion=0

for /f %%a in ('dir /B /ad "%PATH_TO_VERSION_FOLDER%"') do (        
    if !CurrentVersion! LEQ %%a (
        echo CurrentVersion !CurrentVersion! is less than or equal to %%a.
    ) else (
        echo CurrentVersion !CurrentVersion! is greater than %%a.
    )
        set CurrentVersion=%%a
)
pause
于 2013-07-06T05:19:36.457 回答