• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

Batch File that Reads text file and matches dates with system date.

I desperately need some help with a Batch File. How do I get a batch file to read a certain text file called PublicHolidays.txt which holds all the public holidays of year in that particular text file.

The batch file is to read the text file (publicholiday.txt) and the dates listed in the text file (as below) and checks the dates listed against the current system date. If the system date and a specific date in the text file is matched, then a certain task is carried out, otherwise a different task is done.


Text File:
-----------
20-05-2006
13-04-2006
15-03-2006
0
ben1211
Asked:
ben1211
  • 3
1 Solution
 
bkoehler-mprCommented:
Answer from: http://weblogs.asp.net/whaggard/archive/2005/08/18/423029.aspx

@echo off
rem This file assumes the US date format. Change the ABC order for your timezone (date /t)

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
      Set Current=%%B-%%A-%%C
)

for /f %%i in (dates.txt) do if %%i==%current% echo Do something today


0
 
bkoehler-mprCommented:
Inadvertently posted the previous comment early. Full script below

@echo off
rem This file assumes US date format. If required change the ABC order

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
      Set Current=%%B-%%A-%%C
)

rem Check the current date against the dates listed in the file
rem If the current date is found, set holiday to yes
for /f %%i in (publicholiday.txt) do if %%i==%current% set holiday=yes

rem If today is a Holiday, do something, else go back to work
If not "%holiday%"=="" (echo Today is a Holiday) else (echo Back to work)

rem Destroy the variables I created
set holiday=
set current=
0
 
ben1211Author Commented:
bkoehler-mpr, thank you for the code provided by yourself. I need some help with understanding  two specific lines, which are:

1. for /f %%i in (publicholiday.txt) do if %%i==%current% set holiday=yes

2. If not "%holiday%"=="" (echo Today is a Holiday) else (echo Back to work)


As for line number 1, could you explain the purpose of "set holiday=yes" &

For line number 2 could you please explain "If not "%holiday%"==""

Thank You.
0
 
bkoehler-mprCommented:
for /f was originally intended for file parsing, but can be used to parse strings and commands
tokens are which parts of the input (in our case date /t) that we care about
delims specify the delimiter we want to use (in our case "/")
in order to check for a blank variable you need to surround the variable and result in quotes (if "%variable%" == "") vs (if %variable% == )
We set holiday = yes if today is a holiday, and then check if the holiday variable is blank. We wouldn’t need to check the holiday variable if we only cared if it was a holiday (e.g. for /f %%i in (publicholiday.txt) do if %%i==%current% echo Today is a Holiday). Your example stated wanting to know both if it was or was not, thus we use an “if” statement at the end to determine that state.

If you start a command prompt (Start, Run, cmd) and type date /t you'll get something like this:
C:\WINDOWS>date /t
Mon 05/29/2006

So, we really only want the 05, 29, and 2006 parts; we don't care about the abbreviation for day of the week. Thus we use tokens to set the input variables:
%%A 05
%%B 29
%%C 2006

We then set the variable Current to 29-05-2006, using the same date format you used in your example. Please note the date /t response is reason specific, thus if you’re running this in a different country you might need to change the Set order.

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
     Set Current=%%B-%%A-%%C
)

In this bit we use “for /f” to iterate through your file of public holidays, setting holiday to Yes if a match is found. Being that we’re iterating through a list we really can’t set the variable each time to yes or no, thus if the current date matches any of the dates in the list the variable holiday would be set to yes.

For each entry in the file publicholiday.txt (%%i) check to see if the date matches the current variable (%current%), and if so set holiday to yes, otherwise ignore and continue.

for /f %%i in (publicholiday.txt) do if %%i==%current% set holiday=yes

If the holiday variable isn’t blank (e.g. equals yes) then today is a holiday, else it’s not we should get back to work. This part assumes the holiday variable doesn’t exist prior to running the application. If that were going to be an issue I’d copy the “set holiday=” line to the top of this script to make sure holiday is blank by default. You can’t test for a blank variable with (if %holiday% ==  ) as that wouldn’t work, however you can if you encase both the variable and result in quotes (if "%holiday%" == "") as this would be (if "yes" == "") if today Ire a holiday and (if "" == "") if it were not.

If not "%holiday%"=="" (echo Today is a Holiday) else (echo Back to work)

You can replace the echo statements with whatever you’re trying to do with your script.

Here at the end we set the variables I created back to nothing, thus we don’t have to worry about holiday being set to something if we run it again. I also just like cleaning up after my scripts.

rem Destroy the variables we created
set holiday=
set current=
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now