2017-02-10 43 views
0

我编写了一个批处理脚本,将在指定时间范围上运行.exe,如果目录中的文件名等于其预期的文件名。Windows批处理:比较时间和字符串文字

例如,从路径的文件是“文件1”和“文件2”,而process_one =“文件1”和process_two =“文件2”

结果是它总会向第一if语句甚至如果条件是错误的。

这里不工作的是if语句。 例如 file1 =“filenameA.txt”; 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 
+0

什么不适用于您的脚本? – npocmaka

+1

')&popd'这意味着'popd'将在每次迭代中执行。为什么不放在单独的行上,而是附加到for循环? – npocmaka

+2

用你自己的PATH变量覆盖PATH变量通常不是一个好主意。 – Squashman

回答

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%集的整数变量,可能是Set "count=0"。但是,如果您在脚本的其他地方不使用%count%,则可以忽略该建议并从我上面提供的代码中删除Set/A count+=1。您需要在脚本的任何部分中更改变量名称,这些部分未从%path%发布到%_path%您已经声明已经有名为%process_one%%process_two%的字符串变量集合

+0

我看到了,我试着做了建议的解决方案,但问题是,它总是在第一个if语句中执行命令......它总是在第一个if语句中启动该过程。我已经比if语句通过了,每个if命令都包含执行特定.exe文件的进程 – newbie

+0

您需要发布整个脚本,以便我们可以调试其余的代码。 – Compo

+0

请参阅编辑的文章。非常感谢。 – newbie