Click an area from the above list to jump directly to that section.
001. :HEADER
[Script Information Block]
002. :
*****************************************************************************
003. : * !PROTECTED SCRIPT!
(Copyright 1995-2004, JWC Computer Communications) *
004. :
*****************************************************************************
005. : * This Command Script is
protected domestically by the laws of the United *
006. : * States, and
internationally by the Berne Convention (over 130 countries *
007. : * participating). The
packaging and presentation of this script is a Work, *
008. : * protected in it's
entirety. The full script, or any portion thereof, may *
009. : * not be executed,
reproduced or redistributed except as authorized in the *
010. : * license. See
http://TheSystemGuard.com/license.htm for complete license. *
011. : * THIS NOTICE MAY BE
APPENDED TO BY LICENSED USERS BUT MAY NOT BE REMOVED. *
012. :
*****************************************************************************
013. : * -> Append additional
notices regarding upgrades or modifications here. <- *
014. : * yyyymmdd Replace this
example with your notice(s). *
015. :
*****************************************************************************
016. :
=============================================================================
017. : -> TSG_Snapshot.cmd
2004.04.24 (create a backup whenever a file changes)
018. :
=============================================================================
019. : File
Name.............TSG_Snapshot.cmd
020. : File
Location.........PlaceLocalPathHere
021. : Coded
By..............TheSystemGuard
022. : Service
Date..........2004.04.24
023. : Last Revised
By.......TheSystemGuard
024. : Revision
Date.........2004.04.24
025. :
=============================================================================
026. : >>> DO NOT CHANGE THE
SPACING OR FORMATTING OF ANYTHING ABOVE THIS LINE!! <<<
027.
028.
:PRELIM [common to all scripts]
029.
030. @(
031.
(ECHO
OFF)
032.
(SETLOCAL
ENABLEEXTENSIONS)
033. )
034.
035.
:SETUP [specific to this script]
036.
037. :* If no parameters or switches are specified, show syntax and exit with EL=1
038.
IF
"%1"==""
GOTO :Syntax
039.
040. :* If the first
parameter contains [/? -? /help -help], show help screen and exit
041.
ECHO:"%1"|
%systemroot%\system32\FindStr.exe
/i "\/? -? \/help
-help" >NUL &&
GOTO :Help
042.
042. :* Initialize
any available Library - brief method
044. @(
045.
(IF
NOT DEFINED #
CALL %cmdlib% /i /q 2>NUL>NUL)
046.
(IF
NOT DEFINED #
CALL NTCmdLib.cmd /i /q 2>NUL>NUL)
047.
(IF
NOT DEFINED #
CALL MtCmds.cmd /i /q 2>NUL>NUL)
048.
(IF
NOT DEFINED #
CALL ntlib.cmd /i /q 2>NUL>NUL)
049. )
050.
051. :* Make sure Command Library is
initialized
052. :! The double "%.rem%
REM"
statements are there to keep the script from
053. :! _ failing
when a library is not loaded (i.e., %.rem%
is undefined)
054.
IF
NOT DEFINED #libName (
055.
(SET
"ErrorMsg=Unable to Initialize Command Library.")
056.
(%.rem%
REM
This section is for NO LIBRARIES FOUND)
057.
(GOTO
:ERROR)
058. )
ELSE (
059.
IF
/I NOT "%#libName%"=="NTCmdLib" (
060.
IF
/I NOT "%#libName%"=="MtCmds" (
061.
IF
/I NOT "%#libName%"=="ntlib" (
062.
(SET
"ErrorMsg=Command Library %#libName% not recognized.")
063.
(%.rem%
REM
This section is for a library is found, but it's not NTCmdLib, MtCmds or ntlib)
064.
(GOTO
:ERROR)
065.
) ELSE (
066.
(%.rem%
REM
This section is for ntlib found)
067.
)
068.
) ELSE (
069.
(%.rem%
REM
This section is for MtCmds found)
070.
)
071.
) ELSE (
072.
(%.rem%
REM
This section is for NTCmdLib found)
073.
)
074. )
075.
076. :! Now
.Mount/\Commands and all other library resources are available!
077.
078. :Bgn_Parse
[parse the command line]
079.
080. :GetSwitches [Process any command line switches]
081.
082.
:! Command line switch processing requires the Expert Command Library
083.
:! _ which contains the Procedure
:Parse_Command_Line_Switches.
084.
085.
:GetParms [Process any parameters]
086.
087. :* Clear/Reinitialize all variables just in case
they were set from previous runs
088.
@(
089.
(SET
"ScriptName=TSG_Snapshot Advanced 2004.04.24")
090.
(SET
"ReportTitle=TSG_Snapshot running on [%COMPUTERNAME%] as [%USERNAME%]")
091.
(SET
FileToSave=)
092.
(SET
SafePlace=)
093.
(SET
Interval=)
094.
(SET
SecondsToWait=)
095.
(SET
ErrorMsg=)
096.
(SET
WindowTitle=)
097.
(SET
Snapshot_Count=0)
098.
(SET
Snapshot_Passes=0)
099.
(SET
Snapshots_Taken=0)
100.
(SET
Snapshots_Missed=0
101.
)
102.
103. :* Retrieve the file to save
104.
SET
"FileToSave=%~fs1"
105.
106. :* If no file was specified, go to the ERROR
section
107.
IF
NOT DEFINED FileToSave (
108.
(SET
"ErrorMsg=No file specified.")
109.
(GOTO
:ERROR)
110.
)
111.
112. :* If the specified file cannot be found, go to the
ERROR section
113.
IF
NOT EXIST %FileToSave% (
114.
(SET
"ErrorMsg=File %FileToSave% does not exist")
115.
(GOTO
:ERROR)
116.
)
117.
118.
SET
"FileDisplayName=%~nx1"
119.
120.
:* Retrieve the name of the folder to store snapshots, if specified
121.
SET
"SafePlace=%2"
122.
123.
:* If no SafePlace was specified, set SafePlace to C:\!Snapshot
124.
IF
NOT DEFINED SafePlace
SET
"SafePlace=C:\!Snapshot"
125.
126.
:* If the SafePlace folder doesn't exist, try to (silently) create it.
127.
IF
NOT EXIST %SafePlace%
%.Silent%
MD %SafePlace%
128.
129.
:* If there was an error creating the folder, go to the ERROR section
130.
%.ifErr% (
131.
(SET
ErrorMsg=Unable to create folder %SafePlace%)
132.
(GOTO
:ERROR)
133.
)
134.
135.
:* If an Interval was specified, try to use it
136.
:! A non-numeric value will evaluate to 0
137.
:! "Keep-It-To-Yourself" (Kity) suppresses any
error message
138.
IF
NOT "%3"=="" (%.Kity%
SET
/A
Interval=%3)
139.
140.
:* If Interval is less then 1 minute (or blank), default to 5 minutes
141.
:! The extra "1" in front of both numbers keeps the
IF statement
142.
:! _ from failing even if %Interval% is not defined.
143.
IF
1%Interval% LSS
11 SET
/A
Interval=5
144.
145. :End_Parse
146.
147.
GOTO
:WORK
148.
149. :Help
150.
151.
@(
152.
(ECHO:
^^ =========================================================================)
153.
(ECHO:
/~\ Snapshot Advanced Release 2004.04.24 ^(Free from TheSystemGuard.com!^))
154.
(ECHO:/cmd\
=========================================================================)
155.
(ECHO:)
156.
(ECHO:
Create a snapshot backup when a file changes.)
157.
(ECHO:_______________________________________________________________________________)
158.
(ECHO:)
159.
(ECHO:TSG_Snapshot
[/?] [drive:][path]FileToProtect [SafeLocation] [Interval])
160.
(ECHO:)
161.
(ECHO:SWITCHES:)
162.
(ECHO:
/? Display this help.)
163.
(ECHO:)
164.
(ECHO:PARAMETERS:
Default values are shown in {}'s)
165.
(ECHO:
FileToProtect A SINGLE file to protect ^(no wildcards!^))
166.
(ECHO:
[SafeLocation] Folder to hold snapshots {C:\!Snapshot})
167.
(ECHO:
[Interval] Minutes between file checks {5})
168.
(ECHO:)
169.
(ECHO:ERRORLEVELS:)
170.
(ECHO:
0 = Normal or expected finish.)
171.
(ECHO:
1 = Help screen was displayed.)
172.
(ECHO:
2 = Script exited with a known error. See %%ErrorMsg%% for details.)
173.
(ECHO:
3 = Script exited with an unspecified error.)
174.
(ECHO:)
175.
(ECHO:NOTES:)
176.
(ECHO:
The SafeLocation folder will be created if necessary.)
177.
(ECHO:)
178.
(ECHO:
TSG_Snapshot will stop monitoring after 10 successful snapshots have)
179.
(ECHO:
been taken, or after 4 hours of waiting time, whichever comes first.)
180.
(ECHO:)
181.
(ECHO:
Interval time DOES NOT include time spent comparing and copying files.)
182.
(ECHO:
The Interval counter is reset at the beginning of each waiting period.)
183.
(ECHO:
Actual wait time is NOT PRECISE, and can overshoot by several seconds,)
184.
(ECHO:
depending upon system load ^(see the
.Wait command^).)
185.
(ECHO:)
186.
(ECHO:
If any optional parameter is used, all optional parameters to it's left MUST)
187.
(ECHO:
be specified. For example, to use Interval, also specify SafeLocation.)
188.
(ECHO:)
189.
)
190.
191.
@(
192.
(ECHO:
All Advanced Scripts ^(those prefixed with "TSG_"^) from TheSystemGuard.com)
193.
(ECHO:
REQUIRE the Advanced NT/2K/XP/K3 Command Library ^(ntlib.cmd^). This library)
194.
(ECHO:
provides over 200 resources for writing, testing and maintaining RELIABLE)
195.
(ECHO:
shell scripts using only the native commands and utilities that are present in)
196.
(ECHO:
ALL NT-based operating systems. Write a script ONCE using ntlib.cmd and your)
197.
(ECHO:
script will perform CONSISTENTLY under Windows NT4, 2000, XP and Server 2003!)
198.
(ECHO:
Request your FREE copy of the Advanced Library today at ^(http://ntlib.com^).)
199.
(ECHO:)
200.
(ECHO:*******************************************************************************)
201.
(ECHO:)
202.
(ECHO:
Snapshot Expert uses the enlarged "Expert Command Library" ^(NTCmdLib.cmd^),)
203.
(ECHO:
enabling many additional command line switches and parameters. All Expert)
204.
(ECHO:
Scripts ^(those prefixed with "!"^) are complete, ready-to-run applications)
205.
(ECHO:
that do not require any commands or utilities not already present in every)
206.
(ECHO:
NT/2K/XP/K3 install. !Scripts are distributed uncompressed and are fully)
207.
(ECHO:
commented so you can quickly fashion them to your specific requirements.)
208.
(ECHO:)
209.
(ECHO:
An example of what you're missing ^(from the help screen of !Snapshot.cmd^):)
210.
(ECHO:)
211.
(ECHO:SWITCHES:
Default values are shown in {}'s)
212.
(ECHO:
/C Compact window during run ^(size restored at Final Report^). {off})
213.
(ECHO:
/FP FullPath ^(use the full path of FileToProtect in window title^) {name.ext})
214.
(ECHO:
/NV NoVisual ^(don't change console colors to reflect operating status^) {on})
215.
(ECHO:
/P Pause ^(pause console after Final Report^) {off})
216.
(ECHO:
/WF WaitforFile ^(bypass error if FileToProtect does not exist^) {off})
217.
(ECHO:)
218.
)
219.
220.
@(
221.
(ECHO:PARAMETERS:
Default values are shown in {}'s)
222.
(ECHO:
[Warning_Trigger] Consecutive missed snapshots before warning. {1})
223.
(ECHO:
[Error_Trigger] Consecutive missed snapshots before error. {2})
224.
(ECHO:
[MaxSnaps] Maximum snapshots to take before stopping. {25})
225.
(ECHO:
[MaxTime] Maximum time ^(in minutes^) to wait. {480} ^(8 hours^))
226.
(ECHO:)
227.
(ECHO:
!Snapshot.cmd provides a color-coded, self-adjusting console window to enable)
228.
(ECHO:
at-a-glance monitoring of all running snapshots while using minimum desktop)
229.
(ECHO:
real estate. We also include a ".reg" file to allow right-click access to the)
230.
(ECHO:
!Snapshot script from inside Windows Explorer. Just highlight any file, then)
231.
(ECHO:
right-click, and select "Protect with !Snapshot".)
232.
(ECHO:)
233.
(ECHO:
Both the Expert Command Library and !Snapshot are "Almost Free" at $9 or less!)
234.
(ECHO:
See ^(http://TheSystemGuard.com/AlmostFree.asp^) for The Almost Free Price List.)
235.
(ECHO:)
236.
(ECHO:
The VERY LATEST KNOWLEDGE pertaining to the Windows NT/2K/XP/K3 command line)
237.
(ECHO:
can be found at TheSystemGuard.com. Check the MasterCatalog often for a)
238.
(ECHO:
continually growing collection ^(http://TheSystemGuard.com/MasterCatalog.asp^).)
239.
(ECHO:
You'll learn to build RELIABLE and MAINTAINABLE shell scripts that will always)
240.
(ECHO:
perform CONSISTENTLY on every Windows NT-based system right out of the box!)
241.
(ECHO:
Don't forget our FREE KnowledgeLetter at ^(http://BoomingOrFuming.com^) and...)
242.
(ECHO:)
243.
(ECHO:
Congratulations On Your Decision To RISE ABOVE THE REST!!!)
244.
)
245.
246. :Syntax
247.
248.
@(
249.
(ECHO:_______________________________________________________________________________)
250.
(ECHO:)
251.
(ECHO:TSG_Snapshot
[/?] [drive:][path]FileToProtect [SafeLocation] [Interval])
252.
(ECHO:)
253.
(MD 2>NUL)
254.
(GOTO
:EOF)
255.
)
256.
257.
:WORK [This is where the job actually gets done]
258.
259. :* Valid
parameters are assumed at this point
260.
261.
:* FileToSave=a specified file that DOES EXIST
262.
:* SafePlace=a specified folder (or "C:\!Snapshot") that DOES EXIST
263.
:* Interval=a specified number of minutes (or the default of 5)
264.
265. :* Convert the
numeric interval to seconds
266. :* Clear the
#LogDate and #LogTime before entering the loop
267. @(
268.
(TITLE
Snapshot [%FileDisplayName%])
269.
(SET
/A
SecondsToWait=Interval*60)
270.
(SET
#LogDate=)
271.
(SET
#LogTime=)
272. )
273.
274. :Bgn_Loop
275.
276. :* Check the file
Latest.FileToSave.Ext in the SafePlace folder
277.
:* _ If it's current, go to the wait loop, if not, make a copy
278.
@(
279.
(TITLE
Snapshot [%FileDisplayName%] {%Snapshots_Taken%} ^(comparing^))
280.
(%.Silent%
(@ECHO:N
|
%.Comp% "%FileToSave%" "%SafePlace%\Latest.%~nx1") &&
GOTO
:Current)
281.
)
282.
283.
:MakeSnap
284.
285.
:* Process any pre-existing snapshots
286.
287.
:! If this IS NOT the first time through the loop, save the previous
288.
:! _ #LogDate and #LogTime, and then (silently) get fresh ones
289.
@(
290.
(IF
DEFINED #LogDate
SET
"PreviousLogDate=%#LogDate%")
291.
(IF
DEFINED #LogTime
SET
"PreviousLogTime=%#LogTime%")
292.
(%.Silent%
( (%.GetLogDate%)&(%.GetLogTime%)
) )
293.
)
294.
295.
:! If this IS the first time through the loop AND there IS a
previous
296.
:! _ "latest" backup, rename it to Before.#LogDate-#LogDate.%~nx1.
297.
:! If this IS NOT the first time through the loop, rename the Latest
298.
:! _ backup to PreviousLogDate-PreviousLogTime.%~nx1.
299.
IF
1%Snapshot_Passes%
LSS 11 (
300.
(%.Silent%
REN
"%SafePlace%\Latest.%~nx1" "%SafePlace%\Before.%#LogDate%-%#LogTime%.%~nx1")
301.
) ELSE (
302.
(%.Silent%
REN
"%SafePlace%\Latest.%~nx1" "%PreviousLogDate%-%PreviousLogTime%.%~nx1")
303.
)
304.
305.
:* Try to take a new snapshot
306.
@(
307.
(TITLE
Snapshot [%FileDisplayName%] {%Snapshots_Taken%} ^(copying^))
308.
(%.Silent%
COPY
"%FileToSave%" "%SafePlace%\Latest.%~nx1")
309.
)
310.
311. :* Update counters
(either good or bad)
312.
%.ifOK% (
313.
(SET
/A
Snapshots_Taken+=1,NoError_Sequence+=1 ,Error_Sequence=0,Skip_Sequence=0)
314.
(%.SetEL0%)
315.
)
ELSE (
316.
(SET
/A
Snapshots_Missed+=1,Error_Sequence+=1,NoError_Sequence=0,Skip_Sequence=0)
317.
(%.SetEL1%)
318.
)
319.
320. :* Update console
message
321.
@(
322.
(
323.
%.ifOK% (
324.
(%.dtEcho%
Snapshot taken. {%Snapshots_Taken%})
325.
)
ELSE (
326.
(%.dtEcho%
Snapshot missed. {%Error_Sequence%})
327.
)
328.
)
329.
(TITLE
Snapshot [%FileDisplayName%] {%Snapshots_Taken%})
330.
(GOTO
:Wait)
331.
)
332.
333. :Current
334.
335.
@(
336.
(SET
/A
Skip_Sequence+=1)
337.
(%.dtEcho%
Snapshot current. {^%%Skip_Sequence^%%})
338.
(GOTO
:Wait)
339.
)
340.
341. :Wait
342.
343.
:* Add one to the passes counter, update the total minutes waited
344.
SET
/A Snapshot_Passes+=1,Total_Wait_Minutes+=Interval
345.
346.
:! Quit after 10 successful snapshots or 60 minutes, whichever comes first
347.
:! If it's not time to quit, update the console message
348.
349.
@(
350.
(IF
1%Snapshots_Taken%
GEQ 110
GOTO
:End_Loop)
351.
(IF
1%Total_Wait_Minutes%
GEQ 160
GOTO
:End_Loop)
352.
(TITLE
Snapshot [%FileDisplayName%] {%Snapshots_Taken%} ^(waiting^))
353.
(%.dtEcho%
Waiting %Interval% minute^(s^)...)
354.
(%.Wait%
%SecondsToWait%)
355.
(GOTO
:Bgn_Loop)
356.
)
357.
358.
:End_Loop
359.
360.
:VERIFY [commands to verify any actions taken]
361.
362.
TITLE
Snapshot [%FileDisplayName%] {%Snapshots_Taken%} (verify)
363.
364. :! Place any
additional verification commands here
365.
366.
:NOTIFY [commands to report what happened]
367.
368. @(
369.
(TITLE
Snapshot [%FileDisplayName%] {%Snapshots_Taken%} ^(notify^))
370.
(%.ReportHeader%)
371.
(ECHO:
Source File: %#TAB%
%~dpnx1)
372.
(ECHO:
Destination Folder: %#TAB%
%SafePlace%)
373.
(ECHO:
Snapshots Taken: %#TAB%
%Snapshots_Taken%)
374.
(ECHO:
Total Minutes Waited: %#TAB%
%Total_Wait_Minutes% minute^(s^))
375. )
376.
377.
:FINISH [cleanup commands for a normal or expected ending]
378.
379. (TITLE
Snapshot [%FileDisplayName%] {%Snapshots_Taken%} ^(finish^))
380.
381. :* If there has
been at least 1 SnapShot Pass, show the finish line
382. @(
383.
(IF
1%Snapshot_Passes%
GTR 10 (%.ReportFooter%))
384.
(%.SetEL0%)
385.
(GOTO
:EOF)
386. )
387.
388. :* Uninstall the
library (removes all traces)
389. :! This is not
recommended unless you will not be using any additional
390. :! _ library
resources during this session. If the script was opened
391. :! _ in a fresh
console window, simply closing the window will remove
392. :! _ the Command
Library, and all of it's resources, from memory.
393. :! %cmdlib%
/uninstall
394. :!
-OR-
395. :*
%.Call%
/uninstall
396.
397.
:ERROR [display any error message and set errorlevels]
398.
399. :! If an error
message is available, display it and exist with EL=2
400. :! If no message
is available, exit with EL=3
401. :! Message is
sent to STDERR, not STDOUT.
402.
403. :* Show error in
title bar and at console via STDERR
404. @(
405.
(TITLE
Snapshot [%FileDisplayName%] {%Snapshots_Taken%} ^(error^))
406.
(%.ErrEcho%)
407.
(
408.
IF
DEFINED ErrorMsg (
409.
(%.ErrEcho% %ErrorMsg%)
410.
(%.ErrEcho%)
411.
(%.SetEL2%)
412.
(GOTO
:EOF)
413.
) ELSE
(
414.
(%.ErrEcho%Script exited with an unspecified error)
415.
(%.ErrEcho%)
416.
(%.SetEL3%)
417.
(GOTO
:EOF)
418.
)
419.
)
420. ) |