0

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’
4

3 回答 3

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.

于 2013-03-15T20:54:08.690 回答
1

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

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

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

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

find最后,您可以使用或findstr命令 测试字符串是否存在。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.
)
于 2013-03-15T20:33:13.203 回答
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

于 2013-03-15T20:29:44.107 回答