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’
With each of the commands I list here, you can do
commandname /?
for more information.The 2nd token of the
%date%
environment variable and thedate /t
command display today's date in the same format as adir
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..
) usedir /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 thefindstr
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:
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.
Do not put the batch in the searched folder (the batch file contains the
test string
!):Edit: this version is for European date format, e.g.
dd.mm.yyyy
.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:
Here, any file matching
examp*.txt
that has been created/modified today in\destdir
will be examined for the target string.