Questo blocco funzione da eseguire in task Back, ritorna le informazioni di singoli file o directory oppure dei file o directory contenuti in una directory. Passando in PathNane il nome del file o directory completo del percorso ed attivando Init, se il file o directory è presente ne sono ritornate le informazioni.
Eseguire listing di tutti i files in una cartella
Definendo in PathName il nome di una cartella seguito dal filtro “/*.*”, attivando Init si inizializza la lista dei files e viene ritornato il primo file trovato. Ad ogni comando Next si esegue la ricerca di un nuovo file, se file trovato l’uscita Found si attiva per un loop e ne viene ritornato il nome in Name. Terminato l’elenco di tutti i files presenti su comando Next non viene più attivata l’uscita Found e Name è abblencato.
E’ anche possibile eseguire un filtro sui files da tornare. Usando ad esempio “/*.htm” vengono tornati solo i file con estensione “htm”. L’uscita IsDir si attiva se il nome del file ritornato è quello di una sottodirectory
Sostituisce SysDirListing
Sostituisce il blocco funzione SysDirListing, modificando il tipo di parametri in ingresso e in uscita da stringa a puntatore a stringa. La precedente dichiarazione:
VAR
DList : SysDirListing; (* Get file infos *)
END_VAR
DList.PathName:='C:/';
DList();
IF (DList.Found) THEN i:=SysVfprintf(Fp, ADR('%s'), VR_TYPE#STRING_TYPE, ADR(DList.Name)); END_IF;
Diventa:
VAR
DList : SysGetFileInfos; (* Get file infos *)
END_VAR
DList.PathName:=ADR('C:/');
DList();
IF (DList.Found) THEN i:=SysVfprintf(Fp, ADR('%s'), VR_TYPE#STRING_TYPE, DList.Name); END_IF;
Descrizione
Init (BOOL) Alla attivazione viene inizializzato l’indice dei files nella directory indicata e viene ritornato il nome del primo file trovato.
Next (BOOL) Alla attivazione viene ritornato il nome del file puntato dall’indice nella directory indicata. L’indice si incrementa puntando il successivo file.
PathName (@STRING) Nome del file completo di percorso.
Per rilevare i dati di un file specifico definirne percorso e nome, esempio C:\MyDir\MyFile.log.
Per rilevare i dati di una directory definirne il percorso esempio C:\MyDir\MyFile.log.
Per eseguire listing dei files in una directory definire il percorso con il filtro esempio C:\MyDir\*.log.
Found (BOOL) Si attiva per un loop se su comando Init o Next è stato trovato un file.
Fault (BOOL) Si attiva per un loop se errore esecuzione.
IsDir (BOOL) Attiva se il nome di file ritornato appartiene ad una sottodirectory.
Size (UDINT) Dimensione del file.
FTime (UDINT) Data ultima modifica del file in Epoch time (UTC).
Name (@STRING) Nome del file comprensivo di eventuale estensione

Esempi
ST_SysGetFileInfos: Collegandosi in telnet alla porta indicata viene tornato l’elendo dei file e directory trovati nel percorso impostato.
ST_DeleteOldestFiles: Attivando da debug la variabile Delete, vengono controllati i files presenti nella directory definita e sono cancellati quelli più vecchi per mantenere il numero di files totali pari al valore di Threshold.
Atri esempi si trovano nella funzione Sysremove.
LogicLab (Ptp116, ST_SysGetFileInfos)
PROGRAM ST_SysGetFileInfos
VAR
CaseNr : USINT; (* Program case *)
FFound : USINT; (* Files found *)
Fp : eFILEP; (* File pointer array *)
DFound : USINT; (* Directories found *)
LDTime : LDATETIMESTRUCT; (* Date/Time struct *)
TCPServer : SysTCPServer; (* TCP server *)
DList : SysGetFileInfos; (* Get file infos *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_SysGetFileInfos"
// *****************************************************************************
// When a telnet client connect to port 1200 it's executed the listing of
// a directory.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Program initializations.
IF (SysFirstLoop) THEN
// TCPServer initialization.
TCPServer.FilesArr:=ADR(Fp); //Files array
TCPServer.LocalAdd:=ADR('0.0.0.0'); //Local address
TCPServer.LocalPort:=1200; //Local port
TCPServer.MaxConn:=1; //Accepted connections
TCPServer.FlushTm:=50; //Flush time (mS)
TCPServer.LifeTm:=30; //Life time (S)
TCPServer.RxSize:=128; //Rx buffer size
TCPServer.TxSize:=128; //Tx buffer size
// Directory listing initialization.
DList.PathName:=ADR('/tmp/*.*'); //Directory to list
DList.PathName:=ADR('C:/*.*'); //Directory to list
END_IF;
// -------------------------------------------------------------------------
// TCP SERVER MANAGEMENT
// -------------------------------------------------------------------------
// Manage the TCP server and directory listing.
DList(); //Directory listing
TCPServer(); //TCPServer management
TCPServer.Enable:=TRUE; //TCPServer enable
// Check if a client has been connected.
IF NOT(SysFIsOpen(Fp)) THEN CaseNr:=0; RETURN; END_IF;
// -------------------------------------------------------------------------
// PROGRAM CASES
// -------------------------------------------------------------------------
// Program case management.
CASE (CaseNr) OF
// ---------------------------------------------------------------------
// Init listing.
0:
DFound:=0; //Directories found
FFound:=0; //Files found
DList.Init:=TRUE; //Init listing
eTO_JUNK(SysVfprintf(Fp, ADR('%s$r$n$r$n'), VR_TYPE#STRING_TYPE, ADR('"SysDirListing" example program')));
CaseNr:=10; //Program case
// ---------------------------------------------------------------------
// Init listing.
1:
DList.Next:=TRUE; //Next command
CaseNr:=10; //Program case
// ---------------------------------------------------------------------
// Reset commands.
10:
DList.Init:=FALSE; //Init listing
DList.Next:=FALSE; //Next command
// If file found returns file data.
IF (DList.Found) THEN
// Returns Date/Time.
eTO_JUNK(SPLIT_DT(TO_DATE_AND_TIME(DList.FTime), ADR(LDTime.Year), ADR(LDTime.Month), ADR(LDTime.Day), ADR(LDTime.Hours), ADR(LDTime.Minutes), ADR(LDTime.Seconds)));
eTO_JUNK(SysVfprintf(Fp, ADR('%02d/'), VR_TYPE#UINT_TYPE, ADR(LDTime.Day)));
eTO_JUNK(SysVfprintf(Fp, ADR('%02d/'), VR_TYPE#UINT_TYPE, ADR(LDTime.Month)));
eTO_JUNK(SysVfprintf(Fp, ADR('%04d'), VR_TYPE#UINT_TYPE, ADR(LDTime.Year)));
eTO_JUNK(SysVfprintf(Fp, ADR(' %02d:'), VR_TYPE#UINT_TYPE, ADR(LDTime.Hours)));
eTO_JUNK(SysVfprintf(Fp, ADR('%02d'), VR_TYPE#UINT_TYPE, ADR(LDTime.Minutes)));
// Returns directory label or file length.
IF (DList.IsDir) THEN
DFound:=DFound+1; //Directories found
eTO_JUNK(SysVfprintf(Fp, ADR(' %10s'), VR_TYPE#STRING_TYPE, ADR('[DIR]')));
ELSE
FFound:=FFound+1; //Files found
eTO_JUNK(SysVfprintf(Fp, ADR(' %10d'), VR_TYPE#UDINT_TYPE, ADR(DList.Size)));
END_IF;
// Returns file name.
eTO_JUNK(SysVfprintf(Fp, ADR(' %s$r$n'), VR_TYPE#STRING_TYPE, DList.Name));
CaseNr:=20; //Program case
RETURN;
END_IF;
// All files in the directory have been listed.
eTO_JUNK(SysVfprintf(Fp, ADR('%16d Files$r$n'), VR_TYPE#USINT_TYPE, ADR(FFound)));
eTO_JUNK(SysVfprintf(Fp, ADR('%16d Directory$r$n'), VR_TYPE#USINT_TYPE, ADR(DFound)));
CaseNr:=30; //Program case
// ---------------------------------------------------------------------
// Wait until the output buffer is empty.
20, 30:
IF (TO_UDINT(SysFGetOSpace(Fp)) <> SysFGetOBfSize(Fp)) THEN RETURN; END_IF;
IF (CaseNr = 30) THEN Fp:=FClose(Fp); END_IF; //Close socket
CaseNr:=1; //Program case
END_CASE;
// [End of file]
LogicLab (Ptp116, ST_DeleteOldestFiles)
PROGRAM ST_DeleteOldestFiles
VAR
Delete : BOOL; (* Delete files command *)
CaseNr : USINT; (* Program case *)
FFound : USINT; (* Files found *)
FDate : UDINT; (* File date (Epoch) *)
FName : STRING[ 64 ]; (* File name *)
Threshold : USINT; (* File number threshold *)
Dir : STRING[ 32 ]; (* Directory name *)
DirWithFilter : STRING[ 32 ]; (* Directory name with filter *)
DList : SysGetFileInfos; (* Directory listing *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_DeleteOldestFiles"
// *****************************************************************************
// If set by debug the "Delete" variable the program maintains a defined number
// of files into a directory. If there are more than defined the oldest are
// deleted.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// PROGRAM INITIALIZATION
// -------------------------------------------------------------------------
// Execute initializations.
IF (SysFirstLoop) THEN
Threshold:=2; //File number threshold
Dir:='C:/MyDir'; //Directory to list
DirWithFilter:=CONCAT(Dir, '/*.*'); //Directory to list with filter to get all its files
DList.PathName:=ADR(DirWithFilter); //Directory to list
END_IF;
// -------------------------------------------------------------------------
// EXECUTE THE CONTROL
// -------------------------------------------------------------------------
// Check if command set.
IF NOT(Delete) THEN
CaseNr:=0; //Program case
ELSE
// Manage the directory listing.
DList(); //Directory listing
DList.Init:=FALSE; //Init listing
DList.Next:=FALSE; //Next command
// ---------------------------------------------------------------------
// PROGRAM CASES
// ---------------------------------------------------------------------
// Program case management.
CASE (CaseNr) OF
// -----------------------------------------------------------------
// Init directory listing.
0:
FFound:=0; //Files found
FDate:=SysDateTime; //File date (Epoch)
DList.Init:=TRUE; //Init listing
CaseNr:=CaseNr+1; //Program case
// -----------------------------------------------------------------
// If file found returns file data.
1:
IF NOT(DList.Found) THEN CaseNr:=CaseNr+1; RETURN; END_IF;
FFound:=FFound+1; //Files found
DList.Next:=TRUE; //Next command
// Check if file date is older previous.
IF (DList.FTime > FDate) THEN RETURN; END_IF;
FDate:=DList.FTime; //File date (Epoch)
eTO_JUNK(SysVsnprintf(ADR(FName), SIZEOF(FName), ADR('%s'), STRING_TYPE, ADR(Dir)));
eTO_JUNK(SysCVsnprintf(ADR(FName), SIZEOF(FName), ADR('/%s'), STRING_TYPE, DList.Name));
// -----------------------------------------------------------------
// If number of files in directory is greater than the threshold,
// the oldest is deleted.
2:
IF (FFound <= Threshold) THEN Delete:=FALSE; RETURN; END_IF;
eTO_JUNK(SysFileRemove(ADR(FName))); //Delete file
CaseNr:=0; //Program case
END_CASE;
END_IF;
// [End of file]