Passa al contenuto principale

SysFileRemove, file remove

Questa funzione da eseguire in task Back, esegue la rimozione (cancellazione) di un file. In Name occorre definire il nome del file da eliminare specificando l'intero percorso (Esempio C:\Directory\File.txt).

Se l'operazione di rimozione va a buon fine la funzione ritorna TRUE, in caso di errore viene ritornato FALSE.

Sostituisce Sysremove

Sostituisce la funzione Sysremove modificando il tipo di parametri in ingresso da stringa a puntatore a stringa. La precedente dichiarazione:

i:=Sysremove('C:\Directory\File.txt');

Diventa:

i:=SysFileRemove(ADR('C:\Directory\File.txt'));

Descrizione​

Immagine F SysFileRemove

ParametroTipoDescrizione
Name@_STRING\]Nome del file da cancellare compreso di percorso.
OutBOOLFALSE: Errore esecuzione, TRUE: Esecuzione Ok.

Esempi​

Come utilizzare gli esempi.

ST_Sysremove: Attivando da debug la variabile Remove, il file C:DirectoryFile.txt se presente viene eliminato dal disco e la variabile viene resettata. In caso di errore di esecuzione il codice di errore è memorizzato nella variabile ECode.

ST_DeleteOldFiles: Attivando da debug la variabile LogOn, viene compialto un record di log con Data/Ora ed il testo definito nella variabile Text. Il record viene scritto nel file D:/Logs/Month, e sono cancellati dalla directory C:/Logs tutti i files precedenti mantenendo solo gli ultimi 6 mesi rispetto all'attuale.

ST_DeleteFilesByDate: Attivando da debug la variabile Delete, viene eseguita la scansione di tutti i files presente nel percorso definito e cancellati quelli più vecchi di 2 giorni.

