Pass the year a file was modified as variable

November 11, 2010 at 10:10:58
Specs: Windows XP
When running a batch file, I want to know if a certain .txt file has been updated in the past year. If it has, I wish to goto one label in the batch file, and if it hasn't been updated in the past year, to goto another label in the same batch file.

I found when searching the Internet, that you can use the for /f command with the ~t, but I am unsure how it should be applied. I can't seem to get the year out of the file's modified date.

I have been able to get the current year into a variable using this for /f command. But getting the year from the file itself is not coming to me. It is likely just the syntax.

If I could compare the last modified year of this file to the current year, then I can automate my batch file with no input necessary from the user. This batch file will be run by other people who have a Windows XP computer in our company.

I have seen some very long scripts that try to do it all, but I believe this is just a simple task.

See More: Pass the year a file was modified as variable

Report •

November 11, 2010 at 11:07:56
The output of ~T needs to be known.

Life is too important to be taken seriously.


Report •

November 11, 2010 at 11:35:49
Hi Mechanix2Go,

Thanks for your reply. I'm sorry, but I'm not sure what you mean by simply "the output of ~t needs to be known". I have only done a bit of batch file programming. I support secretaries at our local school board. I am trying to automate a procedure for them and I need to let the batch file know if it has been run this year already or not. Each year this batch would get run twice and I want to know if its the first or second run of the school year. Knowing the year the file was last updated would solve this.

Here is an excerpt of what I am working with. I modified someone else's example where they originally used the current date ("%date%") instead of a filename ("idlink.txt") to get the year, month, and day. I assumed that I could set a variable to a particular part of the file's modified date. A should be the month, B the day and C the year.

@echo off

For /F "tokens=1-3 delims=/ " %%A in ("idlink.txt") do (
Set Month=%%~tA
Set Day=%%~tB
Set Year=%%~tC
Set fDate=%%~tC-%%~tA-%%~tB
echo fdate: %fDate%
echo Month: %Month%
echo Day: %Day%
echo Year: %Year%

My Results are:

fdate: -11/05/2008 10:44 AM-
Month: 11/05/2008 10:44 AM

As you can see the first token A contains the entire date. Not just the month as I intended. B and C are null.

I then intend to use the %Year% field to see if it is greater than the current year.

Again I am quite novice at this. If there is any documentation online that helps to explain this function clearly, I would also appreciate some direction.


Report •
Related Solutions

Ask Question