File Comparison by Date Time

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: File Comparison by Date Time

#16 Post by Dragokas » 26 May 2014 05:30

i dont think it can be done with robocopy cause its job is only to copy.

No, it will not. Please, look at /L switch.

This batch will do the work you last asked:

Code: Select all

@echo off
SETLOCAL EnableExtensions

set folder1=e:\Pandoraflyff\Resclient
set folder2=e:\Pandoraflyff\Patcher
set mask=*.GZ

chcp 862 2>NUL 1>&2
For /F "tokens=1-2*" %%a in (
  'robocopy "%folder1%" "%folder2%" "%mask%" /E /IS /XL /L /NJS /NJH /NDL ^| find /i "Newer"'
) do call :NEWER "%%c"

pause & exit /B

:NEWER
  echo %~1 is NEWER.
exit /B

imagination
Posts: 13
Joined: 24 May 2014 01:32

Re: File Comparison by Date Time

#17 Post by imagination » 26 May 2014 05:58

Thank you for the anwer
And yes you are correct about the date conversion.

I will implant that and will let you know if that solves it


Thank you verry much Aacini
Its solved now

It is comparing in the correct way and if e:\Pandoraflyff\Resclient\file.res is greater then e:\Pandoraflyff\Patcher he only gzips the correct file.

Here is the altered code.
BTW i didnt not set the datetime conversion in the batch file but i did it with the registery.

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

SETLOCAL EnableDelayedExpansion
for /f "skip=1 tokens=1-6 delims= " %%a in ('wmic path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do (
   IF NOT "%%~f"=="" (
      set /a FormattedDate=10000 * %%f + 100 * %%d + %%a
      set FormattedDate=!FormattedDate:~-2,2!-!FormattedDate:~-4,2!-!FormattedDate:~-8,4!
   )
)

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A

   REM COMPARE THIS FILE VS. THE CORRESPONDING ONE (SAME PATH) PATCHER
   FOR %%B IN ("!FileName:Resclient=Patcher!.GZ") do (
      SET Compare=%%~TB
      SET FileNameCompare=%%B
      if !Original! GTR !Compare! ( CALL :NEWER ) ELSE ( CALL :OLDER )
   )

)
GOTO :EOF

:OLDER
echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!
exit /B

:NEWER
REM echo file !FileName! !Original! is newer then file !FileNameCompare! !Compare!
set NewFileName=!FileName:Resclient=Patcher!
e:\Pandoraflyff\Batch\Program\Gzip.exe -9 -k -c -r !FileName! > !NewFileName!.gz
REM echo !NewFileName!.gz
exit /B

Squashman
Expert
Posts: 4483
Joined: 23 Dec 2011 13:59

Re: File Comparison by Date Time

#18 Post by Squashman » 26 May 2014 06:43

Imagination, batch does not know the difference between a date variable or a string variable. Hence the reason for needing to reformat the date into a usable number format: YYYYMMDDHHMM.
If the date and time is not in that format you will never be able to compare the time stamps of two files to detemine which file is older or newer.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: File Comparison by Date Time

#19 Post by Dragokas » 26 May 2014 06:55

imagination, you can preserve Date format. Change it. Than recover after calling a main script.

Code: Select all

For /f "tokens=1-2*" %%a In ('Reg.exe query "HKCU\Control Panel\International" /v "sShortDate"') do set "sShortDate=%%~c"
reg add "HKCU\Control Panel\International" /v "sShortDate" /t REG_SZ /d "M/d/yyyy" /f >NUL

:: main script

reg add "HKCU\Control Panel\International" /v "sShortDate" /t REG_SZ /d "%sShortDate%" /f >NUL

Aacini
Expert
Posts: 1904
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: File Comparison by Date Time

#20 Post by Aacini » 26 May 2014 07:20

If your %%~T modifier gives the date/time in this format: "DD-MM-YYYY HH:MM", then you may directly compare both dates this way:

Code: Select all

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A

   REM COMPARE THIS FILE VS. THE CORRESPONDING ONE (SAME PATH) PATCHER
   FOR %%B IN ("!FileName:Resclient=Patcher!.GZ") do (
      SET Compare=%%~TB
      SET FileNameCompare=%%B
      REM SEPARATE DATE PARTS OF BOTH DATES
      for /F "tokens=1-3,5-7 delims=- " %%a in ("!Original! !Compare!") do (
         REM COMPARE YYYYMMDD IN BOTH DATES
         if %%c%%b%%a GTR %%f%%e%%d ( CALL :NEWER ) ELSE ( CALL :OLDER )
      )
   )

)
GOTO :EOF

You may also insert the time in the comparison adjusting the "tokens=" value and the %%letters used. Please note that if the %%~T gives additional information after the HH:MM (like a.m. or p.m.) it would be necessary to adjust the "tokens=" value accordingly.

Antonio

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: File Comparison by Date Time

#21 Post by Compo » 26 May 2014 14:52

Here's some powershell code which may help lead you in a different direction.

Code: Select all

foreach($f in ls "E:\Pandoraflyff\Patcher\*.gz"){if($f.Lastwritetime -ne $(ls $("E:\Pandoraflyff\Resclient\"+$f.BaseName)).Lastwritetime){write-Host $f.Name has a different timestamp than $f.BaseName}}
I still believe that its easier to use XCopy, (by renaming with matching extensions in both directories first).

Post Reply