Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FileDateTime(pathname)

    I use Iomega QuickSync II to constantly back up my documents and templates directories to my Zip Drive. This not only provides me with an off site backup, but also allows me to work on any file outside of the office.

    To enhance the system, I have written a macro, OpenFromZip(), that runs on my office machine. It makes
    it easy to update my hard drive with any file I edited or created elsewhere. OpenFromZip() simply presents a FileOpen dialog on the ZipDriveDocuments directory. When I choose a file, the macro copies it to the corresponding subdirectory of the Documents directory on the hard drive. If the corresponding subdirectory does not exist on the hard drive, it creates it.

    Most of the time it works fine, but I have had a couple of problems caused by human error. A couple of times, I have inadvertently overwritten the newer copy on the hard drive with the older copy on the zip because I mistakenly believed that the zip version was newer.

    The macro currently overwrites the hard drive copy by default. I want to enhance it by adding a confirmation that shows the corresponding modification dates.

    I have tried to use the FileDateTime(pathname) function in


    <pre>
    If FileDateTime(ZipVersion) < FileDateTime(HDVersion) Then

    If MsgBox(HDVersion & " is newer than " & ZipVersion & _
    ". Do you want to overwrite " & HDVersion & "(" & _
    HDVerDate & ") with " & ZipVersion & "(" & ZipVerDate _
    & "?", VBYesNoCancel) = VBYes Then

    [proceed with the copy, etc.]
    </pre>


    I can't seem to get the variables HDVerDate and ZipVerDate to work. The documentation says that FileDateTime(pathname) returns a variant. Any suggestions?

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: FileDateTime(pathname)

    Hi,
    When you say you can't get them to work, what exactly is happening? You haven't posted the code that determines what ZipVerDate and HDVerDate are - can you post that too?
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    Sorry, I left that out. It is

    <pre>Dim ZipVerDate
    Dim HDVerDate

    ...

    ZipVerDate = FileDateTime(ZipVersion)
    HDVerDate = FileDateTime(HDVersion)
    </pre>


    ZipVersion and HDVersion are strings comprised of the full path names of the respective files. The problem is that the HDVerDate and ZipVerDate vaiables remain empty throughout the macro's execution, so the "If" test retuns "false" and the macro executes as if the test did not exist.

    I assume that I don't understand and use the data type for the FileDateTime(pathname) function properly.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    I had problems with this sort of stuff about five months back. I needed date/timestamps from files as part of an installation routine.

    I waded through the busines sof PKZip (not Zip drives) using a form of UTC but got bogged down in effecting the conversion in Word97SR2/VBA.

    I probably have the old code floatinga round, if you get really stuck. They are probably lotsa functions with TESTcode that let me single-step through stuff to see just waht wa shappening.

    In the end I elected to test just the file name and size, and skip the date/time comparisons.

    This won't be much help except to say that the FileDateTime stuff is either broken, or just darn difficult to get working or to understand.

    Ultimately I'd like to reintroduce the timestamp code, so I'd entertain the idea of a fresh start on it ....

    (later)

    I backup from two HDs to a removable HD each morning using the DOS XCOPY command and PKZip to collect the essential system files (registry, INI files etc). The Xcopy command does a good job of identifying which files are changed since the latest copy. I could post the BATch file.

  5. #5
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: FileDateTime(pathname)

    I can't see anything wrong with that code (I just tested it and it's OK on my PC) so could you post the whole thing in case something is going wrong somewhere else?
    Regards,
    Rory

    Microsoft MVP - Excel

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: FileDateTime(pathname)

    Hi,
    I'm stumped - I pasted that into a module, changed the paths and it worked fine! Have you tried stepping through the code with the locals window open and viewing where it fails? (I'm assuming that the fact you have a line which has an underscore and then carries on on the same line rather than on the next line is simply down to a formatting glitch when you posted)
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    Yeah. The hardest part of posting the Sub was reformatting it for the lounge.

    I'm going to try it on another machine.

  8. #8
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    *** Edited - Geoff W. Long lines split (They fo bad things to , particularly in flat mode) ***

    Rory,

    Here is the entire sub
    <hr>

    <pre>Sub OpenFromZip()
    '
    ' DisplayFileOpen Macro
    ' An aide in Zip Sync file management.
    ' This macro allows you to open a document which
    ' only exists on the QuickSync disk and copies it
    ' to the corresponding folder on the hard drive
    '
    ' This macro could have been written to run on any
    ' Office97 machine by using functions that return the
    ' pertinent directories on the machine in use, but
    ' that was unnecessary for my purposes and would have
    ' slowed the macro down. Instead, I chose to hard
    ' code the directories into the macro. Hence, if the
    ' directories change, the macro needs to be manually
    ' updated.
    '
    Dim ZipDir As String 'variable holds the Zip drive path
    Dim SaveDir As String 'variable holds the hard drive path
    Dim RelPath As String 'variable hold the relative path
    '(stripped of the drive letter)
    Dim FName As String 'variable holds the file name
    Dim ZipVersion As String 'variable holds the full path name
    'of the zip version
    Dim HDVersion As String 'variable holds the full path name
    'of the HD version
    Dim ZipVerDate 'variable holds the time/date stamp
    'of the zip version
    Dim HDVerDate 'variable holds the time/date stamp
    'of the HD version

    On Error GoTo CreateIt
    Err.Clear
    StatusBar = "Select a file from the Zip drive."
    'First, change the default directory to the documents 'directory on the zip drive
    ChangeFileOpenDirectory "Jocuments"
    'Now, display the FileOpen dialog
    Application.Dialogs(wdDialogFileOpen).Show

    'Find out which subdirectory, on the Zip drive, the opened
    'document is located and assign it to a variable (ZipDir)
    ZipDir = ActiveDocument.Path
    ZipVersion = ActiveDocument.FullName
    ZipVerDate = FileDateTime(ZipVersion)
    'Strip the zip drive letter from the full path and assign
    'this relative path to a variable (RelPath). By ommitting
    'the third (length) parameter from the
    'Mid(string,start,length) function, the function returns the
    'remainder of the string,regardless of its length.
    RelPath = Mid(ZipDir, 4)
    'Identify the corresponding directory on the hard drive by
    'appending the hard drive letter to the relative path
    'variable and assigning it to a new variable (SaveDir)
    'IMPORTANT: While windows is not really case sensitive and
    'will open or save a file to the proper directory regardless
    'of the case in the path string, VisualBasic's string
    'comparison operators are case sensitive. Therefore, even
    'though
    'ActiveDocument.SaveAs "eocumentsArthur" will work as
    'expected, ActiveDocument.Path will not equal
    '"e:documentsarthur" since ActiveDocument.Path
    'will always return the drive letter as a capital
    SaveDir = "EocumentsArthur" & RelPath
    'Now, get just the file name without the path and assign it
    'to the (FName) variable
    FName = ActiveDocument.Name
    'Put the hard drive full path name in a string
    HDVersion = SaveDir & "" & FName
    'if it exists get its date time stamp
    HDVerDate = FileDateTime(HDVersion)
    'compare the dates/times of the two files
    If ZipVerDate < HDVerDate Then
    If MsgBox(HDVersion & " is newer than " & ZipVersion _
    & ". Do you want to overwrite " _
    & HDVersion & "(" & HDVerDate & ") with " & _
    ZipVersion & "(" & ZipVerDate & "?", _
    vbYesNoCancel) = vbYes Then GoTo TryAgain
    Else
    GoTo Done
    End If


    TryAgain:

    StatusBar = "Saving " & FName & " to " & SaveDir & "..."
    'Finally, save the document to the corresponding directory
    'on the hard drive using SaveAs to save it to the
    'corresponding hard drive directory (SaveDir) with the same
    'file name (FName).
    ActiveDocument.SaveAs HDVersion
    GoTo TestArea

    CreateIt:
    StatusBar = "Creating " & SaveDir & "..."
    ChDrive "E"
    ChDir "EocumentsArthur"
    MkDir RelPath
    GoTo TryAgain

    TestArea:
    'Test to see if the procedure was successful and, display a
    'confirmation or error. If the procedure was successful,
    'the ActiveDocument should reside on the hard drive, in
    'the corresponding directory. If the ActiveDocument does
    'not, something went wrong.
    'If you are having trouble with this test, see the comments
    'at lines 45-50 which discuss the
    'case sensitive nature of the comparison string which
    'requires that SaveDir have acurate
    'capitalization.
    If ActiveDocument.Path = SaveDir Then
    MsgBox FName & " has been saved to " & SaveDir, _
    vbInformation
    Else
    MsgBox "The copy to the hard drive failed.", _
    vbCritical
    End If
    MsgBox FName & " has been saved to " & ActiveDocument.Path()
    Done:
    End Sub
    </pre>

    <hr>

    Hope you can understand my spagetti<g>

  9. #9
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    ** edited Geoff W. Split long lines ***

    Finally got it to work. I changed the IF test to
    <pre> If ZipVerDate < HDVerDate Then
    If MsgBox(HDVersion & " is newer than " & ZipVersion _
    & ". Do you want to overwrite " _
    & HDVersion & "(" & HDVerDate & ") with " & _
    ZipVersion & "(" & ZipVerDate & ")?", _
    vbYesNo) = vbNo Then
    GoTo Done
    End If
    End If
    </pre>


    The original was looking for vbYes and was convoluted with an Else. Changing it to vbNo made it cleaner and fixed the problem. Thanks for the chance to think this one through.

    BTW, if anyone else uses QuickSyncII and can use this routine, I'd be glad to share it.

  10. #10
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    Hi,

    You may be better posting code as a .txt attachment. It keeps the formatting (unlike code without "pre" tags); it can be copied anxd pasted without mucking about with manual line breaks (unlike code with "pre" tags); and lines you've accidentally left too long don't cause display problems (unlike code with pre tags).
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  11. #11
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    Geoff,

    Do you mean a text file attachment or pasteing a text file?

  12. #12
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    Rory,

    Everything is working now but I still have one problem which was introduced when the date/time comparison started working.

    The routine chokes on the line that tries to get the date/time stamp from the HDVersion if the file does not exist on the hard drive. I couldn't find a VBA test for whether the file exists in order to avoid an error (in JS it's as easy as
    <pre>if (HDVersion) {stuff to execute if it exists}</pre>

    ).

    I tried
    <pre>If Err = 53 Then GoTo LabelThatSkipsTheComparison</pre>


    and <pre> On Err = 53 GoTo . . .</pre>

    ,
    but neither one trapped the Error 53. The only thing that worked was
    <pre>On Error GoTo LabelThatSkipsTheComparison</pre>

    at the beginning of the routine. This works, but it is not satisfactory because it treats all errors the same way.

    Any suggestions? (BTW Error 53 is "File not found".)

  13. #13
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    To find out if a file exists, use the FileSystemObject's FileExists method. You'll need to set a reference to the Microsoft Scripting Runtime.(Tools References...)

    Dim oFSO as FileSystemObject
    Set oFSO = New FileSystemObject

    If oFSO.FileExists(Fullpath & filename) = True _
    Then
    ' File Exists!
    Else
    ' File doesn't exist.
    End If

    Set oFSO = Nothing

    I can highly recommend VB & VBA In A Nutshell by Paul Lomax published by O'Reilly. It's an excellent reference to the VBA language.

    Mike

  14. #14
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    Sorry, I meant attaching a text file- as I've done in <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=vb&Number=16090&page=1& view=expanded&sb=5#Post16090>this post</A>.

    Pasting text into a message loses all leading spaces when the message is viewed.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  15. #15
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileDateTime(pathname)

    How is this method better than the simpler "DIR"?

    I've used the FileScripting method in the past, but not recently.

Page 1 of 2 12 LastLast

Posting Permissions

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