Skip to content

Instantly share code, notes, and snippets.

@nelix
Forked from hiroyukim/AutoBackup.wsh
Created July 7, 2009 17:11
Show Gist options
  • Save nelix/142221 to your computer and use it in GitHub Desktop.
Save nelix/142221 to your computer and use it in GitHub Desktop.
// TARGET.pid のファイルを削除で終了
if ( WScript.Count == 0 ) {
WScript.Echo("対象ファイルをD&Dしてください");
WScript.Quit();
}
var TARGET = WScript.Arguments(0);
// バックアップするかチェックする間隔(ミリ秒)
var PERIOD = 60 * 1000;
var AutoBackup = {
"run": function(file) {
var fs = WScript.CreateObject("Scripting.FileSystemObject");
if (! fs.FileExists(file) ) {
WScript.Echo(file + "is not exist!!");
WScript.Quit();
}
var base = fs.GetParentFolderName(file) + "\\" + fs.GetBaseName(file);
var ext = fs.GetExtensionName(file);
var now = new Date();
var copy_to = base + "_" + this.format_date(now) + "." + ext;
if (! fs.FileExists(copy_to) ) {
fs.CopyFile(file, copy_to);
}
},
"format_date": function(date) {
var year = this.padding_zero(date.getFullYear(), 4);
var month = this.padding_zero(date.getMonth() + 1, 2);
var day = this.padding_zero(date.getDate(), 2);
var hour = this.padding_zero(date.getHours(), 2);
var min = this.padding_zero(date.getMinutes(), 2);
var sec = this.padding_zero(date.getSeconds(), 2);
return ( year + month + day + "_" + hour + min + sec );
},
"padding_zero": function(num, length) {
num += "";
while ( num.length < length ) {
num = "0" + num;
}
return "" + num;
},
"assert_equal": function(expected, got, test_name) {
if ( expected != got ) {
WScript.Echo("failed test: " + test_name + " ( expected " + expected + ", but got " + got + ")");
}
}
};
var DEBUG=0;
if ( DEBUG ) {
// testing padding_zero
AutoBackup.assert_equal("07", AutoBackup.padding_zero(7, 2), "padding test");
var dt = new Date(2009, 8, 1, 1, 2, 3);
AutoBackup.assert_equal("20090901_010203", AutoBackup.format_date(dt), "format_date");
} else {
var fs = WScript.CreateObject("Scripting.FileSystemObject");
var pid_file = fs.GetParentFolderName(TARGET) + "\\" + fs.GetBaseName(TARGET) + ".pid";
if ( !fs.FileExists(pid_file) ) {
var pid = fs.CreateTextFile(pid_file);
pid.WriteLine("dummy");
pid.Close();
}
var lastmodified = "";
while (1) {
if ( !fs.FileExists(pid_file) ) {
WScript.Quit();
}
var f = fs.GetFile(TARGET);
var lm = f.DateLastModified + ""; // どうもそのまま比較はどうもダメらしい
if ( !lastmodified || ( lm != lastmodified ) ) {
WScript.Echo(lm + " " + lastmodified );
AutoBackup.run(TARGET);
}
lastmodified = lm;
WScript.Sleep(PERIOD);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment