GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Read from text file, IF NEW

Hello I would like to read in a text based file if it is newer than the current one that was previously written. Earlier in the script I will create the text based file. I overwrite the text file with a newer one. I want make sure the data written to the file is the newest version. Could someone show an example of a good way to do this?

I know ways this can be done with looping to check when a file is ready. However I do not know the best Opus constructs to determine file times and dates. Any examples appreciated.

Thanks

You can use FSUtil.GetItem to query a file on disk and then use the Item.modify attribute to retrieve its last modified date.

I guess you would store that date in a variable in your script. Then when you want to see if the file has been updated, query it again and compare the dates using the Date.Compare method.

This is super cool to know about. Thank You. I always find that the Opus way works best. That is when in the context of Opus scripts. I like knowing about ways to utilize Opus Scripting functions. Using JavaScript ActiveXObject I was able to use DateLastModified which seem to do the trick. Then wound up comparing it just like you suggest.

This script was the best idea at a way to make VLC Player create a *.m3u playlist from a video. The playlist makes the video jump to users points of interest. The script is run from a *.dcf because it is launched via automation (another program via the command line). VLC Player's HTTP request feature can store XML of the video information. The HTTP request here is handled with WGET and then it stores the XML file. This case the file holds the time code when the Opus Script is launched. I tell all this in case other users want to accomplish something similar with other programs and HTTP requests. The text based file here that Opus helps with is status.xml.

function fromAnywhere() {

	cmm = DOpus.Create.Command
	cmm.AddLine('@admin')
	cmm.AddLine('@externalonly')	
	var wg = "C:\\wget\\wg.bat"
	var rdr = "C:\\test3\\status.xml"
	var nwp = "C:\\Users\\Chris\\AppData\\Roaming\\vlc\\nowplaying.txt"
	var m3u = "C:\\test3\\goodTry.m3u"

	firstDate = createDate(rdr)
	DOpus.Output("1: " + firstDate)

	cmm.AddLine(wg)
	cmm.SetModifier("runmode", "hide")
	cmm.Run()

	var i = 1
	while ( i )
	{

		secondDate = createDate(rdr)
		DOpus.Output("2: " + secondDate)
		i++
		if ( secondDate > firstDate )
		{
			tel = readFile(rdr)
			DOpus.Output (String(tel))
			vlct = getTime(tel)
			vlcn = getPath(nwp)
			DOpus.Output(vlct)
			DOpus.Output(vlcn)
			createM3u (vlct, vlcn, m3u)
			i = 0;
		}

	}

}

function createDate (dir) {
	myObject = new ActiveXObject("Scripting.FileSystemObject")
	var f = myObject.GetFile(dir)
	var dat = f.DateLastModified
	return dat
}

function readFile (dir) {
	var fs = new ActiveXObject("Scripting.FileSystemObject")
	var frd = fs.OpenTextFile(dir, 1)
	var i = 1
	while (i) {
		if (!frd.AtEndOfStream) {
			var rdd = frd.ReadAll()
			frd.Close()
			i = 0
			return rdd
		} 
	}
}

function getTime (txt) {
	var trd  = /<time>.+<\/time>/
	result = trd.exec(txt)
	nst = String(result).replace("<time>", "")
	bst = nst.replace("</time>", "")
	return bst
}

function getPath (file) {
	var fs = new ActiveXObject("Scripting.FileSystemObject")
	var frd = fs.OpenTextFile(file, 1)
	var rdd = frd.ReadAll()
	return rdd
	frd.Close()
}


function createM3u (time, dir, path) {
	var fs = new ActiveXObject("Scripting.FileSystemObject")
	var file = fs.OpenTextFile(path, 8, true)
	file.WriteLine("#EXTVLCOPT:start-time="+time);
	var ndr =String(dir)
	mdr = ndr.replace(/\//g, "\\").slice(1)
	file.WriteLine(mdr);	
}
	
fromAnywhere()