Results 1 to 4 of 4

Thread: how to detect txt file has been changed?

  1. #1

    Thread Starter
    Hyperactive Member temp_12000's Avatar
    Join Date
    Jan 2004
    Location
    LA, USA
    Posts
    411

    Question how to detect txt file has been changed?

    I'm working on a program which needs save some temporary data in current directory. Next time, when users launch the program, it will load the data. However, users may manually change the data file. What we want is that if users manually changed the data file, we can detect that in our program. How to do it? Any suggestion is appreciated. Thanks



  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Compare the date?

  3. #3
    Addicted Member
    Join Date
    Dec 2001
    Posts
    158
    You have a lot of options.

    You could make a seperate file that you use to contain a copy or a hash in.

    You could use something like the (sweet!) Opalis Robot to launch a program every time a file is changed (sweet - but expensive - I love the thing!)


    You could check the modified date on the file. Here's some code (meant for Excel VBA) I copied off a web page some time ago (I can't not remember the sourse of the info - pardon):

    Code:
    Sub TestListFilesInFolder()
        Workbooks.Add ' create a new workbook for the file list
        ' add headers
        With Range("A1")
            .Formula = "Folder contents:"
            .Font.Bold = True
            .Font.Size = 12
        End With
        Range("A3").Formula = "File Name:"
        Range("B3").Formula = "File Size:"
        Range("C3").Formula = "File Type:"
        Range("D3").Formula = "Date Created:"
        Range("E3").Formula = "Date Last Accessed:"
        Range("F3").Formula = "Date Last Modified:"
        Range("G3").Formula = "Attributes:"
        Range("H3").Formula = "Short File Name:"
        Range("A3:H3").Font.Bold = True
        ListFilesInFolder "C:\FolderName\", True ' list all files included subfolders
    End Sub
    
    Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
    ' lists information about the files in SourceFolder
    ' example: ListFilesInFolder "C:\FolderName\", True
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
    Dim FileItem As Scripting.File
    Dim r As Long
        Set FSO = New Scripting.FileSystemObject
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
        r = Range("A65536").End(xlUp).Row + 1
        For Each FileItem In SourceFolder.Files
            ' display file properties
            Cells(r, 1).Formula = FileItem.Path & FileItem.Name
            Cells(r, 2).Formula = FileItem.Size
            Cells(r, 3).Formula = FileItem.Type
            Cells(r, 4).Formula = FileItem.DateCreated
            Cells(r, 5).Formula = FileItem.DateLastAccessed
            Cells(r, 6).Formula = FileItem.DateLastModified
            Cells(r, 7).Formula = FileItem.Attributes
            Cells(r, 8).Formula = FileItem.ShortPath & FileItem.ShortName
            ' use file methods (not proper in this example)
    '        FileItem.Copy "C:\FolderName\Filename.txt", True
    '        FileItem.Move "C:\FolderName\Filename.txt"
    '        FileItem.Delete True
            r = r + 1 ' next row number
        Next FileItem
        If IncludeSubfolders Then
            For Each SubFolder In SourceFolder.SubFolders
                ListFilesInFolder SubFolder.Path, True
            Next SubFolder
        End If
        Columns("A:H").AutoFit
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        ActiveWorkbook.Saved = True
    End Sub
    
    
     
     
     Check if a file exists
    
    Sub FileExists()
    Dim fso
    Dim file As String
    file = "C:\Test.xls" ' change to match the file w/Path
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(file) Then
        MsgBox file & " was not located.", vbInformation, "File Not Found"
    Else
        MsgBox file & " has been located.", vbInformation, "File Found"
    End If
    End Sub

  4. #4
    Addicted Member
    Join Date
    Dec 2001
    Posts
    158
    Here's the Opalis Robot homepage:
    http://www.opalis.com/products/robot/index.html

    It's like a visual app that you can do almost anything with. Anything that could happen on a computer, or network computer can be used as a trigger to do anything you want (launch a program, send email, start a process). I do so much cool automation stuff with it at work!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width