copy last modified file from directory Visual Basic
Results 1 to 11 of 11

Thread: copy last modified file from directory Visual Basic

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Bentleyville, Pa
    Posts
    462

    Post copy last modified file from directory Visual Basic

    okay at work we get credit card transaction from a unix mailbox that need moved to a PC network drive then transferred via FTP to our mainframe drives so that the cobol programs can manipulate the data.

    the problem is that the file is not named with any pattern at all (like the date it's recieved) so I never know what the name of the file will be. Only that it will have a .dat extension. What I am trying to do is look into the folder where the files are and copy the last modified .dat file to the network drive and name it greentre.dat. can anyone guide me as to how to start.

  2. #2
    Registered User Deity's Avatar
    Join Date
    Mar 2001
    Location
    Elsewhere
    Posts
    1,412

    Post

    What type of system is the .dat file stored on and what computer will you run this program from?

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Bentleyville, Pa
    Posts
    462

    Post

    the .dat file is downloaded to our computer room PC running win98 in a local folder. (we pull it in using procomm plus) I'll just be executing the command from the local pc to send the last created file from the local folder to the network drive as a different name.

    now I have to use dos command
    cd\
    cd progra~1\procom~1\downloads
    copy *032.dat i:\xmit\greentre.dat
    (I use last 3 digits of whatever the file is named that day)

    If they would just send them to us with some logical pattern to the batch name It would be easy. I've gotten our other credit card report file figured out but it uses the date of the file in it's name.

  4. #4
    Registered User Deity's Avatar
    Join Date
    Mar 2001
    Location
    Elsewhere
    Posts
    1,412

    Post

    Try this:

    [code]
    Dim fso, Folder, FileList, File, LastMod As Variant
    Dim dHigh As Long

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Folder = fso.GetFolder("C:\MyDirectory")
    Set FileList = Folder.Files

    dHigh = 0

    For Each File In FileList
    If File.DateLastModified > dHigh Then
    dHigh = File.DateLastModified
    LastMod = File.Name
    End If
    Next File
    ' Now you have the file name in LastMod.
    ' FTP that filename
    </pre><hr></blockquote>

    This should get you the last modified file in the directory you specify for MyDirectory.

  5. #5
    Registered User Deity's Avatar
    Join Date
    Mar 2001
    Location
    Elsewhere
    Posts
    1,412

    Post

    Do you already have a method to FTP the file once you've found it?

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Bentleyville, Pa
    Posts
    462

    Post

    [quote]Originally posted by Deity:
    <strong>Do you already have a method to FTP the file once you've found it?</strong><hr></blockquote>

    diety,

    thank you again. I do have a FTP script on the mainframe side which will pull the file from the PC network drive.

    one more quick questions. Can I specify only .dat files in the code you sent me, and where specifically(just in case there is a text file or something in there too)

  7. #7
    Registered User Deity's Avatar
    Join Date
    Mar 2001
    Location
    Elsewhere
    Posts
    1,412

    Post

    Just modify the For..Each Loop to check for .dat files like this:

    [code] For Each File In FileList
    If File.DateLastModified > dHigh And Right(File.Name, 3) = "dat" Then
    dHigh = File.DateLastModified
    LastMod = File.Name
    End If
    Next File
    </pre><hr></blockquote>

    This will only check the dat files.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Bentleyville, Pa
    Posts
    462

    Post

    I wish you could teach my class! I've learned more with you helping me twice than the countless hours I've spent in that classroom

    thanks so much

  9. #9
    Registered User Deity's Avatar
    Join Date
    Mar 2001
    Location
    Elsewhere
    Posts
    1,412

    Post

    Do you have a copy of the MSDN library? If you don't you can always find it at Microsoft's site. The MSDN is a great source.

  10. #10
    Registered User Deity's Avatar
    Join Date
    Mar 2001
    Location
    Elsewhere
    Posts
    1,412

    Post

    You also wanted to copy the file to a special folder with a specific name right? Here's how to do that:
    [code]
    fso.CopyFile "C:\MyDirectory\" & LastMod, "Path to new server/directory"
    </pre><hr></blockquote>

    Just tack that line after the loop. Some cleanup is needed also. Just add these lines after the copy.

    [code]
    Set FileList = Nothing
    Set Folder = Nothing
    Set fso = Nothing
    </pre><hr></blockquote>

    This makes the code a little cleaner.

  11. #11
    Registered User Deity's Avatar
    Join Date
    Mar 2001
    Location
    Elsewhere
    Posts
    1,412

    Post

    [quote]Originally posted by kato2274:
    <strong>I wish you could teach my class! I've learned more with you helping me twice than the countless hours I've spent in that classroom

    thanks so much</strong><hr></blockquote>

    I'm glad to help. I was in your shoes once too. Luckily my programming instructor was wonderful. He was a good teacher and is now a good friend.

Bookmarks

Posting Permissions

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