Can we compare two files having date and find the newer date

78 Views Asked by At

I have 2 plain text files, one on the local PC and another on the server. (The files only store the release date, e.g. 02/02/2018).

I want to write a to check if the text file on the server is a newer date than the date on the users machine and download the new files from server based on this.

I have used and it works fine, but the concern is to maintain the newer set of files.

Please advise if this is achievable.

1

There are 1 best solutions below

0
AudioBubble On

Your sample date is ambiguous.

To compare a date (without converting to datetime type or serial) you need to order it by yyyy,MM,dd.

What is your 02/02/2018 meant to be MM/dd/yyyy? Or dd/MM/yyyy?

:: Q:\Test\2019\05\02\SO_55934933.cmd
@Echo off
Call :GetDate Srv "\\server\share\folder\file.txt"
Call :GetDate PC  "X:\folder\file.txt"

if not defined Srv (Echo couldn't obtain server date  &pause&Exit /b 1)
if not defined PC  (Echo couldn't obtain local PC date&pause&Exit /b 1)

if %Srv% geq %PC% (
    Echo Server date %Srv% newer or equal to PC date %PC%
) else (
    Echo Server date %Srv% older than PC date %PC%
)
Goto :Eof
:GetDate and reverse order
set /p "Dt="<%2
:: assuming dd/MM/yyyy
For /f "tokens=1-3delims=/" %%D in ("%DT%") Do Set "%1=%%F%%E%%D"
:: assuming dd/MM/yyyy
:: For /f "tokens=1-3delims=/" %%D in ("%DT%") Do Set "%1=%%F%%D%%E"

The batch contains both variants, MM/dd/yyyy is commented out.

Sample output:

> Q:\Test\2019\05\02\SO_55934933.cmd
Server date 20180203 newer or equal to PC date 20180202