Modules |
5.67 Alcune funzioni sui file |
Alessandro Baraldi |
Spostare un file Con la funzione che segue è possibile spostare un file: Public Function MoveFile(StartPath As String, EndPath As String) As Boolean On Error GoTo error MoveFile = False FileCopy StartPath$, EndPath$ Kill StartPath$ MoveFile = True Exit Function error: MsgBox Err.Description, vbExclamation, "Error" End Functiondove StartPath è una variabile stringa che contiene il path completo del file da spostare ed EndPath è una variabile stringa che contiene il path completo del file dopo lo spostamento. La funzione restituisce il valore booleano True o False a seconda che lo spostamento del file sia andato o meno a buon fine. Copiare un file Con la funzione che segue è possibile copiare un file: Public Function CopyFile(StartPath As String, EndPath As String) As Boolean On Error GoTo error CopyFile = False FileCopy StartPath$, EndPath$ CopyFile = True Exit Function error: MsgBox Err.Description, vbExclamation, "Error" End Functiondove StartPath è una variabile stringa che contiene il path completo del file da copiare ed EndPath è una variabile stringa che contiene il path completo del file copiato. La funzione restituisce il valore booleano True o False a seconda che la copia del file sia andata o meno a buon fine. Cancellare un file Con la sub che segue è possibile cancellare un file: Public Sub DeleteFile(ByVal FilePath As String) On Error GoTo error Kill FilePath$ Exit Sub error: MsgBox Err.Description, vbExclamation, "Error" End Subdove FilePath è una variabile stringa che contiene il path completo del file da cancellare. Esegui/apri un file Con la sub che segue è possibile eseguire o aprire un file: Public Sub ExecuteFile(FilePath As String) On Error GoTo error Dim ret As Integer ret = Shell("rundll32.exe url.dll,FileProtocolHandler " & (FilePath)) Exit Sub error: MsgBox Err.Description, vbExclamation, "Error" End Subdove FilePath è una variabile stringa che contiene il path completo del file da eseguire/aprire. Rilevare la data di creazione di un file Con la funzione che segue è possibile ottenere la data di creazione di un file: Public Function DataCreazione(strPath) As Date On Error Resume Next DataCreazione = FileDateTime(strPath) End FunctionDove strPath è una variabile stringa che contiene il path completo del file di cui si vuole conoscere la data di creazione. Rilevare le dimensioni di un file Con la funzione che segue è possibile ottenere le dimensioni di un file in KBytes: Public Function Dimensioni(strPath) On Error Resume Next Dimensioni = Int(FileLen(strPath) / (1024)) & " KB" End FunctionDove strPath è una variabile stringa che contiene il path completo del file di cui si vogliono conoscere le dimensioni. |