find a word and replace a line with batch file - windows

I have a local server on my Windows XP Virtual Box.
Every time my main network changes like from wifi to cable or I move to a different network the IP of my Win XP box changes.
I want to run a batch file to map the new IP to some common name in my hosts file
C:\WINDOWS\system32\drivers\etc\hosts
for example if I have now:
10.97.100.74 www.myAppServer.com
and if the ip changes to 192.168.11.9,
I want to search for the string myAppServer in the hosts file and replace the whole line with
192.168.11.9 www.myAppServer.com
I was able to get the current IP using:
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
set IP=%IP%
but how do I find that line and replace it?

I found this on DOS Tips
BatchSubstitute - parses a File line by line and replaces a substring
#echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
::BatchSubstitute - parses a File line by line and replaces a substring
::syntax: BatchSubstitute.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
::$changed 20100115
::$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)

If you want to replace just one line, you can do it this way:
#echo off
setlocal
cd "C:\WINDOWS\system32\drivers\etc"
for /F "delims=:" %%a in ('findstr /N "myAppServer" hosts') do set lineNum=%%a
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" hosts') do (
if %%a equ %lineNum% (
echo 192.168.11.9 www.myAppServer.com
) else (
echo %%a
)
)) > hosts.new
This program eliminate empty lines and will have problems if the hosts file contain Batch special characters, like | > < & !. These details may be fixed, if needed.

