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

Da Gambas-it.org - Wikipedia.
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 5: Riga 5:
 
   
 
   
 
   
 
   
  '''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 15: Riga 15:
 
   End With
 
   End With
 
    
 
    
  '''End'''
+
  End
 
   
 
   
 
   
 
   
  '''Public''' Sub Tempus_Timer()
+
  Public Sub Tempus_Timer()
 
    
 
    
 
   Dim s As String
 
   Dim s As String
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 31: Riga 34:
 
   Endif
 
   Endif
 
      
 
      
  '''End'''
+
  End
 
 
 
oppure un codice simile al precedente, ma ponendo il file da verificare sotto ''osservazione'' con la parola chiave ''Watch'':
 
oppure un codice simile al precedente, ma ponendo il file da verificare sotto ''osservazione'' con la parola chiave ''Watch'':
 
  Private dr As String
 
  Private dr As String
Riga 39: Riga 41:
 
   
 
   
 
   
 
   
  '''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)
 
    
 
    
 
   fl = Open dr For Read Watch
 
   fl = Open dr For Read Watch
 
    
 
    
  '''End'''
+
  End
 
   
 
   
 
   
 
   
  '''Public''' Sub File_Watch()
+
  Public Sub File_Read()
 
    
 
    
 
   Dim s As String
 
   Dim s As String
Riga 55: 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
Riga 62: Riga 67:
 
   Endif
 
   Endif
 
      
 
      
  '''End'''
+
  End

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