Using Event Routine for Renaming Text Files. Renaming Not Working. Code Included.

I'm trying to use an event routine to check a directory for any text files over 4kb. If it finds a file matching that description I want to move and rename the file with the date as part of the name. I keep running into "Can't Rename File"

I have this code in the user.sys module set to trigger with an Event RESET:

VAR String Date;
VAR iodev logfile;
VAR dir directory;
VAR string filename;

PROC rSaveAndClearEvent()<-------This is the routine triggered by the event RESET
rSearchDir "HOME:/TEMPDATA","rSaveAndClear";
ENDPROC

PROC rSearchDir(string dirname, string actionproc)<-------Which Proc Calls this
IF IsFile(dirname \Directory) THEN
OpenDir directory, dirname;
! .. 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) > 4096 THEN
Date:=CDate();
RenameFile filename, "Home:/ERRORDATA/"+Date+filename;<-----I know this is the line of code giving me the problems.
Open filename, logfile\write;                                                               Right now, 'filename' contains the entire path Home:/TEMPDATA/******.txt
Write logfile, " ";                                                                                  Is there a way to pull out just the actual filename and use it in the RenameFile?
Close logfile;                                                                                       If I only use "Home:/ERRORDATA/"+Date; The rest of the code works, but only gives me the date and on
ENDIF                                                                                        the next file I get an error that I already have a file with that name (obvious, but I wanted to demonstrate
RETURN;                                                                                           everything else is working)
ENDPROC

• mod

See the attached on function StrPart, use the "filename" variable in the function as below.

part := StrPart(FileName,15,8);

You need to know how long the file name is - if they vary you will need to use STRLEN to find the total length of the "FileName" then subtract the known 15 characters of the path from it to get the file name length.....

• Graeme saves the day again!
I played around with StrPart and StrLen and got it working.

This is what my final code looks like:

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

Thank you very much for your help!