目录中是否有修改日期的文件 - 批处理文件

时间:2013-03-15 19:28:12

标签: windows date for-loop batch-file cmd

我是批处理文件的新手并且有一些基本的了解,如果我在我面前看到它们,可以编辑批处理。 我想知道你是否可以帮我解决我的批处理文件的以下要求。

Is there a file in a directory with a modified date of today
If yes
Is there a certain text string of ‘test’ in the file
    If yes
        echo ‘test string found’
    Else
        echo ‘test string NOT found’
Else 
    Echo ‘no file today’

3 个答案:

答案 0 :(得分:1)

@ECHO OFF
SETLOCAL
:: Point to the target directory. establish target file and string required
SET target=.
SET targfile=example.txt
SET targtxt=FIND me
::
SET targfile="%target%\%targfile%"
IF NOT EXIST %targfile% ECHO No file today& GOTO :EOF 
:dfloop
SET filename=q%random%.q$q
SET fullname="%target%\%filename%"
IF EXIST %fullname% GOTO dfloop
:: create a dummy file dated today
COPY nul: %fullname% >nul
 FOR /f %%i IN ('dir %fullname% ^|find /i "%filename%"') DO SET today=%%i
DEL %fullname%
FOR /f %%i IN ('dir %targfile% ') DO IF %today%==%%i (SET today=)
IF DEFINED today ECHO No file today&GOTO :eof
FIND /i "%targtxt%" %targfile% >NUL
IF ERRORLEVEL 1 (ECHO test string NOT found) ELSE (ECHO test string found) 

我已将目标目录设置为. - 当前目录 - 以进行测试。将.替换为目标目录名称。

在目标文件'example.txt'

中查找字符串

现在 - 假设您正在查找可能在...\myappdir\myapp.log

等目录中显示/修改的特定文件名

如果你的意思是“今天创建/修改的任何文件[匹配某些文件掩码]”将被搜索字符串,那么稍微修改一下例程:

@ECHO OFF
SETLOCAL
:: Point to the target directory. establish target filemask and string required
SET target=\destdir
SET targfile=examp*.txt
SET targtxt=FIND me
::
SET targfile="%target%\%targfile%"
IF NOT EXIST %targfile% ECHO No file today& GOTO :EOF 
:dfloop
SET filename=q%random%.q$q
SET fullname="%target%\%filename%"
IF EXIST %fullname% GOTO dfloop
:: create a dummy file dated today
COPY nul: %fullname% >nul
FOR /f %%i IN ('dir %fullname% ^|find /i "%filename%"') DO SET today=%%i
DEL %fullname%
SET count=0
FOR /f %%i IN ('dir %targfile% ') DO IF %today%==%%i (SET /a count+=1)
IF %count% equ 0 ECHO No file today&GOTO :eof
FOR /f "tokens=1*delims=:" %%i IN (
 'dir /b/a-d/o-d %targfile%^|findstr /n /v "*" '
  ) DO IF %%i leq %count% (
FIND /i "%targtxt%" "%target%\%%j" >NUL
IF NOT ERRORLEVEL 1 (SET today=)
)
IF DEFINED today (ECHO test string NOT found) ELSE (ECHO test string found) 

此处,将检查今天在examp*.txt中创建/修改的任何与\destdir匹配的文件,以查找目标字符串。

答案 1 :(得分:1)

使用我在此列出的每个命令,您可以commandname /?获取更多信息。

%date%环境变量的第二个标记和date /t命令以与dir列表相同的格式显示今天的日期。

dir命令有一个可用的/a开关,只允许显示具有(或不具有)特定属性的文件。要排除目录(例如...),请使用dir /a:-d

您可以使用for /f来捕获命令的输出。

最后,您可以使用findfindstr命令测试字符串是否存在。 findstr允许您使用正则表达式进行搜索,以获得更大的灵活性。但是,如果您只想搜索文字字符串,find将正常工作。

把所有这些放在一起:

@echo off

rem "setlocal" prevents any variables you set within your batch script
rem from remaining as environment orphans after the script completes.
setlocal

rem set variable %today% as the second token of %date%
for /f "tokens=2" %%I in ("%date%") do set today=%%I

rem dir list, skip directories, skip this batch script, include only files with a date matching %today%
for /f "tokens=4*" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "%today%"') do (

    rem record success for later
    set found=1

    rem search file %%I for "string" (case-insensitive).
    find /i "string" "%%I">NUL

    rem Was last command successful?
    if %ERRORLEVEL%==0 (
        echo Test string found
    ) else (
        echo Test string NOT found
    )
)

rem if success was not recorded
if not defined found echo No file today

然后,当你开始将编码看作是艺术表达的手段而不是达到目的的手段时,你可以用更少的代码行执行更高级的技巧来执行相同的任务。

@echo off
setlocal
for /f "tokens=2" %%I in ("%date%") do set today=%%I
for /f "tokens=4*" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "%today%" ^|^| echo No file today 1^>^&2') do (
    (find /i "string" "%%I" >NUL && (
        echo Test string found.
    )) || echo Test string not found.
)

答案 2 :(得分:1)

不要将批次放在搜索到的文件夹中(批处理文件包含test string!):

@echo off &setlocal
:: first modify the search path here!
set "searchpath=."
set "text=test"

dir /a-d | find "%date%" >nul || (echo no file today&goto :eof)
for /f "tokens=3*" %%i in ('dir /a-d %searchpath% ^| find "%date%"') do (
    for /f %%k in ('findstr /mc:"%text%" "%searchpath%\%%j"') do (
        if not "%%k"=="" set "found=true"
    )
)
if not defined found (echo test string NOT found) else echo test string found
endlocal

编辑:此版本适用于欧洲日期格式,例如dd.mm.yyyy