Finding and using file date in batch command

December 2016




Issue


I am trying to write what I hoped could be a simple batch program with XP command line tools. I want to find the date of a specific file, then if the date of that file is the same as the current date, the batch process would start a program. I have not been having luck. What commands do I need to use to do this?

Solution


You can use the exact following script. Save this script in file C:\Scripts\FileCheck.txt. Start biterscripting.
Enter the following command. 


script FileCheck.txt


It will do what you need. (See the code for script below.) Biterscripting is a free batch scripting environment. Follow installation instructions at http://www.biterscripting.com/install.html .

Email me if you have any questions.

Sen

I have inserted some debug statements so you can see what the script is doing. You can remove the debug statements once you know the script is doing what it is supposed to be doing. You can even schedule this script to run automatically once a day, once a week, etc. - schedule the following command using task scheduler.

"C:\biterscripting\biterscripting.exe" "C:\Scripts\FileCheck.txt"



# START OF SCRIPT FileCheck.txt 
# Get today's date. 
var str today ; set $today = gettime() ; chex "8]" $today > $today 
echo -e "DEBUG Today is " $today 

# Set the file name whose date we want to check. 
var str file ; set $file = "C:/Something/somefile" 
# ****** ENTER CORRECT FILE LOCATION ABOVE ****** 
echo -e "DEBUG Checking the date of file " $file 

# Get the date of the file 
var str fledate ; af -m $file > $filedate ; chex "8]" $filedate > $filedate 
echo -e "DEBUG File date is " $filedate 

# Check if the file date is same is today. 
if ($filedate == $today) 
# File date is same as today. 
echo -e "DEBUG File date is same as today" 

# Set the program to start 
var str program ; set $program = "C:/Program Files/someprogram.exe" 
# ****** ENTER CORRECT PROGRAM LOCATION ABOVE ********** 

# Start the program 
echo -e "DEBUG Starting program " $program 

system ("\""+$program+"\"") 
endif 
# END OF SCRIPT

Note


Thanks to SenHu for this tip on the forum.

Related :

This document entitled « Finding and using file date in batch command » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.