EmEditor (text editor) Forum Index
   Macro Samples
     Learning by doing: Save current file with an timestamp as name
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
Stefan
Posted on: 11/1/2011 12:13 pm
Home away from home
Joined: 7/14/2008
From: Germany, EU
Posts: 285
Learning by doing: Save current file with an timestamp as name
Best to learn is to do some lessons.
So i will show my steps creating an macro for others.

I am not saying that this is the best way to do it, it is just an start.

If you have an better way to do it, or other suggestions, just reply



Purpose:
Save the current file with an timestamp as part of the name
to have the current version of your code saved in between.

For example open "LIZENZ.txt" and execute this macro
to get "LIZENZ_2011,10,01-21,12.txt" too in same folder.

But "LIZENZ.txt" will still be the current open file!


Handling:
Save this an EmEditor macro as *.jsee (JavaScript)
or add it as an macro snippet into the snippet plugin
and add an shortcut just as Ctrl+.



Code:

//// Purpose: save current file with an timestamp in name
//// That way i can save versions of the current file at easy.

/////////////////////////////////////////////////////////////////////
///////////////////// Script:

////Pause update of the view till script is finished:
Redraw = false;

////Put original full file name incl. path into var "strOrig":
strOrig = document.FullName;

////Store the path separately :
strPath = document.Path; //But as i see we don't need this info.

////Calculate Base name and Extension and an timestamp as separate values:
strBase = fBaseName(document.Name);
strExte = fNameExte(document.Name);
strDate = fCurrDateTime(); 


////Compose the new name incl. timestamp:
//strNewName = strPath + "\\" +  strBase + "_" + strDate + "." + strExte;
//strNewName =  strBase + "_(Version-" + strDate + ")." + strExte;
strNewName =  strBase + "_" + strDate + "." + strExte;

////Save the file with this new name:
document.Save(strNewName);

////Open the file with the last name again:
editor.OpenFile( strOrig );


/////////////////////////////////////////////////////////////////////
///////////////////// Functions:

function fBaseName(str)
{
   if(str.lastIndexOf(".") != -1)       
       base = str.substring(0, str.lastIndexOf("."));
   return base;
}

function fNameExte(str)
{
   if(str.lastIndexOf(".") != -1)       
       exten = str.substring(str.lastIndexOf(".")+1);
   return exten;
}


function fCurrDateTime()
{
  var currentDate = new Date();
  var day = currentDate.getDate();
  if (day < 10) day = "0" + day;
  var month = currentDate.getMonth() +1;
  if (month < 10) month = "0" + month;
  var year = currentDate.getFullYear();
  vDate = year + "," + month + "," + day;

  var currentTime = new Date();
  var hours = currentTime.getHours();
  var minutes = currentTime.getMinutes();
  if (minutes < 10)  minutes = "0" + minutes;
  vTime = hours + "," + minutes;
  return vDate + "-" + vTime;
}
Threaded | Newest First Previous Topic | Next Topic | Top


Register To Post
 
English čeština Deutsch español français italiano 日本語 한국어 Русский 简体中文 繁體中文