我有一个包含带时间戳的文本行的文件。我需要搜索在特定时间开始并在特定时间结束的行。我已经将所需的时间存储在变量中。我相信,我需要能够将线路和时间分解为标记,并搜索 geq“开始时间”和 leq“停止时间”的标记。我知道这需要一个 FOR 循环,但我不确定如何分解它。(另外,我发现 8 和 9 在用于输入时失败。我正在处理这个问题,我认为比较之前的 hr_start+100 和 hr_stop+100 以及比较之后的 -100 等)对此的任何改进表示赞赏。我的目标是一个完全没有错误的界面。
:time_begin
cls
echo.
echo (Use 24-hour clock)
echo.
:hr_start
set /a hr_start=0
set /p hr_start=What hour to start?
if "%hr_start%"=="b" goto day
if %hr_start% geq 0 (
if %hr_start% leq 23 (
if %hr_start% lss 10 set hr_start=0%hr_start%) else (
echo Invalid hour
goto hr_start))
:min_start
set /a min_start=0
set /p min_start=What minute to start?
if "%min_start%"=="b" goto hr_start
if %min_start% geq 0 (
if %min_start% leq 59 (
if %min_start% lss 10 set min_start=0%min_start%) else (
echo Invalid minute
goto min_start))
:sec_start
set /a sec_start=0
set /p sec_start=What second to start?
if "%sec_start%"=="b" goto min_start
if %sec_start% geq 0 (
if %sec_start% leq 59 (
if %sec_start% lss 10 set sec_start=0%sec_start%) else (
echo Invalid second
goto sec_start))
echo.
echo.
:hr_stop
set /a hr_stop=23
set /p hr_stop=What hour to stop?
if "%hr_stop%"=="b" goto sec_start
if %hr_stop% geq 0 (
if %hr_stop% leq 23 (
if %hr_stop% lss 10 set hr_stop=0%hr_stop%) else (
echo Invalid hour
goto hr_stop))
:min_stop
set min_stop=59
set /p min_stop=What minute to stop?
if "%min_stop%"=="b" goto hr_stop
if %min_stop% geq 0 (
if %min_stop% leq 59 (
if %min_stop% lss 10 set min_stop=0%min_stop%) else (
echo Invalid minute
goto min_stop))
:sec_stop
set /a sec_stop=59
set /p sec_stop=What second to stop?
if "%sec_stop%"=="b" goto min_stop
if %sec_stop% geq 0 (
if %sec_stop% leq 59 (
if %sec_stop% lss 10 set sec_stop=0%sec_stop%) else (
echo Invalid second
goto sec_stop))
echo.
set start_disp=%hr_start%:%min_start%:%sec_start%
set stop_disp=%hr_stop%:%min_stop%:%sec_stop%
echo Start time is %start_disp%.
echo Stop time is %stop_disp%.
echo.
:compare
set start_hr=%hr_start%*10000
set /a start_min=%min_start%*100
set /a stop_hr=%hr_stop%*10000
set /a stop_min=%min_stop%*100
set /a start_time=%start_hr% + %start_min% + %sec_start%
set /a stop_time=%stop_hr% + %stop_min% + %sec_stop%
if %stop_time% leq %start_time% (
echo End time must be at least one second after start time
pause
goto time_begin)
:time_confirm
echo.
set start_disp=%hr_start%:%min_start%:%sec_start%
set stop_disp=%hr_stop:~-2%:%min_stop%:%sec_stop%
echo Start time is %start_disp%.
echo Stop time is %stop_disp%.
echo.
set corr_time=blank
set /p corr_time=Times are correct?
if /i "%corr_time%"=="y" goto summary
if /i "%corr_time%"=="n" goto time_begin
goto time_confirm
如您所见,我有两种方法可以搜索:使用小时、分钟和秒的整数,或使用 %start_disp% 和 %stop_disp% 的字符串。这是一条示例行,所有行都遵循相同的模式,直到行中的这一点;在最后的 ] 之后,模式会有所不同:
2013/10/30 00:00:13 [501014]CODELINE_INDICATION_MSG 192.168.013.254:5501 TX 41 bytes