Rename file using date at the end of the string. My code is included.

I’m currently writing timestamped data to a text file. Then, pulling the data from the text file for an excel spreadsheet. I would like to be able to use a RESET event routine to save that text file with a new name using the current date at the end of the name. Am I able to do this? rSaveAndClear is the proc I would like to be triggered by the event. Ideally, I copy/rename the current text file (17_ALLI_0192.txt), add the current date to the end (17_ALLI_0192_20170531.txt) and remove all data from the old one.

PERS num nGoodCycles;

VAR String Date;
PERS num WeekDay;
PERS num Hour;
PERS num Minute;
PERS num Second;
VAR iodev logfile;

PROC rGoodCycleCount()
Incr nGoodCycles;
Date:=CDate();
WeekDay:=GetTime(\WDay);
Hour:=GetTime(\Hour);
Minute:=GetTime(\Min);
Second:=GetTime(\Sec);
rWriteGoodCycleDayShift;
RETURN;
ENDPROC

PROC rWriteGoodCycleDayShift()
Open “HOME:/” \File:= “17_ALLI_0192.txt”, logfile\append;
Write logfile, “GOOD CYCLE”\nonewline;
Write logfile, “"+Date\nonewline;
Write logfile, "
”\Num:=Hour\nonewline;
Write logfile, “:”\Num:=Minute\nonewline;
Write logfile, “:”\Num:=Second;
Close logfile;
RETURN;
ENDPROC

PROC rSaveAndClear()
RenameFile “Home:/17_ALLI_0192.txt”, “Home:/17_ALLI_0192_JOB_END.txt”; <--------Can I add in the date during the rename???
Open “HOME:/” \File:= “17_ALLI_0192.txt”, logfile\write;
Write logfile, " ";
Close logfile;
RETURN;
ENDPROC

I’m open to making new directories or an adjacent method that accomplishes the goal.

Hi

You already have a variable with the date so you can just concatenate that on the end of your new file name - as below:

RenameFile “Home:/17_ALLI_0192.txt”,“Home:/17_ALLI_0192_JOB_END.txt”+date;

Regards

Graeme

Thank you for the response Graeme!
I used the code you provided and it didn’t quite work as anticipated.
It only added +Date to the end.

-Rick

Graeme!
You used a word (concatenate) I hadn’t heard before.
After searching the manuals for this word I discovered what you were saying.
I changed my code in the following way:

PROC rSaveAndClear()
Date:=CDate();<-----I was missing this to request the date again
RenameFile “Home:/17_ALLI_0192.txt”, “Home:/17_ALLI_0192_”+Date+“.txt”;<—I had to change to this format to get the file to save correctly.
Open “HOME:/” \File:= “17_ALLI_0192.txt”, logfile\write;
Write logfile, " ";
Close logfile;
RETURN;
ENDPROC

Thank you very much for your response!
My life has just been made much easier!

Hello,

I am doing this same thing.

and I haven’t been able to load or unload a “.txt”, “.Doc” or a “.Log” through my network connection. Is it possible to load or unload these files from the Home directory?

Thanks in advance.

This is the code I’ve been using to write to a “*.txt” file in my HOME directory.

PERS num nGoodCycles;

PERS num Hour;
PERS num Minute;
PERS num Second;

PROC rGoodCycleCount()
Incr nGoodCycles;
Date:=CDate();
WeekDay:=GetTime(\WDay);
Hour:=GetTime(\Hour);
Minute:=GetTime(\Min);
Second:=GetTime(\Sec);
rWriteGoodCycle;
RETURN;
ENDPROC

PROC rWriteGoodCycle()
Open “HOME:/” \File:= “17_BORG_3027.txt”, logfile\append;
Write logfile, “1”\nonewline;
Write logfile, “\t”+Date\nonewline;
Write logfile, “\t”\Num:=Hour\nonewline;
Write logfile, “:”\Num:=Minute\nonewline;
Write logfile, “:”\Num:=Second;
Close logfile;
RETURN;
ENDPROC

I also use this code in my system module. I trigger it with an event routine RESET:rSaveAndClearEvent

VAR String Date;
VAR iodev logfile;
VAR dir directory;
VAR string filename;
VAR string name;
VAR num nNameLength;
VAR num nStringLength;
VAR string part;

PROC rSaveAndClearEvent()
rSearchDir “HOME:/TEMPDATA”,“rSaveAndClear”;
ENDPROC

PROC rSearchDir(string dirname, string actionproc)
IF IsFile(dirname \Directory) THEN
OpenDir directory, dirname;
WHILE ReadDir(directory, filename) DO
! .. and . is the parent and resp. this directory
IF filename <> “..” AND filename <> “.” THEN
rSearchDir dirname+“/”+filename, actionproc;
ENDIF
ENDWHILE
CloseDir directory;
ELSE
%actionproc% dirname;
ENDIF
ERROR
RAISE;
ENDPROC

PROC rSaveAndClear(string filename)
IF FileSize(filename) > 1024 THEN
Date:=CDate();
nStringLength:=StrLen(filename);
nNameLength:=nStringLength-19;
part:=StrPart(filename,16,nNameLength);
RenameFile filename, “Home:/ERRORDATA/”+part+“_”+Date+“.txt”;
Open filename, logfile\write;
Write logfile, " ";
Close logfile;
ENDIF
RETURN;
ENDPROC