Is there a file in a directory with a modified dat

2019-08-29 13:38发布

I am new to batch files and have some basic understanding and can edit batches if I see them in front of me. I wonder if you could help me with the following requirement for my batch file.

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条回答
贪生不怕死
2楼-- · 2019-08-29 13:50

With each of the commands I list here, you can do commandname /? for more information.

The 2nd token of the %date% environment variable and the date /t command display today's date in the same format as a dir listing.

The dir command has an available /a switch which allows displaying only files with (or without) a specific attribute. To exclude directories (such as . and ..) use dir /a:-d.

You can capture the output of commands using for /f.

Finally, you can test for the existence of a string using either the find or the findstr command. findstr lets you search using regular expressions for a little more flexibility. But if you just want to search for a literal string, find will work just fine.

Put all that together:

@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

Then when you start to see your coding more as a means of artistic expression than a means to an end, you can perform more advanced trickery to perform the same task with fewer lines of code.

@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.
)
查看更多
女痞
3楼-- · 2019-08-29 13:54

Do not put the batch in the searched folder (the batch file contains the 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

Edit: this version is for European date format, e.g. dd.mm.yyyy.

查看更多
Explosion°爆炸
4楼-- · 2019-08-29 14:09
@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) 

I've set the target directory to . - the current directory - for testing. Replace the . with your target directory name.

Looks for a string in the target file 'example.txt'

Now - that assumes that you are looking for a specific filename that may appear/be modified in a directory such as ...\myappdir\myapp.log

If you mean "any file [matching some filemask] that was created/modified today" is to be searched for the string then a slightly modified routine:

@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) 

Here, any file matching examp*.txt that has been created/modified today in \destdir will be examined for the target string.

查看更多
登录 后发表回答