Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for files with current date using dos commands

Status
Not open for further replies.

gmoorthy

Programmer
Jul 13, 2004
107
US
How can I use a dos scripting to check for files with current date

I get 4 files daily

File1_yyyymmdd
file2_yyyymmdd
file3_yyyymmdd
file4_yyyymmdd
 
Send your directory search to a file then process that using a program written in the language of your choice.


Nic
 
With DOS command you can get the current date from the variable DATE or from the command DATE /T.
Then you have to extract day, month and year from your date format, assemble the filename part _yyyymmdd and search for *_yyyymmdd
 
you can try out this little example:

search_files.cmd
Code:
@[COLOR=#008080]echo[/color][COLOR=#804040][b] off[/b][/color]

[COLOR=#008080]rem[/color][COLOR=#0000ff] given is date, e.g. NN dd.mm.yyyy[/color]
[COLOR=#008080]echo[/color][COLOR=#ff00ff] current date is: "[/color][COLOR=#008080]%date%[/color][COLOR=#ff00ff]"[/color]

[COLOR=#008080]rem[/color][COLOR=#0000ff] extract date from variable DATE[/color]
[COLOR=#008080]set[/color][COLOR=#008080] mydate[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%DATE:~[/color][COLOR=#ff00ff]3[/color][COLOR=#008080],[/color][COLOR=#ff00ff]10[/color][COLOR=#008080]%[/color]
[COLOR=#008080]echo[/color][COLOR=#008080] mydate[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%mydate%[/color]

[COLOR=#008080]rem[/color][COLOR=#0000ff] extract date from the command DATE /T[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] for /F "tokens=[/color][COLOR=#ff00ff]2[/color][COLOR=#0000ff]" [/color][COLOR=#6a5acd]%%[/color][COLOR=#0000ff]i in ('DATE /T') do set mydate=[/color][COLOR=#6a5acd]%%[/color][COLOR=#0000ff]i[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] echo mydate=[/color][COLOR=#008080]%mydate%[/color]

[COLOR=#008080]rem[/color][COLOR=#0000ff] now extract day, month, year:[/color]
[COLOR=#008080]rem[/color][COLOR=#0000ff] get day[/color]
[COLOR=#008080]set[/color][COLOR=#008080] dd[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%mydate:~[/color][COLOR=#ff00ff]0[/color][COLOR=#008080],[/color][COLOR=#ff00ff]2[/color][COLOR=#008080]%[/color]
[COLOR=#008080]echo[/color][COLOR=#008080] dd[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%dd%[/color] 

[COLOR=#008080]rem[/color][COLOR=#0000ff] get month[/color]
[COLOR=#008080]set[/color][COLOR=#008080] mm[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%mydate:~[/color][COLOR=#ff00ff]3[/color][COLOR=#008080],[/color][COLOR=#ff00ff]2[/color][COLOR=#008080]%[/color]
[COLOR=#008080]echo[/color][COLOR=#008080] mm[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%mm%[/color] 

[COLOR=#008080]rem[/color][COLOR=#0000ff] get year[/color]
[COLOR=#008080]set[/color][COLOR=#008080] yyyy[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%mydate:~[/color][COLOR=#ff00ff]6[/color][COLOR=#008080],[/color][COLOR=#ff00ff]4[/color][COLOR=#008080]%[/color]
[COLOR=#008080]echo[/color][COLOR=#008080] yyyy[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%yyyy%[/color] 

[COLOR=#008080]rem[/color][COLOR=#0000ff] create file name[/color]
[COLOR=#008080]set[/color][COLOR=#008080] filename_part[/color][COLOR=#804040][b]=[/b][/color]_[COLOR=#008080]%yyyy%%mm%%dd%[/color]
[COLOR=#008080]echo[/color][COLOR=#008080] filename_part[/color][COLOR=#804040][b]=[/b][/color][COLOR=#008080]%filename_part%[/color]

[COLOR=#008080]rem[/color][COLOR=#0000ff] search in subdirectories for files named: *_yyyymmdd[/color]
[COLOR=#008080]dir[/color] [COLOR=#6a5acd]/s/b[/color] *[COLOR=#008080]%filename_part%[/color]

Note: I have on my computer date in format Mo 04.03.2019
 
You can cut out one step by using the /t parameter on the DATE command. This, on my system, returns dd/mm/ccyy.


Nic
 
If I omit the /t switch on my Win7, then the command DATE shows me the current date and then it wants to enter other value to change it:
Code:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

U:\>date
The current date is: ut 05.03.2019
Enter the new date: (dd-mm-yy)
 
I would probably use basic as the step to extract the file names.

Ed Fair
Give the wrong symptoms, get the wrong solutions.
 
>If I omit the /t switch on my Win7, then the command DATE shows me the current date and then it wants to enter other value to change it:

Yes, all the /t switch does it instruct the command not to prompt for a new date. It has no effect on the output format of the date, which is reliant on the regional settings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top