Page 1 of 1

Listing and opening files with filename selection filter

Posted: 12 Nov 2012 20:40
by d360991
Folks,

In a particular folder, I have a collection of files with dates as prefixes in the filenames. E.g.:

20121024 - file1.txt
20121028 - file2.jpg
20121112 - file3.docx
20121114 - file4.txt
20121127 - file5.xlsx

I would like to open only those files that have as prefix a date that is today or before. For instance, in the above set, I would like my batch file to launch the first three files and ignore the other two.

How would I write a script to do this?

Thank you very much,
-Roger

Re: Listing and opening files with filename selection filter

Posted: 12 Nov 2012 21:21
by Aacini
Assuming that the format displayed by DATE command is MM/DD/YYYY:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set today=%%c%%a%%b
for %%a in (*.*) do (
   set fileName=%%a
   set fileDate=!fileName:~0,8!
   if !fileDate! gtr %today% goto exitLoop
   start "" !filename!"
)
:exitLoop