The simplest way of doing it is not replacing it. Remove it and add a new line.
If you have your ip address in %IP%, then
set "hostsFile=%systemroot%\system32\drivers\etc\hosts"
(
findstr /v /i /c:"www.MyAppServer.com" "%hostsFile%"
echo(
echo %IP% www.MyAppServer.com
) > "%temp%\hosts"
move /y "%temp%\hosts" "%hostsFile%" >nul 2>nul
And be sure you have rights enough to change the hosts file and that no antivirus is blocking changes to it.

this get the job done finally:
#echo off
REM Set a variable for the Windows hosts file location
set "hostpath=%systemroot%\system32\drivers\etc"
REM set "hostpath=C:\projectStar\Programs\etc"
set "hostfile=hosts"
REM Make the hosts file writable
attrib -r -s -h "%hostpath%\%hostfile%"
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
REM echo %IP%
set IP=%IP%
move /y "%hostpath%\%hostfile%" "Hs.txt"
findstr /v/c:"www.nexsoftnetwork.com" Hs.txt > "Hss2.txt"
echo %IP% www.nexsoftnetwork.com >>"Hss2.txt"
move /y "Hss2.txt" "%hostpath%\%hostfile%"
echo Done.
pause
thank you all for your inputs.
But I wanted to hit this server from my phone which will be on the same network as the server. and eventually I came to notice that unless I modify the hosts file of the host operating system or the mobile phone it won't still solve my problem. this solution works only if I want to hit this server with in the virtual box.

Related

How can I use a batch file to find a string in a text file and replace the entire line while keeping blank lines [duplicate]

This DOS batch script is stripping out the blank lines and not showing the blank lines in the file even though I am using the TYPE.exe command to convert the file to make sure the file is ASCII so that the FIND command is compatible with the file. Can anyone tell me how to make this script include blank lines?
#ECHO off
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE.exe "build.properties" ^| FIND.exe /V ""`) DO (
ECHO --%%A--
)
pause
That is the designed behavior of FOR /F - it never returns blank lines. The work around is to use FIND or FINDSTR to prefix the line with the line number. If you can guarantee no lines start with the line number delimiter, then you simply set the appropriate delimiter and keep tokens 1* but use only the 2nd token.
::preserve blank lines using FIND, assume no line starts with ]
::long lines are truncated
for /f "tokens=1* delims=]" %%A in ('type "file.txt" ^| find /n /v ""') do echo %%B
::preserve blank lines using FINDSTR, assume no line starts with :
::long lines > 8191 bytes are lost
for /f "tokens=1* delims=:" %%A in ('type "file.txt" ^| findstr /n "^"') do echo %%B
::FINDSTR variant that preserves long lines
type "file.txt" > "file.txt.tmp"
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" "file.txt.tmp"') do echo %%B
del "file.txt.tmp"
I prefer FINDSTR - it is more reliable. For example, FIND can truncate long lines - FINDSTR does not as long as it reads directly from a file. FINDSTR does drop long lines when reading from stdin via pipe or redirection.
If the file may contain lines that start with the delimiter, then you need to preserve the entire line with the line number prefix, and then use search and replace to remove the line prefix. You probably want delayed expansion off when transferring the %%A to an environment variable, otherwise any ! will be corrupted. But later within the loop you need delayed expansion to do the search and replace.
::preserve blank lines using FIND, even if a line may start with ]
::long lines are truncated
for /f "delims=" %%A in ('type "file.txt" ^| find /n /v ""') do (
set "ln=%%A"
setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
echo(!ln!
endlocal
)
::preserve blank lines using FINDSTR, even if a line may start with :
::long lines >8191 bytes are truncated
for /f "delims=*" %%A in ('type "file.txt" ^| findstr /n "^"') do (
set "ln=%%A"
setlocal enableDelayedExpansion
set "ln=!ln:*:=!"
echo(!ln!
endlocal
)
::FINDSTR variant that preserves long lines
type "file.txt" >"file.txt.tmp"
for /f "delims=*" %%A in ('findstr /n "^" "file.txt.tmp"') do (
set "ln=%%A"
setlocal enableDelayedExpansion
set "ln=!ln:*:=!"
echo(!ln!
endlocal
)
del "file.txt.tmp"
If you don't need to worry about converting the file to ASCII, then it is more efficient to drop the pipe and let FIND or FINDSTR open the file specified as an argument, or via redirection.
There is another work around that completely bypasses FOR /F during the read process. It looks odd, but it is more efficient. There are no restrictions with using delayed expansion, but unfortunately it has other limitations.
1) lines must be terminated by <CR><LF> (this will not be a problem if you do the TYPE file conversion)
2) lines must be <= 1021 bytes long (disregarding the <CR><LF>)
3) any trailing control characters are stripped from each line.
4) it must read from a file - you can't use a pipe. So in your case you will need to use a temp file to do your to ASCII conversion.
setlocal enableDelayedExpansion
type "file.txt">"file.txt.tmp"
for /f %%N in ('find /c /v "" ^<"file.txt.tmp"') do set cnt=%%N
<"file.txt.tmp" (
for /l %%N in (1 1 %cnt%) do(
set "ln="
set /p "ln="
echo(!ln!
)
)
del "file.txt.tmp"
I wrote a very simple program that may serve as replacement for FIND and FINDSTR commands when they are used for this purpose. My program is called PIPE.COM and it just insert a blank space in empty lines, so all the lines may be directly processed by FOR command with no further adjustments (as long as the inserted space don't cares). Here it is:
#ECHO off
if not exist pipe.com call :DefinePipe
FOR /F "USEBACKQ delims=" %%A IN (`pipe ^< "build.properties"`) DO (
ECHO(--%%A--
)
pause
goto :EOF
:DefinePipe
setlocal DisableDelayedExpansion
set pipe=´)€ì!Í!ŠÐŠà€Ä!€ü.t2€ü+u!:æu8²A€ê!´#€ì!Í!².€ê!´#€ì!Í!²+€ê!´#€ì!Í!Šò€Æ!´,€ì!Í!"Àu°´LÍ!ëÒ
setlocal EnableDelayedExpansion
echo !pipe!>pipe.com
exit /B
EDIT: Addendum as answer to new comment
The code at :DefinePipe subroutine create a 88 bytes program called pipe.com, that basically do a process equivalent to this pseudo-Batch code:
set "space= "
set line=
:nextChar
rem Read just ONE character
set /PC char=
if %char% neq %NewLine% (
rem Join new char to current line
set line=%line%%char%
) else (
rem End of line detected
if defined line (
rem Show current line
echo %line%
set line=
) else (
rem Empty line: change it by one space
echo %space%
)
)
goto nextChar
This way, empty lines in the input file are changed by lines with one space, so FOR /F command not longer omit they. This works "as long as the inserted space don't cares" as I said in my answer.
Note that the pipe.com program does not work in 64-bits Windows versions.
Antonio
Output lines including blank lines
Here's a method I developed for my own use.
Save the code as a batch file say, SHOWALL.BAT and pass the source file as a command line parameter.
Output can be redirected or piped.
#echo off
for /f "tokens=1,* delims=]" %%a in ('find /n /v "" ^< "%~1"') do echo.%%ba
exit /b
EXAMPLES:
showall source.txt
showall source.txt >destination.txt
showall source.txt | FIND "string"
An oddity is the inclusion of the '^<' (redirection) as opposed to just doing the following:
for /f "tokens=1,* delims=]" %%a in ('find /n /v "" "%~1"') do echo.%%ba
By omitting the redirection, a leading blank line is output.
Thanks to dbenham, this works, although it is slightly different than his suggestion:
::preserve blank lines using FIND, no limitations
for /f "USEBACKQ delims=" %%A in (`type "file.properties" ^| find /V /N ""`) do (
set "ln=%%A"
setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
echo(!ln!
endlocal
)
As mentioned in this answer to the above question, it doesn't seem that lines are skipped by default using for /f in (at least) Windows XP (Community - Please update this answer by testing the below batch commands on your version & service pack of Windows).
EDIT: Per Jeb's comment below, it seems that the ping command, in at least Windows XP, is
causing for /f to produce <CR>'s instead of blank lines (If someone knows specifically why, would
appreciate it if they could update this answer or comment).
As a workaround, it seems that the second default delimited token (<space> / %%b in the example)
returns as blank, which worked for my situation of eliminating the blank lines by way of an "parent"
if conditional on the second token at the start of the for /f, like this:
for /f "tokens=1,2*" %%a in ('ping -n 1 google.com') do (
if not "x%%b"=="x" (
{do things with non-blank lines}
)
)
Using the below code:
#echo off
systeminfo | findstr /b /c:"OS Name" /c:"OS Version"
echo.&echo.
ping -n 1 google.com
echo.&echo.
for /f %%a in ('ping -n 1 google.com') do ( echo "%%a" )
echo.&echo.&echo --------------&echo.&echo.
find /?
echo.&echo.
for /f %%a in ('find /?') do ( echo "%%a" )
echo.&echo.
pause
.... the following is what I see on Windows XP, Windows 7 and Windows 2008, being the only three versions & service packs of Windows I have ready access to:

Windows Batch, Split File by Delimiter, refinement

I have a folder full of text files that I need to split based on the (optional) presence of a delimiter. I found an answer for how to actually split the file here: Split text file into 2 files by separator
#echo off&setlocal
set "file=_frmCore.frm"
for /f "delims=[]" %%i in ('^<"%file%" find /n "SearchTermHere"') do set "split=%%i"
(for /f "tokens=1*delims=[]" %%i in ('^<"%file%" find /n /v ""') do if %%i lss %split% echo(%%j)>"%file%.new1"
<"%file%">"%file%.new2" more +%split%
type "%file%.new?"
Works great for what it is, but I need a few refinements and not sure where to start. Looking for:
Wrap it in a loop for all files in the directory (no subfolders to worry about)
"SearchTermHere" is the first on a line (the only on a line), with a specific term on the previous line that I'd rather match for safety... how can I tell it "PreviousLine/r/nSearchTermHere/r/n"? Unsure of the correct syntax here.
Rather than creating two new files, move the "after search term" portion to a new file and remove it from the original
Parameterize folder name to operate in so I can call from other programs
(apologies... I've tried deciphering this code and finding out what does what and trying to go from there, but this stuff is not my cup of tea and a strong push in the right direction would be wonderful)
#ECHO OFF >NUL
SETLOCAL enableextensions
rem enabledelayedexpansion
set "_files=*.frm"
set "_sfind=SearchTermHere"
if not "%~1"=="" if exist "%~1\" (
pushd "%~1"
set "_popd=popd"
) else ( set "_popd=" )
for /F "delims=" %%G in ('findstr /m "^%_sfind%$" %_files%') do (
type NUL > "%%G.new1"
type NUL > "%%G.new2"
for /f "delims=:" %%i in ('findstr /n "^%_sfind%$" "%%G"') do (
for /f "tokens=1* delims=:" %%I in ('findstr /n "^" "%%G"') do (
if %%I lss %%i (
>> "%%G.new1" echo(%%J
)
if %%I gtr %%i (
>> "%%G.new2" echo(%%J
)
)
)
rem remove `ECHO` from next line no sooner than debugged!
ECHO move /Y "%%G.new1" "%%G"
type "%%G.new?"
)
%_popd%
Changes made in your code:
Wrap it in a loop for all files in the directory: see for /F "delims=" %%G loop against all files matching your criteria: findstr /m "^%_sfind%$" %_files%.
"SearchTermHere" is the first on a line (the only on a line): ^=beginning of line and $=end of line in findstr /m "^%_sfind%$" %_files%; used findstr command rather than find.
Rather than creating two new files, move the "after search term" portion to a new file and remove it from the original: see the move /Y "%%G.new1" "%%G" workaround. Operational move command is ECHOed here merely for debugging purposes. Remove ECHO from that line no sooner than debugged!
Parameterize folder name to operate in so I can call from other programs:
call "batch path\31749577.bat" "folder path"
see %~1 test: if a parameter is supplied to the batch and represents a folder, (more in Command Line arguments (Parameters)) and
see pushd - popd pair: PUSHD changes the current directory/folder and store the previous folder/path for use by the POPD command.
The tricky <"%%G">"%%G.new2" more +%%i command substituted with less effective but clear to understand if %%I gtr %%i ( ... inside the %%I loop. However, next code snippet (entire %%G loop) will work as well:
for /F "delims=" %%G in ('findstr /m "^%_sfind%$" %_files%') do (
for /f "delims=:" %%i in ('findstr /n "^%_sfind%$" "%%G"') do (
>"%%G.new1" (for /f "tokens=1* delims=:" %%I in ('
findstr /n "^" "%%G"') do if %%I lss %%i echo(%%J)
<"%%G">"%%G.new2" more +%%i
)
rem remove `ECHO` from next line no sooner than debugged!
ECHO move /Y "%%G.new1" "%%G"
type "%%G.new?"
)
Excuse me. Although your description is extensive, it is also confusing. The Batch file below:
Search for a line that is "SearchTermHere".
If the previous line is "PreviousLine":
Move from line after SearchTerm up to end of file to another file
Repeat previous process on all files in folder.
.
#echo off
setlocal EnableDelayedExpansion
set "find=SearchTermHere"
set "prevLine=PreviousLine"
rem Process the folder given in parameter
cd %1
rem Process all files in folder
for /F "delims=" %%F in ('dir /A-D /B') do (
rem Get the line number of the search line - 1
set "numLines="
for /F "delims=:" %%a in ('findstr /N "^%find%$" "%%F"') do set /A "numLines=%%a-1"
if defined numLines (
rem Open a code block to read-input-file/create-output-file
< "%%F" (
rem Copy numLines-1 lines
for /L %%i in (1,1,%numLines%) do (
set "line="
set /P "line="
echo(!line!
)
rem If the line before search is prevLine
if "!line!" equ "%prevLine%" (
rem Copy just the search line to original file
set /P "line="
echo !line!
rem and copy the rest of lines to another file
findstr "^" > "%%~nF-PART2%%~xF"
)
) > "%%F.tmp"
if exist "%%~nF-PART2%%~xF" (
rem Replace original file with created output file (first part)
move /Y "%%F.tmp" "%%F" > NUL
) else (
rem Remove the output file
del "%%F.tmp"
)
)
)
For a further explanation of this method, see this post.
Sorry, I was unable to use either of the proposed solutions. However they did help - I spent the last two days learning about the (odd) syntaxes and operations involved in batch files and came up with the following. It doesn't do quite everything that I was looking for (and I altered the program that outputs the files a bit for further support), but it does get the job done:
#ECHO off
setlocal EnableDelayedExpansion
:: change the working directory to match the input
cd /D %~d1
cd %~p1\
:: loop all .frm files and find the CodeBehindForm string
for %%F in (*.frm) do (
for /F "delims=[]" %%a in ('^<%%F find /n "CodeBehindForm"') do (
if %%a gtr 0 (
for /F "tokens=1*delims=[]" %%i in ('^<"%%F" find /n /v ""') do if %%i gtr %%a echo(%%j)>>"%%~nF.vb"
for /F "tokens=1*delims=[]" %%i in ('^<"%%F" find /n /v ""') do if %%i lss %%a echo(%%j)>>"%%~nF.fwm"
:: if the codebehind was found and parsed out, there's a .fwm and .vb file
:: remove the original
if exist %%~nF.vb del %%F
)
)
:: change the .fwm extensions back to their original .frm extensions
ren *.fwm *.frm
I'm sure it's not "correct" in many ways, but for now it gets the job done.
Thanks for the help.

How to compare file names in directory with file names in a text file?

I try to improve an overview of my search and find memories for batch files in Windows XP command prompt environment.
In order to my previous sentence I am not happy with my search possibilities and have to post a question.
I try to compare the names of some text files and have written words in a text file that are by reading the same. With such a start environment I wrote following batch script to get an echo output.
The aim is
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & echo !var!
rem The output is the name of the files without extension. Now my question:
rem Is it possible to compare the above file names with some input
rem from a text file, for example like:
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & for /f %%a in (Textfile.txt) do (if !var!==%%a echo good else echo search)
rem That returns no output. I would like to know if there are possibilities
rem to do that? And if it is possible, how to revise this batch file?
endlocal disabledelayedexpansion
pause
Have a nice day, wishes
Stefan
This should work with Latin characters - some foreign characters may not work:
#echo off
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder\*.*" ') do find /i "%%~nb" < "textfile.txt" >nul && (echo "%%~nb" found) || (echo "%%~nb" not found)
pause
proper formatting you code increases readabilty:
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder"') do (
for /f %%a in (textfile.txt) do (
if "%%~nb"=="%%a" ( echo good ) else ( echo search )
)
)
I added a /b to the dircommand (show name only, no date/time/attributes) and a /a-d to exclude directorynames.
You don't need to use a variable (!var!) here (but you can, it works fine).

windows 7 not generating txt report

I want to generate a txt file with my dns history. Although the batch script executes just fine on windows 8, when i run it on windows 7 it simply creates a blank txt file. Does anyone knows why this is happening?
Here's the batch script
#echo off
setlocal enableextensions
set "baseName=dnshistory"
set "count=0"
for /f "delims=%baseName%." %%a in (
'dir /b /o-d "%baseName%*.txt" 2^>nul'
) do ( set /a "count=%%a+1" & goto saveData )
:saveData
ipconfig /displaydns | find "Record Name" > "%baseName%%count%.txt"
Is you Windows 7 version in English too ?
Open a CMD windows and test just the command :
ipconfig /displaydns | find /i "Record Name"
and look if something is displayed.
If not, try just the command :
ipconfig /displaydns
and look the language used Then correct your code with the correct words.
IE in Portuguese it will be :
ipconfig /displaydns | find /i "Nome do Registro"
try this for a language independent solution:
:saveData
(for /f "tokens=2 delims=:" %%a in ('ipconfig /displaydns') do (
echo %%a| find "." |findstr /v /r "[0-9]$"
))>file.txt
(take every line, filter those, that have a . after a : (second token) and filter out all lines that end with a number)
EDIT another approach (because the above gives some unwanted lines):
find the first line after every ----------------- line:
#echo off
setlocal enabledelayedexpansion
ipconfig /displaydns |findstr /n "^" >a.txt
for /f "tokens=1 delims=:" %%a in ('findstr /c:" --------------" a.txt') do (
set /a line=%%a+1
for /f "tokens=1,2,* delims=:" %%i in ('findstr /B "!line!:" a.txt') do echo(%%k
)

Is there a file in a directory with a modified date of today - Batch File

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’
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.
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.
)
#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.

Resources