Differenze tra le versioni di "Sapere in tempo reale se un file di nome conosciuto viene modificato o distrutto"

Da Gambas-it.org - Wikipedia.
 
(Una versione intermedia di uno stesso utente non è mostrata)
Riga 7: Riga 7:
 
  Public Sub Main()
 
  Public Sub Main()
 
    
 
    
   dr = "''<FONT Color=gray>''/percorso/del/file''</font>"
+
   dr = "<FONT Color=gray>''/percorso/del/file''</font>"
 
   lun = File.Load(dr)
 
   lun = File.Load(dr)
 
    
 
    
Riga 24: Riga 24:
 
   If Exist(dr) Then
 
   If Exist(dr) Then
 
     s = File.Load(dr)
 
     s = File.Load(dr)
     If lun <> s Then Print "Si sta modificando il file: "; dr
+
     If lun <> s Then
 +
      Print "Il file '"; dr; "' è stato modificato !\n"
 +
      Print s
 +
    Endif
 
     lun = s
 
     lun = s
 
   Else
 
   Else
Riga 40: Riga 43:
 
  Public Sub Main()
 
  Public Sub Main()
 
    
 
    
   dr = "''<FONT Color=gray>''/percorso/del/file''</font>"
+
   dr = "<FONT Color=gray>''/percorso/del/file''</font>"
 
   lun = File.Load(dr)
 
   lun = File.Load(dr)
 
    
 
    
Riga 48: Riga 51:
 
   
 
   
 
   
 
   
  '''Public Sub File_Read()
+
  Public Sub File_Read()
 
    
 
    
 
   Dim s As String
 
   Dim s As String
Riga 54: Riga 57:
 
   If Exist(dr) Then
 
   If Exist(dr) Then
 
     s = File.Load(dr)
 
     s = File.Load(dr)
     If lun <> s Then Print "Il file "; dr; " è stato modificato !"
+
     If lun <> s Then
 +
      Print "Il file '"; dr; "' è stato modificato !\n"
 +
      Print s
 +
    Endif
 
     lun = s
 
     lun = s
 
   Else
 
   Else

Versione attuale delle 16:23, 11 set 2023

Per sapere in tempo reale se un file, di cui si conosce il percorso ed il nome, viene modificato o distrutto, si può adottare un codice come il seguente:

Private tempus As Timer
Private dr As String
Private lun As String


Public Sub Main()
 
 dr = "/percorso/del/file"
 lun = File.Load(dr)
 
 With tempus = New Timer As "Tempus"
   .Delay = 250
   .Start
 End With
  
End


Public Sub Tempus_Timer()
 
 Dim s As String
  
 If Exist(dr) Then
   s = File.Load(dr)
   If lun <> s Then
     Print "Il file '"; dr; "' è stato modificato !\n"
     Print s
   Endif
   lun = s
 Else
   Print "\nIl file '"; dr; "' è stato distrutto !"
   tempus.Stop
 Endif
   
End

oppure un codice simile al precedente, ma ponendo il file da verificare sotto osservazione con la parola chiave Watch:

Private dr As String
Private lun As String
Private fl As File


Public Sub Main()
 
 dr = "/percorso/del/file"
 lun = File.Load(dr)
 
 fl = Open dr For Read Watch
  
End


Public Sub File_Read()
 
 Dim s As String
  
 If Exist(dr) Then
   s = File.Load(dr)
   If lun <> s Then
     Print "Il file '"; dr; "' è stato modificato !\n"
     Print s
   Endif
   lun = s
 Else
   Print "\nIl file '"; dr; "' è stato distrutto !"
   fl.Close
 Endif
   
End