Hi. I have this code below that creates a new file with same filename and extension of the selected file + a custom suffix, then it writes metadata information to the file itself.
I'm only missing the code to get the new file have the same timestamps of the source file.
Can you please help me? ![]()
Here is the code, also feel free to suggest improvements ![]()
// clone selected file
function OnClick(clickData) {
var suffix = " (Clone)";
var fsu = DOpus.FSUtil;
var tab = clickData.func.sourcetab;
var sel = new Enumerator(tab.selected_files);
// auto-size
function hrSize(bytes) {
if (bytes >= 1e9) return (bytes/1e9).toFixed(2) + " GB";
if (bytes >= 1e6) return (bytes/1e6).toFixed(2) + " MB";
if (bytes >= 1e3) return (bytes/1e3).toFixed(2) + " KB";
return bytes + " bytes";
}
// format date
function fmtDate(d) {
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var dd = ('0' + d.getDate()).slice(-2);
var mm = ('0' + (d.getMonth()+1)).slice(-2);
var yyyy = d.getFullYear();
var day = days[d.getDay()];
var hh = ('0'+d.getHours()).slice(-2);
var mi = ('0'+d.getMinutes()).slice(-2);
var ss = ('0'+d.getSeconds()).slice(-2);
return dd + "." + mm + "." + yyyy + ", " + day + ", " + hh + ":" + mi + ":" + ss;
}
for (; !sel.atEnd(); sel.moveNext()) {
var item = sel.item();
var origPath = item.realpath;
var origName = item.name;
var newName = origName + suffix;
var newPath = fsu.Resolve(tab.path + "\\" + newName);
// collect metadata
var size = item.size;
var created = new Date(item.create);
var modified = new Date(item.modify);
var accessed = new Date(item.access);
var attrs = item.attr;
var now = new Date();
// write metadata
var info = [];
info.push("==============================");
info.push("Cloned File Information");
info.push("==============================");
info.push("");
info.push("File Name : " + origName);
info.push("File Path : " + origPath);
info.push("File Size : " + size + " bytes (" + hrSize(size) + ")");
info.push("File Attributes : " + attrs);
info.push("Created : " + fmtDate(created));
info.push("Modified : " + fmtDate(modified));
info.push("Accessed : " + fmtDate(accessed));
info.push("");
info.push("Cloned At : " + fmtDate(now));
info.push("");
info.push("==============================");
info.push("");
// write text file
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(newPath, true, true); // overwrite, unicode
file.WriteLine(info.join("\r\n"));
file.Close();
DOpus.Output("Created clone: " + newName);
}
}