Search code examples
datebatch-filelast-modified

Batch Code to Skip Script if File is too Old


My Problem

As there are a ton of threads that address 'using a batch file to return file modify date' (or delete files older than, etc) - let me first specify my issue.

I'm looking to create a batch (not PowerShell, etc) that will return the last modify date of a specific file given UNC path and filename.

Code, Attempt 1

I've taken a peek at a few potential solutions on other threads, but I've run into a number of unique issues. The first and most obvious solution for this would be the "ForFiles" command in batch. For example:

set myPath=\\myUNCpath
set myFile=myFileName.csv

forfiles /p "%myPath%" /m %myFile% /c "GoTo OldFile" /d -6 

Thus, if the file is older than I want -- jump to a specific section of my batch for that. However, this yields the error:

ERROR: UNC paths (\machine\share) are not supported.

However, this cmd won't work due to the use of UNC (which is critical as this batch is called by system's task scheduler). So it seems like the 'ForFiles' cmd is out.

Code, Attempt 2

I could go a more round about way of doing it, but simply retrieving the last modified date of the file (which in batch would return a string). I can truncate the string to the necessary date values, convert to a date, and then compare to current date. To do that, I've also looked into just using a for loop such as:

set myFile=\\myUNCpath\myFileName.csv
echo %myFile%
pause

FOR %%f IN (%myFile%) DO SET myFileDate=%%~tf
echo %myFileDate%
pause

Though my first debug echo provides the proper full file name, my second debug just returns ECHO is off., which tells me it's either not finding the file or the for loop isn't returning the file date. Not sure why.

I've also tried minor changes to this just to double check environmental variable syntax:

FOR %%f IN (%myFile%) DO SET myFileDate=%%~ta

Returns %ta

And finally:

FOR %%f IN (%myFile%) DO SET myFileDate=%~ta

Which without the extra '%', just crashes the batch.

I'm really at a loss at this point. So any tips or guidance would be greatly appreciated!


Solution

  • Huge thanks to both @Squashman and @MadsTheMan for the help. Luckily this batch finally is the piece of code that I can take to my boss to push switching over to at least using PowerShell!

    Anyway, for those of you looking for the best way to get a UNC path file's modify date, here is what I've come up with. Not very different than other threads (and a thanks goes out to Squashman for spotting my missing " " in the for loop).

    set myFile=\\myUNCpath\myFileName.ext
    
    FOR %%f IN ("%myFile%") DO SET myFileDate=%%~tf
    
    ::And if you'd like to try to do long winded calculations you can further break the dates down via...
    set fileMonth=%myFileDate:~0,2%
    set fileDay=%myFileDate:~3,2%
    set fileYear=%myFileDate:~6,4%
    
    set currentDate=%date%
    set currentMonth=%currentDate:~4,2%
    set currentDay=%currentDate:~7,2%
    set currentYear=%currentDate:~10,4%
    

    The plan was to then convert the strings to integers, and then use a switch-case block to determine if the variance between dates was acceptable... blah blah blah.

    Long story short - if you are limited to batch (and not creating secondary files -- such as a csv or the temp file suggested by MadsTheMan) you're going to have a really long script on your hands that still might have flaws (like leap year, etc unless you're adding even MORE code), when you could just make the comparison calculation using one line of code in other programs.