Visualizzazione dei risultati da 1 a 10 su 10

Discussione: [VB6] ultimo file

  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    752

    [VB6] ultimo file

    Ciao
    Ho scritto questa Sub:

    Private Sub WatchChangeAction(fPath As String)
    Dim fName As String
    With List1
    .Clear
    fName = Dir(fPath & "\" & "*.*")
    If Len(fName) > 0 Then
    .AddItem "path: " & vbTab & fPath
    .AddItem "file: " & vbTab & fName
    .AddItem "size: " & vbTab & FileLen(fPath & "\" & fName)
    .AddItem "attr: " & vbTab & FileDateTime(fPath & "\" & fName)
    End If
    End With
    End Sub

    ma mi stampa il primo file che trova.
    Vorrei invece stampare l'ultimo file scritto nella mia cartella.

    Help
    Comunque Grazie

  2. #2
    codice:
    Public Sub AddDirSep(ByRef Path As String)
        Const PathSep As String = "\"
        If Right$(Path, 1)<>PathSep Then Path = Path + PathSep
    End Function
    
    Public Function GetLastModifiedFile(ByVal Directory As String)
        Dim fileName As String
        Dim lastAccessDate As Date
        Dim curLastAccessDate As Date
        AddDirSep(Directory)
        fileName = Dir(Directory + "*.*")
        While fileName<>""
            curLastAccessDate = FileDateTime(Directory + fileName)
            If curLastAccessDate > lastAccessDate Then
                lastAccessDate = curLastAccessDate
                GetLastModifiedFile = Directory + fileName
            End If
        Wend
    End Sub
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    752
    Non riesco a integrarlo con il mio programmino.
    Posto tutto sperando in un vostro aiuto:


    Option Explicit
    Dim hChangeHandle As Long
    Dim hWatched As Long
    Dim terminateFlag As Long

    Private Const INFINITE As Long = &HFFFFFFFF
    Private Const FILE_NOTIFY_CHANGE_FILE_NAME As Long = &H1
    Private Const FILE_NOTIFY_CHANGE_DIR_NAME As Long = &H2
    Private Const FILE_NOTIFY_CHANGE_ATTRIBUTES As Long = &H4
    Private Const FILE_NOTIFY_CHANGE_SIZE As Long = &H8
    Private Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10
    Private Const FILE_NOTIFY_CHANGE_LAST_ACCESS As Long = &H20
    Private Const FILE_NOTIFY_CHANGE_CREATION As Long = &H40
    Private Const FILE_NOTIFY_CHANGE_SECURITY As Long = &H100
    Private Const FILE_NOTIFY_FLAGS = FILE_NOTIFY_CHANGE_ATTRIBUTES Or _
    FILE_NOTIFY_CHANGE_FILE_NAME Or _
    FILE_NOTIFY_CHANGE_LAST_WRITE

    Private Declare Function FindFirstChangeNotification Lib "kernel32" _
    Alias "FindFirstChangeNotificationA" _
    (ByVal lpPathName As String, _
    ByVal bWatchSubtree As Long, _
    ByVal dwNotifyFilter As Long) As Long

    Private Declare Function FindCloseChangeNotification Lib "kernel32" _
    (ByVal hChangeHandle As Long) As Long

    Private Declare Function FindNextChangeNotification Lib "kernel32" _
    (ByVal hChangeHandle As Long) As Long

    Private Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

    Private Const WAIT_OBJECT_0 As Long = &H0
    Private Const WAIT_ABANDONED As Long = &H80
    Private Const WAIT_IO_COMPLETION As Long = &HC0
    Private Const WAIT_TIMEOUT As Long = &H102
    Private Const STATUS_PENDING As Long = &H103



    Private Sub Command1_Click()

    Dim watchPath As String
    Dim watchStatus As Long

    watchPath = "D:\Fabio"
    terminateFlag = False
    Command1.Enabled = False

    Label2.Caption = "Watch Folder By Fab Fabolous."


    'get the first file text attributes to the listbox (if any)
    GetLastModifiedFile watchPath

    'show a msgbox to indicate the watch is starting
    'MsgBox "Per iniziare a monitorare la cartella " & watchPath & " .. premi OK"

    'create a watched directory
    hWatched = WatchCreate(watchPath, FILE_NOTIFY_FLAGS)

    'poll the watched folder
    watchStatus = WatchDirectory(hWatched, 100)

    'if WatchDirectory exited with watchStatus = 0,
    'then there was a change in the folder.
    If watchStatus = 0 Then

    'update the listbox for the first file found in the
    'folder and indicate a change took place.
    GetLastModifiedFile watchPath

    MsgBox "The watched directory has been changed. Resuming watch..."

    '(perform actions)
    'this is where you'd actually put code to perform a
    'task based on the folder changing.

    'now go into a second loop, this time calling the
    'FindNextChangeNotification API, again exiting if
    'watchStatus indicates the terminate flag was set
    Do
    watchStatus = WatchResume(hWatched, 100)

    If watchStatus = -1 Then

    'watchStatus must have exited with the terminate flag
    MsgBox "Il controllo della cartella è finita " & watchPath

    Else

    GetLastModifiedFile watchPath
    MsgBox "Il contenuto della cartella è cambiato!!."

    '(perform actions)
    'this is where you'd actually put code to perform a
    'task based on the folder changing.

    End If

    Loop While watchStatus = 0


    Else
    'watchStatus must have exited with the terminate flag
    'MsgBox "Il programma è teminato " & watchPath

    End If

    End Sub


    Private Sub Command2_Click()

    'clean up by deleting the handle to the watched directory
    Call WatchDelete(hWatched)
    hWatched = 0

    Command1.Enabled = True
    Label2.Caption = "Press 'Begin Watch'"

    End Sub


    Private Sub Command3_Click()

    If hWatched > 0 Then Call WatchDelete(hWatched)
    Unload Me

    End Sub


    Private Function WatchCreate(lpPathName As String, flags As Long) As Long

    'FindFirstChangeNotification members:
    '
    ' lpPathName: folder to watch
    ' bWatchSubtree:
    ' True = watch specified folder and its sub folders
    ' False = watch the specified folder only
    ' flags: OR'd combination of the FILE_NOTIFY_ flags to apply

    WatchCreate = FindFirstChangeNotification(lpPathName, False, flags)

    End Function


    Private Sub WatchDelete(hWatched As Long)

    terminateFlag = True
    DoEvents

    Call FindCloseChangeNotification(hWatched)

    End Sub


    Private Function WatchDirectory(hWatched As Long, interval As Long) As Long

    'Poll the watched folder.
    'The Do..Loop will exit when:
    ' r = 0, indicating a change has occurred
    ' terminateFlag = True, set by the WatchDelete routine

    Dim r As Long

    Do

    r = WaitForSingleObject(hWatched, interval)
    DoEvents

    Loop While r <> 0 And terminateFlag = False

    WatchDirectory = r

    End Function


    Private Function WatchResume(hWatched As Long, interval) As Boolean

    Dim r As Long

    r = FindNextChangeNotification(hWatched)

    Do

    r = WaitForSingleObject(hWatched, interval)
    DoEvents

    Loop While r <> 0 And terminateFlag = False

    WatchResume = r

    End Function

    Public Sub AddDirSep(ByRef Path As String)
    Const PathSep As String = "\"
    If Right$(Path, 1) <> PathSep Then Path = Path + PathSep
    End Sub

    Public Function GetLastModifiedFile(ByVal Directory As String)

    Dim fileName As String
    Dim lastAccessDate As Date
    Dim curLastAccessDate As Date
    AddDirSep (Directory)
    fileName = Dir(Directory + "*.*")

    With List1

    .Clear

    While fileName <> ""
    curLastAccessDate = FileDateTime(Directory + fileName)
    If curLastAccessDate > lastAccessDate Then
    lastAccessDate = curLastAccessDate
    GetLastModifiedFile = Directory + fileName
    End If
    Wend
    .AddItem "file: " & Directory & fileName

    End With

    End Function
    Comunque Grazie

  4. #4
    Lascia le funzioni che ho scritto inalterate, e metti
    codice:
    Private Sub WatchChangeAction(fPath As String)
        Dim fName As String
        fName = GetLastModifiedFile(fPath)
        AddDirSep(fPath)
        With List1
            .Clear
            If Len(fName) > 0 Then
                .AddItem "file: " & vbTab & fName
                .AddItem "size: " & vbTab & FileLen(fPath & fName)
                .AddItem "attr: " & vbTab & FileDateTime(fPath & fName)
            End If
        End With
    End Sub
    .
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    752
    Se lancio il tutto mi dice:

    Sub o Form non definita.
    Comunque Grazie

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Ma in quale riga?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  7. #7
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    752
    OK
    Nel tuo post di prima erano invertite End Sub con End Form e viceversa.
    Adesso non ho errori ma non stampa nulla.
    Comunque Grazie

  8. #8
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Originariamente inviato da fosforo
    Nel tuo post ...
    Nel mio post ???
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  9. #9
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    752
    No scusami
    Nel post di MItaly

    Forse non vede nulla perchè da qualche parte c'è un Path errato o mancante?
    Comunque Grazie

  10. #10
    No, è che sono scemo io e mi dimentico i pezzi per strada.
    codice:
    Public Sub AddDirSep(ByRef Path As String)
        Const PathSep As String = "\"
        If Right$(Path, 1)<>PathSep Then Path = Path + PathSep
    End Function
    
    Public Function GetLastModifiedFile(ByVal Directory As String)
        Dim fileName As String
        Dim lastAccessDate As Date
        Dim curLastAccessDate As Date
        AddDirSep(Directory)
        fileName = Dir(Directory + "*.*")
        While fileName<>""
            curLastAccessDate = FileDateTime(Directory + fileName)
            If curLastAccessDate > lastAccessDate Then
                lastAccessDate = curLastAccessDate
                GetLastModifiedFile = Directory + fileName
            End If
            fileName = Dir
        Wend
    End Function
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.