Tapatalk

Active Document date & time

Active Document date & time

7
NewbieNewbie
7

    Nov 28, 2007#1

    I want to use an UltraEdit java script to do a save as, but I want it to abort if the file being overwritten is newer than a particular timestamp.

    I cannot find a way to retrieve the date and time of the active (or any other) file. Am I missing something?

    Is there a way to create a new "Scripting.FileSystemObject" and work with that? My few attempts failed.

    Is it possible to invoke UltraCompare from within a script? That would be cool too.

    --Vorpal

    6,685587
    Grand MasterGrand Master
    6,685587

      Re: Active Document date & time

      Nov 28, 2007#2

      You can define a user tool which calls DOS command dir with the full file name of interest and capture the output which you then can scan for the file date/time.

      For your UltraCompare question see Open UCL from script.
      Best regards from an UC/UE/UES for Windows user from Austria

      262
      MasterMaster
      262

        Dec 01, 2007#3

        Hi Vorpal

        Did you succeed finding a solution from the hint given by Mofi ?

        Just as an excercise (I'm funny that way ;-) ) I decided to try this out and have made the script below. Read the comments carefully as they explain how to set up a user tool.

        I assumed the file whose timestamp we want to check isn't a file already open in UE. Otherwise I could have configured the user tool with %F instead of %sel%.

        The solution isn't pretty but lacking this kind of access to the file system in the Javascript language (per definition) and UltraEdit-main object, a user tool using DOS DIR command is the only way to retrieve information of file timestamps, sizes and other attributes (ie. write protected) from the file system into UE.

        Code: Select all

        // No warrenty. Use at own risk. Modify freely. Improvements welcome. Share with others
        
        // We are going to retrieve the the date and time this file was last saved:
        var wrkFileName = "C:\\temp\\temp.txt";
        
        // Support function used later on: remove starting and ending / as UE does not recognize these for regexp
        RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); }; 
        
        // get the timestamp as a javascript Date object. getFileTimestamp is the central function.
        var fileTimestamp = getFileTimestamp(wrkFileName); 
        
        // If not null = timestamp extracted/file is found on file system:
        if (fileTimestamp) {
        	// Write timestamp - do your own formatting 
        	// - see http://www.w3schools.com/jsref/jsref_obj_date.asp
        	// - and see http://www.svendtofte.com/code/date_format/
        	UltraEdit.messageBox("File "+wrkFileName+" last saved: "+fileTimestamp.toString(),"getFileTimestamp");
        }
        else {
        	UltraEdit.messageBox("File "+wrkFileName+" not found","getFileTimestamp");
        }
        
        // ---------------------------------------------------------
        function getFileTimestamp(inFileName) {
        	// This function will return a timestamp (Date() object) of the input file or Null 
        	// if file is not found
        	
        	// Save active file index
        	var wrkActiveFileIndex = getActiveDocumentIndex();
        
        	// Configure a user tool in : Advanced\Tool configuration with the folling setup:
        	//
        	// Menu item name = DIRtimestamp
        	// Command line = DIR %sel%
        	// Options tab: DOS Program, uncheck 'save active file' and 'save all files first'
        	// Output tab: 'Capture output' checked.
        
        	// Now write the filename into the active file and select:
        	UltraEdit.activeDocument.top();
        	UltraEdit.activeDocument.write(inFileName);
        	UltraEdit.activeDocument.selectToTop();
        	// Now run user tool DIRtimestamp
        	UltraEdit.runTool("DIRtimestamp");
        	//
        	// Parse output according to local date format (important!!):
        	//
        	//  Indhold af C:\temp
        	//
        	// 19-09-2007  21:55            34.790 temp.txt
        
        	// setup perl regexp for finding first occurence of date/time:
        	// pattern is expected to be d-m-Y H:i
        	var findTimestampRe = /(\d\d)-(\d\d)-(\d\d\d\d)\s+(\d\d):(\d\d)/;
        	UltraEdit.perlReOn();
        	UltraEdit.activeDocument.findReplace.regExp=true;
        	UltraEdit.activeDocument.findReplace.find(findTimestampRe.toUEregexp());
        
        	if (! UltraEdit.activeDocument.isFound()) {
        		UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
        		UltraEdit.activeDocument.deleteText(); /* delete inFileName */
        		return; /* no return value = null */
        	}
        
        	// get selected timestamp
        	var wrkTimestampText = UltraEdit.activeDocument.selection;
        
        	// now parse timestamp with perl regexp and destructuring assign 
        	var [, wDay,wMonth,wYear,wHours,wMinutes] = findTimestampRe.exec(wrkTimestampText);
        
        	// Assign the parsed/extracted data to a date object:
        	var outDate = new Date();
        	outDate.setYear(wYear);
        	outDate.setMonth(wMonth - 1); /* Javascript month numbers start from 0 = Jan */
        	outDate.setDate(wDay);
        	outDate.setHours(wHours);
        	outDate.setMinutes(wMinutes);
        	outDate.setSeconds(0); /* We do not get seconds from DIR command output */
        
        	// Close output from DIR user tool
        	UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
        
        	// Restore focus on previously active file:
        	UltraEdit.document[wrkActiveFileIndex].setActive();
        
        	// Remove the inFileName again which is still selected
        	UltraEdit.activeDocument.deleteText();
        
        	return outDate;
        }
        
        // ---------------------------------------------------------
        /* Find the tab index of the active document */
        function getActiveDocumentIndex() {
        	var tabindex = -1; /* start value */
        
        	for (i = 0; i < UltraEdit.document.length; i++)
        	{
        		if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
        			tabindex = i;
        			break;
        		}
        	}
        	return tabindex;
        }