-2

To clarify things, since it does not make sense, i am trying to create a single .bat file that when executed, checks the computer date, then opens a file specified for that date. For example, its's Thursday, then i run the .bat to open a file called Thursday-report, something like that.

If i really cant use .bat file, please do tell me what can i do.

Thanks in advance!

MrTortol
  • 37
  • 1
  • 1
  • 5
  • 1
    If you want the actual Day of the week (Monday, Tuesday, etc....) it would be much easier to do in Powershell or Vbscript. But if you want to do it in pure batch then look at the code on [Ritchie Lawrences website](https://ritchielawrence.github.io/batchfunctionlibrary/). – Squashman Sep 10 '20 at 03:59
  • Perhaps as a single line [tag:batch-file]: `@For /F %%G In ('^""%__AppDir__%wbem\WMIC.exe" Path Win32_LocalTime Get DayOfWeek 2^> NUL ^| "%__AppDir__%findstr.exe" "\<[0123456]\>"^"') Do @For %%H In (Sunday.0 Monday.1 Tuesday.2 Wednesday.3 Thursday.4 Friday.5 Saturday.6) Do @If "%%~xH"==".%%G" Start "" "%%~nH-report.ext"`, obviously changing `.ext` to the real extension of your target file. – Compo Sep 10 '20 at 13:44

1 Answers1

1

Let's say you want to use notepad.exe to open a file dated today, e.g., 20200909.txt. In your batch file, run this:

notepad %date:~10,4%%date:~7,2%%date:~4,2%.txt

You can manipulate the date string to your preference by extracting a range of characters from the %date% variable as done in my example. E.g., %date:~10,4% extracts 4 characters from the %date% variable, starting at character 10.

Run echo %date% to see what the standard date string looks like so you can experiment with it. System settings can affect how the string gets formatted, so yours might have variations. Mine prints like this, giving me an abbreviated day of the week and then the MM/DD/YYYY format:

Thu 09/10/2020

You can also store the date string in a variable and use it at will, like this:

set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
notepad %mydate%.txt

If you specifically want the day of the week (e.g., Thursday), you can:

  1. Derive it from %DATE:~0,3% with some add'l logic not provided here
  2. Make do with the abbreviated date, e.g. %DATE:~0,3%-Report.txt for "Thu-Report.txt"
  3. Use any number of methods listed here.

But since your question left it somewhat open-ended what you mean by "date," I assume the general date string manipulation technique I discuss here can be adapted to your needs.

Marc
  • 11,403
  • 2
  • 35
  • 45