0

私はバッチファイルを初めて使用しますが、基本的な理解があり、バッチが目の前にあれば編集できます。バッチ ファイルの次の要件について教えていただけないでしょうか。

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%環境変数の 2 番目のトークンと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 に答える