Knowing if log file is in use


Recommended Posts


I want to write a script to ftp all the log files in a directory to our ftp server - no problem there. I intend to use the GetFileNameList to get a list of names and then to ftp them in sequence and as each file is successfully transferred to move the file to an archive directory using File.MoveFile.

The potential problem with this scheme is that the present logging file will be open and shouldn't be ftp'ed or moved.

Is there any way to know if a file is locked?

One of the reasons to do it this way is that if any files are missed one day e.g. the ftp server is down, the internet link is down etc. then they will just be picked up the next day. If all goes to plan there should be only one file per day to transfer - I have my logging file created on a daily basis.


Link to comment
Share on other sites


well, didn't quite work as expected. My logging setup has a base file of "C:\Tanks\Tank.txt" but I have asked for it to add on the time so I get a file like "Tank_090330_112324.txt".

When I use the code below to get the file name it gives me the name as "C:\Tanks\Tank.txt" without the time portion.

My idea here is that I want to ftp any data files that have been closed to my ftp server, as each one is ftp'ed I want to copy it to an archive directory - this all works, I have just commented it out here. The present file will be open so I cannot ftp that file so want to skip it. Due to Windows way of getting file names the latest file could be the first in the list or the last or anywhere in fact.

Another thing, is it possible to get the full year as part of the file name - after year 2000 problems I now prefer to work with 4 characters for the year always.


ftp.strServer = "xxx"
ftp.strUserName = "xxx"
ftp.strPassword = "xxx"

if (NumRows(private.strMyFiles) >1)
	file.Write(private.fHandle, FormatDateTime("%d/%m/%Y",systime()))
//	for (private.Count=0, private.count<NumRows(private.strMyFiles),private.count++)
//	   ftp.strLocalFile = "c:\tanks\" + private.strMyFiles(private.count)
//	   if (Logging.datalog.strFileName != "c:\tanks\" + private.strMyFiles(private.count))
//		  ftp.strRemoteFile = "/incoming/RuakuraTanks/" + private.strMyFiles(private.count)
//		  file.Write(private.fHandle,ftp.strRemoteFile)   
//		  ftp.Upload()   
//		  if (ftp.BytesTransfered == -1)
//			 File.MoveFile("c:\tanks\" + private.strMyFiles(private.count),"c:\tanks\archive\" + private.strMyFiles(private.count))   
//		  Endif
//	   Endif
//	endfor   

Link to comment
Share on other sites

I'm not really sure I understand the problem. Where are you setting the date to the end of the filename? Maybe you can post an example of what strMyFiles and logging.datalog.strFileName contain.

As for four digit year, if you are using formatdatetime() there is a specifier for the four digit year. It is %Y (Y in caps). Doing lower case: %y gives you just the 2 digit year.

Link to comment
Share on other sites


A couple of screen shots provided of my logging setup.

On the Main tab I have set the file name as C:\Tanks\Tanks.txt on the details tab I have ticked the "Use Date in File Name" box on the "Details" tab - second image. The system logs every 15 minutes and creates a new file every day.

I presumed DaqFactory had added the date and time parts to the data files.

Outut to my log file is - with my notes underlined

Note - this is a log before I tried to counter the problem of trying to ftp the file in use







Note - this is the code as at present, it counts the number of output files, adds todays date and then outputs the name of the present log file




Hope this helps you understand my problem.




Link to comment
Share on other sites

Gotcha. Don't use AutoSplit files for this. For one thing, it will only work the way you expect if you happen to start the logging set at midnight. If you start it midday, each of your daily log files will contain some data from one day and some from the next. Autosplit files is designed for people that are running in environments where harddrive crashes are more likely (airplanes mostly). By splitting the log files into small files, you lower the chance of losing large chunks of data in a harddrive crash.

Instead, you should change the log filename yourself from script. This is talked about it elsewhere (forum, docs?) and so I assumed you were using this method. Create a sequence that runs all the time and has a priority of 0:

   logging.datalog.strFileName = formatdatetime("C:\tanks\tanks%d%m%Y.txt",systime())

Look up the formatdatetime() function in the expression chapter to see how you can format your date. This is day, month, and 4 digit year. The script constantly updates the filename of the logging set based on the current time, but since the result of the function only changes once a day, you get a new logging file once a day. The logging set automatically closes the log file and opens another whenever the strFileName variable changes.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.