1

它应该很简单,但我无法让它工作!也许有人可以帮助...

我只需要一个程序来检查'07503.csv'文件是否存在,而不是'07504.csv','07505.csv'......直到'07552.csv'。在这里我想结束程序(它被简化了,所以你可以理解我的问题)。

这有效:

SET angle=075
SET velocity=03
SET finalfilename=.csv

SET endofsequency=07552

SETLOCAL EnableDelayedExpansion 

:loop

    SET currentsequency=%angle%%velocity%
    SET fullfilename=%currentsequency%%finalfilename%

    :CHECK_FILES
    IF EXIST %fullfilename% (
        ECHO FILE FOUND: %fullfilename% 
        GOTO :GO_TROUGHT 
    ) ELSE (
        ECHO FILE NOT FOUND: %fullfilename% 
        GOTO :WRITE_LOG 
    )

    :WRITE_LOG
    ECHO %time% missing: %fullfilename% 
    ECHO %time% missing: %fullfilename% >> FilesMissed.txt

    :GO_TROUGHT
    FOR /F "tokens=* delims=0" %%A IN ("%velocity%") DO SET velocity=%%A
    SET /A velocity+=1
    SET velocity=000%velocity%
    SET velocity=%velocity:~-2%

    PAUSE

IF %currentsequency% NEQ %endofsequency% GOTO loop

:END

PAUSE

但是使用 IF-ElSE 语句不会:

SET angle=075
SET velocity=03
SET finalfilename=.csv

SETLOCAL EnableDelayedExpansion 

:loop

    SET currentsequency=%angle%%velocity%
    SET fullfilename=%currentsequency%%finalfilename%

    :CHECK_FILES
    IF EXIST %fullfilename% (
        ECHO FILE FOUND: %fullfilename% 
        GOTO :GO_TROUGHT 
    ) ELSE (
        ECHO FILE NOT FOUND: %fullfilename% 
        GOTO :WRITE_LOG 
    )

    :WRITE_LOG
    ECHO %time% missing: %fullfilename% 
    ECHO %time% missing: %fullfilename% >> FilesMissed.txt

    :GO_TROUGHT
    IF "%velocity%" NEQ "52" (
        FOR /F "tokens=* delims=0" %%A IN ("%velocity%") DO SET velocity=%%A
        SET /A velocity+=1
        SET velocity=000%velocity%
        SET velocity=%velocity:~-2%
    ) ELSE (
        GOTO :END
    )

    PAUSE

GOTO :loop

:END

PAUSE

我的问题是为什么?发生了什么?我不能在 IF-ELSE 中使用 FOR 吗?想...

问候,

4

3 回答 3

1

@Aacini 的回答一样,下面使用FOR /L循环,但方法略有不同:

@ECHO OFF
SET angle=075
SET startvelocity=3
SET endvelocity=52
SET ext=.csv

SET /A startnum=1%angle%*100+startvelocity
SET /A endnum=1%angle%*100+endvelocity
FOR /L %%i IN (%startnum%,1,%endnum%) DO (
  SET "filename=%%i%ext%"
  SETLOCAL EnableDelayedExpansion
  IF EXIST !filename:~1! (
    ECHO FILE FOUND: !filename:~1!
  ) ELSE (
    ECHO FILE NOT FOUND: !filename:~1!
    SET "logline=!time! missing: !filename:~1!"
    ECHO !logline!
    ECHO !logline!>> FilesMissed.txt
  )
  ENDLOCAL
)

也就是说,循环正在迭代像这样的值

107503
107504
107505
...

添加前导10保留75. (前导0s 在算术运算中丢失。)

在循环体中,循环变量的值用于构造文件名:

107503.csv
107504.csv
...

前导1稍后被删除(~1在扩展名称时使用)。

此外,如果您写入FilesMissed.txt文件的内容总是要在屏幕上复制,我引入了一个变量来显示如何确保内容完全复制允许您仅更改行的格式一次。(如果您打算另外将同一行写入其他文件,则可能特别有用。)

于 2013-06-09T12:55:18.930 回答
1

没有仔细阅读 - 会使用这个帮助吗?

IF "%velocity%" EQU "52" goto :END

仔细观察 - 这是延迟扩展的问题。您需要启用它,然后使用 !variable! 在括号内而不是 %variable% 。

编辑:尝试延迟扩展。第一行已更改,最后两SET velocity=行已更改。

setlocal enabledelayedexpansion
SET angle=075
SET velocity=03
SET finalfilename=.csv

SETLOCAL EnableDelayedExpansion 

:loop

    SET currentsequency=%angle%%velocity%
    SET fullfilename=%currentsequency%%finalfilename%

    :CHECK_FILES
    IF EXIST %fullfilename% (
        ECHO FILE FOUND: %fullfilename% 
        GOTO :GO_TROUGHT 
    ) ELSE (
        ECHO FILE NOT FOUND: %fullfilename% 
        GOTO :WRITE_LOG 
    )

    :WRITE_LOG
    ECHO %time% missing: %fullfilename% 
    ECHO %time% missing: %fullfilename% >> FilesMissed.txt

    :GO_TROUGHT
    IF "%velocity%" NEQ "52" (
        FOR /F "tokens=* delims=0" %%A IN ("%velocity%") DO SET velocity=%%A
        SET /A velocity+=1
        SET velocity=000!velocity!
        SET velocity=!velocity:~-2!
    ) ELSE (
        GOTO :END
    )

    PAUSE

GOTO :loop

:END

PAUSE
于 2013-06-07T15:45:02.860 回答
1

这就是我会这样做的方式:

@ECHO OFF
SETLOCAL EnableDelayedExpansion 

SET angle=075
SET "velocity=103"               // Will use just 2 last digits of velocity
SET finalfilename=.csv

SET "endVelocity=152"            // The same here...

SET /A numberOfValues=endVelocity - velocity + 1

for /L %%i in (1,1,%numberOfValues%) do (

    SET fullfilename=%angle%!velocity:~-2!%finalfilename%

    rem CHECK_FILES
    IF EXIST !fullfilename! (
        ECHO FILE FOUND: !fullfilename! 
    ) ELSE (
        ECHO FILE NOT FOUND: !fullfilename! 
        rem WRITE_LOG
        ECHO !time! missing: !fullfilename! 
        ECHO !time! missing: !fullfilename! >> FilesMissed.txt
    )

    SET /A velocity+=1

    PAUSE
)

PAUSE
于 2013-06-08T04:12:47.840 回答