Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2004, 04:14 PM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Enumerate files from a folder & get its timestamp

Hi,

I need help in VB.net.
I have a folder say "d:\test".
sort the files based on the "Time Modified"
At regular intervals this program should run so that copy all the files in that folder to another folder say "c:\temp". And should write the last filename and its timestamp in a text file.
The next time when the program runs, It should copy only files which are after the last filename that is updated in the text file. Now I should note the lastfilename in the text file.

EXAMPLE:

d:\test
filename TimeModified
1.txt 30, 2004, 12:36:22 AM
2.txt 30, 2004, 12:30.22 AM
3.txt 30, 2004, 12:29.22 AM
4.txt 30, 2004, 12:29.22 AM

When the Program runs at 30,2004, 12:30.24 AM
the program should copy only the following files to "c:\test"
2.txt 30, 2004, 12:30.22 AM
3.txt 30, 2004, 12:29.22 AM
4.txt 30, 2004, 12:29.22 AM

and should write to a text file last file name "2.txt" and its Timestamp " 12:30.22 AM"

Now the 1.txt will come fall into the folder "d:\test" at 30, 2004, 12:36:22 AM

When the program runs again say at 30, 2004, 12.37.00 AM it should copy only
1.txt 30, 2004, 12:36:22 AM
and should write the lastfilename as 1.txt and its timestamp as 12:36:22 AM

PLEASE HELP PLEASE HELP PLEASE HELP PLEASE HELP PLEASE HELP
 
Old March 11th, 2004, 06:05 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I would do something like this:

' set up paths...
Dim destPath As String = "", sourcePath As String = ""
' get a reference to the directory...
Dim di As New DirectoryInfo(sourcePath)
' get all the files in the directory...
Dim fis() As FileInfo = di.GetFiles()
' array to hold all files that have already been copied, must be
' static so that it persists...
Static fisCopied() As FileInfo
' reference to an individual file...
Dim fi As FileInfo

' go through all files in the directory...
For Each fi In fis
    If fisCopied.LastIndexOf(fisCopied, fi) > 0 Then
        ' file has already been copied, so do nothing...
    Else
        ' file hasn't yet been copied, so copy...
        fi.CopyTo(destPath)
        ' add the file to the list of files that have
                ' been copied...
        ReDim Preserve fisCopied(fisCopied.GetUpperBound(0) _
                                                             + 1)
        fisCopied(fisCopied.GetUpperBound(0)) = fi
    End If
Next

Therefore, once a file has been copied, it won't be copied again. If you need to write out any further details of the file being copied, just interrogate the properties (I'm not sure if the fileinfo object is serialisable, but this would be the easiest way of saving it). Further to this, if the fileinfo object is serialisable, you could save the fisCopied object rather than having it as a static variable, if the calls to the sub are pretty infrequent and you don't want to hold fisCopied in memory for an extended period.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Create backup folder & also backup files miracles Excel VBA 0 April 16th, 2007 02:28 AM
How to Zip files within a folder Udi C# 2 January 25th, 2007 12:45 AM
return files in folder Dj Kat Classic ASP Basics 2 February 9th, 2006 04:23 AM
save files into a folder jessiang General .NET 3 January 16th, 2006 09:48 AM
FILES in FOLDER luma SQL Server DTS 0 June 9th, 2005 01:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.