Batch Script : Find Files with Timestamp in Folder

soumyajit9 4 Posts Tuesday April 17, 2018Registration date April 18, 2018 Last seen - Apr 17, 2018 at 03:27 PM - Latest reply: soumyajit9 4 Posts Tuesday April 17, 2018Registration date April 18, 2018 Last seen
- Apr 18, 2018 at 06:45 PM
Hi Friends,

I am trying to write a batch script that will:
1) Go to a particular Folder/Directory.
2) Find the files which have current date stamp in the filename (TestFile20180417.dat).
3) Generate a log file with the current timestamp filenames found in step2 above (Can be placed either in same directory or different).

Thanks a lot in advance.

Edit: I will be running this on Windows 7 and will set up as a Scheduled Task in Task Scheduler. So I am looking for a bat script only.
See more 

Your reply

7 replies

Best answer
ac3mark 10525 Posts Monday June 3, 2013Registration dateModeratorStatus September 21, 2018 Last seen - Apr 17, 2018 at 05:17 PM
1
Thank you
Which part are you stuck on? Can you build a folder directory? At least start something, like declare variables or something!

The time stamp of the file will have the actual date and time. Why use the name for your qualification?

Thank you, ac3mark 1

Something to say? Add comment

CCM has helped 1872 users this month

soumyajit9 4 Posts Tuesday April 17, 2018Registration date April 18, 2018 Last seen - Apr 17, 2018 at 06:19 PM
Right now, I have placed the below script in the destination folder which runs the script to pull out all the files present in that folder, and then write it to a log file.

However, I want to be able to only pull out one file with the current date stamp as of the machine and output it to a log file. The script should be able to find the system date and use the params to find a file with the datestamp.

I am currently using below script:

@echo off
dir %1 /b /o:gn > "%temp%\Listing"
start /w notepad "%temp%\Listing"
del "%temp%\Listing"
exit

How do I only extract the file which has the current date stamp as on the machine ? This is where I am stuck at. How do I define variables for this ?
ac3mark 10525 Posts Monday June 3, 2013Registration dateModeratorStatus September 21, 2018 Last seen - Apr 18, 2018 at 04:19 PM
Well, to start with, you can write directly to the log, without having to call in notepad. Here is an example of that:

echo log line %A%>>c:\temp\listing\log.log

If Log.log already exist, it will over write it. If you wish to append to the log.log use the ">" instead of the ">>".
soumyajit9 4 Posts Tuesday April 17, 2018Registration date April 18, 2018 Last seen - Apr 18, 2018 at 04:52 PM
Thanks a lot for that. But how do I extract only the file which has the datestamp same as the system date ?
For example, from that folder, I wish to only extract and write to log, the file which has date stamp - TESTFILE20180418.dat ?
ac3mark 10525 Posts Monday June 3, 2013Registration dateModeratorStatus September 21, 2018 Last seen - Apr 18, 2018 at 06:13 PM
Ok, do this:

Cut and paste the code below, then see what it produces. Modify your code based on the result.

for /f %%i in ('dir /b/a-d/od/t:c %yourfoldervariable%\WhatEverFileNameYouWish.*') do set LAST=%%i
echo The Most recently created file is %LAST%>>%LOGFILE%

SET THEWHOLARCHIVE=%thearchivefolder%\%LAST%
echo THE NEWEST ARCHIVE FOLDER IS SET TO %THEWHOLARCHIVE%>>%LOGFILE%


Give that a try!

Have fun!
Respond to ac3mark
Best answer
ac3mark 10525 Posts Monday June 3, 2013Registration dateModeratorStatus September 21, 2018 Last seen - Apr 18, 2018 at 06:28 PM
1
Thank you
Then you can go a little bit further, with another script in powershell to EMAIL you the results of what was found!

ROBOCOPY %oFolder% %thearchivefolder% %LAST% /r:3 /w:5 /np /log:%ROBOLOG%

echo Going to Send Mail

findstr /I "ERROR" %LOGFILE% | find /I "ERROR" > nul
if %errorlevel% EQU 0 goto :CopyF
ECHO %errorlevel% - level of error
goto :CopySuccess
REM -------------------------------------------------------FAIL--------------------------------------------------------------------
:CopyF
echo FAIL

Set eSubjectFail="Copy Failed"
Set eBodyFail="failed %errorlevel%"
echo FAILED COPY %THEWHOLARCHIVE%>>%LOGFILE%

PowerShell.exe -File \\her\is\where\you\store\your\secret\stuff\sendcopyreportmail.ps1 "youremail@somewhere.com,yourbuddieswhileyoureonvacation@anothergrubbyemail.com" "whateveremailaddressisauthentictedtosendonthesmtpserver@domain.com" %eSubjectFail% %eBodyFail% %ROBOLOG%
goto :END
REM --------------------------------------------------------------------------------------------------------------------------------
REM ---------------------------------------------------------SUCCESS----------------------------------------------------------------
:RoboCopySuccess
echo PASS

Set eSubjectPass="Copy Succeeded"
Set eBodyPass="Backup worked"

echo SUCCESS COPYING %THEWHOLARCHIVE%>>%LOGFILE%

PowerShell.exe -File \\her\is\where\you\store\your\secret\stuff\sendcopyreportmail.ps1 "youremail@somewhere.com,yourbuddieswhileyoureonvacation@anothergrubbyemail.com" "whateveremailaddressisauthentictedtosendonthesmtpserver@domain.com" %eSubjectFail% %eBodyFail% %ROBOLOG%
goto :END
REM -----------------------------------------------------------------------------------------------------------------------------------


Have fun!

Thank you, ac3mark 1

Something to say? Add comment

CCM has helped 1872 users this month

soumyajit9 4 Posts Tuesday April 17, 2018Registration date April 18, 2018 Last seen - Apr 18, 2018 at 06:45 PM
Thanks so much ! The purpose of this is to set up a Novell Operations Monitoring alert mechanism.

What I am trying overall is to
1) Generate a log file with the current date stamp file.
2) Setup an alert that will trigger off at a specified time and throw an alert on Novell Dashboard showing that files are not present. If the datestamp file is present, it will not alert.

If it alerts, then we can go and perform the manual workaround to run the process that would have generated the file with datestamp in the first place.
Respond to ac3mark