Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Using string as a NewFile() parameter


Renan Mar 19, 2014 12:31 PM

Hello guys.

I'm working on a program that need to create some txt files under certain conditions. So I'm using the NewFile instruction to create a file, and then the FileOpen/FileWrite/FileClose process to write on that.

My problem is I need to use a variable as the FileName parameter of the NewFile instruction ('cause I need to include some information in the file name, like the Station Name and Timestamp), and the CRBasic looks to accept just "Quoted Text" as FileName.

Somebody knows how I can to create dinamic Filenames despite this limitation?

Thanks all!


Dana Mar 20, 2014 08:20 PM

I don't think NewFile is what you need -- NewFile is a function that monitors a filename to see if a new file has been written with that name.

I think you can do what you need using a variable in FileOpen. e.g.,

FileName="USR:"+Status.StationName+"_"+"MyLogger"+"_"+Time(3)+"_"+Time(2)+"_"+Time(1)

This will create a filename: USR:Dana_MyLogger_20_3_2014

("Dana" is the station name in my logger).

"Filename" can then be used in the FileOpen instruction:

OpenFile = FileOpen (FileName,"a",SeekPoint)

A file Dana_MyLogger_20_3_2014 will be written to the USR Drive of the datalogger.

You could also use that long string itself in the FileOpen instruction rather than using the FileName variable, but that gets pretty messy to read.

My test program is copied below. I think you should be able to run this "as is" in your CR1000, if you want. Just set Flag(1) in the datalogger to trigger the file write.

Hope this helps,

Dana

Public PTemp, Temp, FileName As String * 30
Public OpenFile, SeekPoint
Public Flag(1) As Boolean, Counter
Public Time(9)

BeginProg
SetStatus ("USRDriveSize",16384)

Scan (1,Sec,3,0)
RealTime (Time())
PanelTemp (PTemp,250)
TCDiff (Temp,1,mV2_5,1,TypeT,PTemp,True ,0,250,1.0,0)
FileName="USR:"+Status.StationName+"_"+"MyLogger"+"_"+Time(3)+"_"+Time(2)+"_"+Time(1)
If Flag(1) Then
OpenFile = FileOpen (FileName,"a",SeekPoint)
Do Until Counter = 10
SeekPoint=SeekPoint + FileWrite (OpenFile,"The temperature is ",0)
SeekPoint=SeekPoint + FileWrite (OpenFile,Temp +" degrees",0)
SeekPoint=SeekPoint + FileWrite (OpenFile,CHR(13)+ CHR(10),0)
Counter=Counter+1
Loop
FileClose (OpenFile)
Counter=0
Flag(1) = False
EndIf
NextScan
EndProg

Log in or register to post/reply in the forum.