Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Batch Script to check date created of 2 exe. If source is newer, run command

Posted on 2016-11-17
8
Medium Priority
?
70 Views
Last Modified: 2016-11-23
I am looking to have a scheduled weekly task to run a script that will compare to .exes . If source is newer than run a setup command.  

It will be used on Windows 10 Pro PCs.
0
Comment
Question by:GGHC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 58

Assisted Solution

by:Bill Prew
Bill Prew earned 1200 total points (awarded by participants)
ID: 41892355
Here's an approach, take a look and see if this makes sense.

@echo off
setlocal

rem Define full paths to the two files to compare
set File1=c:\temp\file1.txt
set File2=c:\temp\file2.txt

rem Determine the newest file of the two
set Newest=
for /f "tokens=*" %%A in ('dir /s /b /o-d "%File1%" "%File2%"') do (
  if not defined Newest set Newest=%%~A
)

rem Perform logic when file1 is the newest
if /i "%File1%" EQU "%Newest%" (
  rem LOGIC FOR WHEN FILE1 IS NEWEST HERE
)

Open in new window

~bp
1
 
LVL 43

Expert Comment

by:Steve Knight
ID: 41892624
That sounds good.  I was thinking of suggesting xcopy before I saw Bill had already responded.  As you can use xcopy to copy a file if newer, I was thinking of using /L option to not copy but just say if it needed to copy, i.e.
xcopy /d /l /y file2.exe file1.exe

Open in new window

If file2.exe is newer than file1.exe then it will show the filename and 1 File(s) otherwise 0 File(s)

You could use that inside a script if wanted along the lines of:

xcopy /d /l /y file2.exe file1.exe | find /v "0 File" > NUL

Open in new window


The errorlevel from that of 1 would mean the files are the same, errorlevel 0 means do the copy

xcopy /d /l /y file2.exe file1.exe | find /v "0 File" > NUL && ECHO NEED TO COPY NEWER FILE

Open in new window

or
xcopy /d /l /y file2.exe file1.exe | find /v "0 File" > NUL
if errorlevel 0 ECHO NEED TO COP NEWER FILE

Open in new window


Bill's technique looks neater but that should work too if needed.

Steve
1
 

Author Comment

by:GGHC
ID: 41892785
Nice!
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 800 total points (awarded by participants)
ID: 41892809
Bill,

Does that work for you if the files are in different directories?  When I tried it seems to treat the /b output the same as the non-bare output, i.e. just report the first file then the second file regardless of order IF they are in separate dirs?

That may not be an issue if the two are in the same dir anyway for the requirement but doesn't work for me?

Steve

Using:
set File1=c:\users\stephen\1\x.txt
set File2=c:\users\stephen\2\x.txt
C:\Users\stephen>checkages
FILE 1 - c:\users\stephen\1\x.txt is newest

FILE1: c:\users\stephen\1\x.txt      18/11/2016  13:05            16,724 x.txt
FILE2: c:\users\stephen\2\x.txt      18/11/2016  13:12                88 x.txt

Open in new window



Steve
0
 
LVL 58

Accepted Solution

by:
Bill Prew earned 1200 total points (awarded by participants)
ID: 41892972
Good eye Steve, it's too bad DIR isn't smart enough to sort across multiple locations, argh.

So then, this should cover that possibility.  I would also recommend adding additional logic in the main script that checks that both files exist before using this logic.  Since it wasn't stated if that condition could exist and what action to take I won't include it here.

I'm going to reopen this question, I would suggest the poster accept this solution after testing, and also give an assist to Steve for catching a possible use case that could be a problem with the original script.

@echo off
setlocal

rem Define full paths to the two files to compare
set File1=b:\ee\temp\d1\file1.txt
set File2=b:\ee\temp\d2\file2.txt

rem Determine the newest file of the two
for /F "tokens=1" %%A in ('xcopy /DHYL "%File1%" "%File2%" ^| findstr /I "File"') do (
  if "%%A"=="1" (set Newest=%File1%) else (set Newest=%File2%)
)

rem Perform logic when file1 is the newest
if /i "%File1%" EQU "%Newest%" (
  rem LOGIC FOR WHEN FILE1 IS NEWEST HERE
)

Open in new window

~bp
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 41893035
Change point split to:

    Bill Prew's comment #a41892972 (200 points)
    Bill Prew's comment #a41892355 (100 points)
    Steve Knight's comment #a41892624 (200 points)

~bp
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question