如何FINDSTR在目录中所有TXT中指定的数值范围内寻找事件?

时间:2015-10-16 19:10:08

标签: batch-file cmd

我如何获得" FINDSTR"找到指定范围之间的值? 我设置了"字符串":" 20141001"和" 20141030" 。如果* .TXT文件中存在" 20141017",则应返回" ERRORLEVEL" = 0.

例:

@echo off

SET DATE_STA=20141001
SET DATE_END=20141030

echo Looking for all the files in the folder
echo within the range from %DATE_STA% to %DATA_END% ...
echo.

:FINDING
findstr /r "%DATE_STA% to %DATA_END%" C:\Folder\*.txt
IF %ERRORLEVEL%==0 (
goto OKAY) else (
goto FAIL
)

:OKAY
cls
echo.
echo Located file that contains a value in the specified range.
echo.
pause
exit

:FAIL
cls
echo.
echo Any file located in this folder..
echo.
pause
exit

2 个答案:

答案 0 :(得分:2)

  • findstrcatches only the strings所需的正则表达式一起使用:

    findstr /r "\<201410[0-3][0-9]\>"
    

    当然这是一个简化的正则表达式,它也会捕获10月的31(这是坏的)和无效的0039(但如果你的文件只包含有效的日期,那么不是问题),所以你必须为每10天的范围写几个正则表达式。

  • 或者在循环中生成日期列表,将它们写入文件并在findstr中使用该文件。以下是生成两个日期范围的示例:20141001 2014103020151001 20151030

    @echo off
    del "%temp%\datespan.txt" >nul 2>&1
    call :makeDates 20141001 20141030 "%temp%\datespan.txt"
    call :makeDates 20151001 20151030 "%temp%\datespan.txt"
    findstr /g:"%temp%\datespan.txt" /s C:\Folder\*.txt
    del "%temp%\datespan.txt"
    pause
    exit /b
    
    :makeDates
        setlocal enableDelayedExpansion
        set "date1=%1" & set "date2=%2" & set "dateFile=%3"
        set "y1=!date1:~0,4!" & set "m1=1!date1:~4,2!" & set "d1=1!date1:~6,2!"
        set "y2=!date2:~0,4!" & set "m2=1!date2:~4,2!" & set "d2=1!date2:~6,2!"
        set /a m1-=100, d1-=100, m2-=100, d2-=100
        call :dateCalcLeap & call :dateCalcMonth
        :dateNext
            set "m=0!m1!" & set "d=0!d1!" & set "ymd=!y1!!m:~-2!!d:~-2!"
            if !ymd! GTR !date2! endlocal & exit /b
    
            echo !ymd!>>!dateFile!
    
            set /a d1+=1 & if !d1! GTR !mDays! (
                set "d1=1" & set /a m1+=1 & call :dateCalcMonth
                if !m1! GTR 12 set "m1=1" & set /a y1+=1 & call :dateCalcLeap
            )
            goto dateNext
        :dateCalcMonth
            if !m1!==2 (set/a mDays=28+leapYear) else (set/a mDays="31-(m1-1) %% 7 %% 2")
            exit /b
        :dateCalcLeap
            set leapYear=0
            set /a y4=y1 %% 4 & if !y4!==0 (
                set /a y100=y1 %% 100 & if not !y100!==0 set leapYear=1
                set /a y400=y1 %% 400 & if !y400!==0 set leapYear=1
            )
            exit /b
    

    上述解决方案会[错误地]捕获其他更大数字中的数字,如22222220141001,所以如果这是不合需要的,这是一个更慢但更可靠的版本:

    @echo off
    del "%temp%\datespan.txt" >nul 2>&1
    call :makeDates 20141001 20141030 "%temp%\datespan.txt"
    call :makeDates 20151001 20151030 "%temp%\datespan.txt"
    findstr /g:"%temp%\datespan.txt" /s C:\Folder\*.txt
    del "%temp%\datespan.txt"
    pause
    exit /b
    
    :makeDates
        setlocal enableDelayedExpansion
        set "date1=%1" & set "date2=%2" & set "dateFile=%3"
        set "y1=!date1:~0,4!" & set "m1=1!date1:~4,2!" & set "d1=1!date1:~6,2!"
        set "y2=!date2:~0,4!" & set "m2=1!date2:~4,2!" & set "d2=1!date2:~6,2!"
        set /a m1-=100, d1-=100, m2-=100, d2-=100
        call :dateCalcLeap & call :dateCalcMonth
        :dateNext
            set "m=0!m1!" & set "d=0!d1!" & set "ymd=!y1!!m:~-2!!d:~-2!"
            if !ymd! GTR !date2! endlocal & exit /b
    
            echo \^<!ymd!\^>>>!dateFile!
    
            rem The next three lines catch embedded dates like abc20141001, 10_20141001_22
            echo [^^^^0-9]!ymd![^^^^0-9]>>!dateFile!
            echo [^^^^0-9]!ymd!\^>>>!dateFile!
            echo \^<!ymd![^^0-9]>>!dateFile!
    
            set /a d1+=1 & if !d1! GTR !mDays! (
                set "d1=1" & set /a m1+=1 & call :dateCalcMonth
                if !m1! GTR 12 set "m1=1" & set /a y1+=1 & call :dateCalcLeap
            )
            goto dateNext
        :dateCalcMonth
            if !m1!==2 (set/a mDays=28+leapYear) else (set/a mDays="31-(m1-1) %% 7 %% 2")
            exit /b
        :dateCalcLeap
            set leapYear=0
            set /a y4=y1 %% 4 & if !y4!==0 (
                set /a y100=y1 %% 100 & if not !y100!==0 set leapYear=1
                set /a y400=y1 %% 400 & if !y400!==0 set leapYear=1
            )
            exit /b
    

答案 1 :(得分:1)

我不知道一个好的FINDSTR解决方案。但是有一个使用JREPL.BAT的简单解决方案 - 一个正则表达式文本处理实用程序,可以在XP以后的任何Windows机器上本机运行。它是纯脚本(混合批处理/ JScript),不需要任何第三方可执行文件。

该解决方案使用简单的正则表达式,并在命令行上提供一小部分自定义JScript代码。

for /r %%F in (.) do @type "%%F\*.txt" 2>nul | jrepl "\d{8,}" "($0>=20141001 && $0<=20141030) ? $0 : false" /jmatch >nul && echo FOUND || echo NOT FOUND