1@echo off
2:: usage: ifchange target temporary
3
4set timestamp=
5:optloop
6for %%I in (%1) do set opt=%%~I
7if "%opt%" == "--timestamp" (
8    set timestamp=.
9    shift
10    goto :optloop
11) else if "%opt:~0,12%" == "--timestamp=" (
12    set timestamp=%opt:~12%
13    shift
14    goto :optloop
15)
16if "%opt%" == "" goto :end
17
18set dest=%1
19set src=%2
20set dest=%dest:/=\%
21set src=%src:/=\%
22if not "%dest%" == "" if not "%dest%" == "%%dest:/=\%%" goto :nt
23
24if not exist %2 goto :end
25
26:: check if fc.exe works.
27echo foo > conftst1.tmp
28echo bar > conftst2.tmp
29fc.exe conftst1.tmp conftst2.tmp > nul
30if not errorlevel 1 goto :brokenfc
31del conftst1.tmp > nul
32del conftst2.tmp > nul
33
34:: target does not exist or new file differs from it.
35if not exist %1 goto :update
36fc.exe %1 %2 > nul
37if errorlevel 1 goto :update
38
39:unchange
40echo %1 unchanged.
41del %2
42goto :end
43
44:brokenfc
45del conftest1.tmp > nul
46del conftest2.tmp > nul
47echo FC.EXE does not work properly.
48echo assuming %1 should be changed.
49
50:update
51echo %1 updated.
52:: if exist %1 del %1
53dir /b %2
54copy %2 %1
55del %2
56goto :end
57
58:nt
59if not exist %src% goto :end
60if exist %dest% (
61    fc.exe %dest% %src% > nul && (
62	echo %1 unchanged.
63	del %src%
64	goto :nt_end
65    )
66)
67echo %1 updated.
68copy %src% %dest% > nul
69del %src%
70
71:nt_end
72if "%timestamp%" == "" goto :end
73    if "%timestamp%" == "." (
74        for %%I in ("%dest%") do set timestamp=%%~dpI.time.%%~nxI
75    )
76    goto :end > "%timestamp%"
77:end
78