LogicLab (Ptp116, ST_SysFileRemove)
PROGRAM ST_SysFileRemove
VAR
Remove : BOOL; (* Remove command *)
ECode : UDINT; (* Error code *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_SysFileRemove"
// *****************************************************************************
// By setting the Remove command a file is deleted.
// -----------------------------------------------------------------------------

IF (Remove) THEN
Remove:=FALSE; //Remove command

// Checks if file is present on disk and deletes it.

IF (SysGetFileLen(ADR('C:\Directory\File.txt')) <> eEOF) THEN
IF NOT(SysFileRemove(ADR('C:\Directory\File.txt'))) THEN
ECode:=SysGetLastError(TRUE); //Error code
END_IF;
END_IF;
END_IF;

// [End of file]
LogicLab (Ptp116, ST_DeleteOldFiles)
PROGRAM ST_DeleteOldFiles
VAR
i : INT; (* Auxiliary variable *)
j : INT; (* Auxiliary variable *)
LogOn : BOOL; (* Write log command *)
LMonth : INT; (* Log mount *)
Fp : eFILEP; (* File pointer *)
Filename : STRING[ 32 ]; (* File name *)
LRecord : STRING[ 64 ]; (* Log record *)
Text : STRING[ 16 ]; (* Log text *)
Months : ARRAY[0..11] OF @STRING; (* Month file names *)
LDTS : LDATETIMESTRUCT; (* Long Date/Time struct *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_DeleteOldFiles"
// *****************************************************************************
// A simple data logger that stores a text record on a monthly file. At every
// new month only the 6 older months than the current one are kept in the
// defined directory.
// -----------------------------------------------------------------------------

// -------------------------------------------------------------------------
// INITIALIZATIONS
// -------------------------------------------------------------------------
// At first execution loop the month file names are defined.

IF (SysFirstLoop) THEN
Months[0]:=ADR('January');
Months[1]:=ADR('February');
Months[2]:=ADR('March');
Months[3]:=ADR('April');
Months[4]:=ADR('May');
Months[5]:=ADR('June');
Months[6]:=ADR('July');
Months[7]:=ADR('August');
Months[8]:=ADR('September');
Months[9]:=ADR('October');
Months[10]:=ADR('November');
Months[11]:=ADR('December');
END_IF;

// -------------------------------------------------------------------------
// CREATES LOG RECORD
// -------------------------------------------------------------------------
// If the "LogOn" is set by debug a new record is written in the log file.

IF NOT(LogOn) THEN RETURN; END_IF;
LogOn:=FALSE; //Write log command
Text:='This is a log'; //Log text

// Creates the log record to be written.

eTO_JUNK(DateTimeFormat(TO_LDATE_AND_TIME(SysDateGetNs()), ADR('^d/m/Y\ H\:i\:s'), ADR(LRecord), SIZEOF(LRecord)));
eTO_JUNK(SysCVsnprintf(ADR(LRecord), SIZEOF(LRecord), ADR(' %s$r$n'), STRING_TYPE, ADR(Text)));

// -------------------------------------------------------------------------
// DELETES OLD FILES
// -------------------------------------------------------------------------
// Assign data values.

eTO_JUNK(SPLIT_DT(TO_DATE_AND_TIME(SysDateGetS()), ADR(LDTS.Year), ADR(LDTS.Month), ADR(LDTS.Day), ADR(LDTS.Hours), ADR(LDTS.Minutes), ADR(LDTS.Seconds)));

// Checks if the month has been changed after last record write.

IF (LDTS.Month <> LMonth) THEN
LMonth:=LDTS.Month; //Log mount

// Deletes the 6 months older than the current month.

FOR j:=(LMonth+1) TO (LMonth+6) DO
IF (j > 12) THEN i:=j-12; ELSE i:=j; END_IF;

eTO_JUNK(SysVsnprintf(ADR(Filename), SIZEOF(Filename), ADR('D:/Logs/%s.txt'), STRING_TYPE, Months[i-1]));
IF (SysGetFileLen(ADR(Filename)) <> eEOF) THEN eTO_JUNK(SysFileRemove(ADR(Filename))); END_IF;
eTO_JUNK(SysWrSpyData(SPY_ASCII, 0, 16#00000001, ADR('Checked'), ADR(Filename)));
END_FOR;
END_IF;

// -------------------------------------------------------------------------
// WRITE LOG FILE
// -------------------------------------------------------------------------
// Creates the log file name.

eTO_JUNK(SysVsnprintf(ADR(Filename), SIZEOF(Filename), ADR('D:/Logs/%s.txt'), STRING_TYPE, Months[LMonth-1]));
Fp:=SysFfopen(ADR(Filename), ADR('a')); //File pointer

IF (SysFIsOpen(Fp)) THEN
eTO_JUNK(Sysfwrite(ADR(LRecord), TO_INT(Sysstrlen(ADR(LRecord))), 1, Fp));
Fp:=FClose(Fp); //File pointer
END_IF;

// [End of file]
LogicLab (Ptp116, ST_DeleteFilesByDate)
PROGRAM ST_DeleteFilesByDate
VAR
Delete : BOOL; (* Delete files command *)
CaseNr : USINT; (* Program case *)
FFound : USINT; (* Files found *)
FDeleted : USINT; (* Files deleted *)
FRemove : STRING[ 64 ]; (* File to remove *)
DList : SysGetFileInfos; (* Directory listing *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_DeleteFilesByDate"
// *****************************************************************************
// Setting by debug the "Delete" variable, are deleted from the defined path all
// files older than 2 days.
// -----------------------------------------------------------------------------

// -------------------------------------------------------------------------
// DIRECTORY LISTING MANAGEMENT
// -------------------------------------------------------------------------
// Manage the directory listing.

DList.PathName:=ADR('MyDir'); //Directory to list
DList(); //Directory listing
DList.Init:=FALSE; //Init listing
DList.Next:=FALSE; //Next command

// -------------------------------------------------------------------------
// PROGRAM CASES
// -------------------------------------------------------------------------
// Program case management.

CASE (CaseNr) OF

// ---------------------------------------------------------------------
// Wait for delete command ad init listing.

0:
IF NOT(Delete) THEN RETURN; END_IF;
Delete:=FALSE; //Delete files command
FFound:=0; //Files found
FDeleted:=0; //Files deleted
DList.Init:=TRUE; //Init listing
CaseNr:=CaseNr+1; //Program case

// ---------------------------------------------------------------------
// If file found returns file data.

1:
IF NOT(DList.Found) THEN CaseNr:=0; RETURN; END_IF;
FFound:=FFound+1; //Files found

// Check if file date is older than 2 days (172800 seconds).

IF (DList.FTime > (SysDateTime-172800)) THEN RETURN; END_IF;

// Delete the file from disk.

eTO_JUNK(SysVsnprintf(ADR(FRemove), SIZEOF(FRemove), ADR('%s/'), STRING_TYPE, ADR(DList.PathName)));
eTO_JUNK(SysCVsnprintf(ADR(FRemove), SIZEOF(FRemove), ADR('%s'), STRING_TYPE, ADR(DList.Name)));
eTO_JUNK(SysFileRemove(ADR(FRemove))); //Delete file
FDeleted:=FDeleted+1; //Files deleted
DList.Next:=TRUE; //Next command
END_CASE;

// [End of file]