Windows批处理:比较时间和字符串文字

时间:2017-02-10 22:57:27

标签: windows batch-file

我编写了一个批处理脚本,如果目录中的文件名等于其预期的文件名,会运行.exe

例如,路径中的文件是" file1"和" file2",而process_one =" file1"和process_two =" file2"

结果是,即使条件为假,它总是会转到第一个if语句。

这里没有用的是if语句。 例如 file1 =" filenameA.txt&#34 ;; process_one =" filenmameB.txt"

它总是会在第一个if语句中执行命令,即使它是假的。

我是否正确比较了时间和字符串文字?如果没有,时间和字符串文字的正确比较是什么?

这是我到目前为止所做的。我在这里很新,所以谢谢你的所有见解。 :)

@echo off

For /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x

SET EnabledDelayedExpansion

SET folderPath=D:\InputFiles

SET month=%MyDate:~4,2%
SET day=%MyDate:~6,2%
SET year=%MyDate:~0,4%
SET dateToday=%year%%month%%day%
SET currentTime=%Time: =0%

SET filename1=filename1_%dateToday%.dat
SET filename2=filename2_%dateToday%.dat
SET filename3=filename3_%dateToday%.dat
SET filename4=filename4_%dateToday%.dat
SET filename5=filename5_%dateToday%.dat
SET filename6=filename6_%dateToday%.dat
::SET count=0

pushd "%folderPath%"
for %%F in (*.*) do (  
    ::set /a count+=1
    ::echo %%~nxF       
IF "%%~nxF"=="%filename1%" (
    IF %currentTime% GEQ 01:30:00.00 IF %currentTime% LEQ 23:00:00.00 ( start /b "" "C:\Program Files\Activity\activities.exe" -process acta )
    )               
IF "%%~nxF"=="%filename2%" (
    IF %currentTime% GEQ 02:00:00.00 IF %currentTime% LEQ 18:05:59.59 ( start /b "" "C:\Program Files\Activity\activities.exe" -process actb )
    )
IF "%%~nxF"=="%filename3%" ( 
    IF %currentTime% GEQ 03:00:00.00 IF %currentTime% LEQ 19:00:00.00 ( start /b "" "C:\Program Files\Activity\activities.exe" -process actc )
    )
IF "%%~nxF"=="%filename4%" ( 
    IF %currentTime% GEQ 04:25:00.00 IF %currentTime% LEQ 19:00:00.00 ( start /b "" "C:\Program Files\Activity\activities.exe" -process actd )
    )
IF "%%~nxF"=="%filename5%" (
    IF %currentTime% GEQ 05:00:00.00 IF %currentTime% LEQ 23:59:59.59 ( start /b "" "C:\Program Files\Activity\activities.exe" -process acte )
    )
IF "%%~nxF"=="%filename6%" ( 
    IF %currentTime% GEQ 06:00:00.00 IF %currentTime% LEQ 23:59:59.59 ( start /b "" "C:\Program Files\Activity\activities.exe" -process actf )
    )
)
popd

1 个答案:

答案 0 :(得分:0)


对代码的快速修复可能只是用以下内容替换你的底部两行:

If /I "%fileName%"=="%process_one%" Start "" /B "process.exe" -process "%process_one%"
If /I "%fileName%"=="%process_two%" Start "" /B "process.exe" -process "%process_two%"

我建议您删除pushd "%path%" &&& popd或至少将(%path%\*.*)更改为(*.*)

假设没有您决定省略的脚本部分中的错误,这是最佳猜测重写/替换您发布的代码:

If Not Exist "%_path%\" Exit/B 1

For %%F In (%_path%\*.*) Do (Set/A count+=1
    For %%G In ("%process_one%" "%process_two%") Do If /I "%%~nxF"==%%G (
        Start "" /B "process.exe" -process %%G))

Exit/B 0

重要说明 :您需要确保已经有一个名为%count%的整数变量,可能{{1 }} 的。但是,如果您未在脚本中的其他位置使用Set "count=0",则可以忽略该建议并从我上面提供的代码中删除%count%。您需要在未从Set/A count+=1发布到%path%的脚本的任何部分更改变量名称。 您已经声明已经有名为%_path%%process_one%的字符串变量设置