Batch Script : Find Files with Timestamp in Folder

Closed
soumyajit9 Posts 4 Registration date Tuesday April 17, 2018 Status Member Last seen April 18, 2018 - Updated on Apr 17, 2018 at 04:58 PM
soumyajit9 Posts 4 Registration date Tuesday April 17, 2018 Status Member Last seen April 18, 2018 - 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.

2 responses

Blocked Profile
Apr 17, 2018 at 05:17 PM
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?

1
soumyajit9 Posts 4 Registration date Tuesday April 17, 2018 Status Member Last seen April 18, 2018
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 ?
0
Blocked Profile
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 ">>".
0
soumyajit9 Posts 4 Registration date Tuesday April 17, 2018 Status Member Last seen April 18, 2018
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 ?
0
Blocked Profile
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!
0