add text filename into dataset.

Posted on 2006-04-28
Last Modified: 2010-08-05
My current program will copy txt files from c:A folder into c:\B folder however i need to check the the same text file exist in folder B or not. The current method i am using is the file.exist and it was very unfortunate that my c:\B folder got 2000  overfile and the current method(file.eixst) taking long time and which is not acceble for the user also. One expert adviced me that we can add all the txtfilenames into dataset and while moving files into c:\B, each filename need to be comapared against the dataset and will eliminate the process time also . is it possible? and pls provide the code also.

my sample text file name is  SD1245-4424.txt. Pls advice
Question by:nyee84
    LVL 9

    Expert Comment

    Is it a multi user program?
    If it is not, then when the program starts get the names of the files in c:\B =>
    dim files() as string = system.IO.Directory.GetFiles("c:\B")
    This will give you an string array but arrays are static (you can't add new item to it -at least not easily)

    Therefore, create an arraylist,
    dim xx as new arraylist

    in a loop, insert all the files you have in to the arraylist

    for i as integer = 0 to files.length-1

    then use the contains method before coping

    if not xx.contains("abc.txt") then
       copy from c:\a to c:\b
    end if

    If multiple instances of your program can run from different machines and works on the same folder (c:\B)
    then use a filesystemwatcher and add the newly copied files to your arraylist
    LVL 96

    Expert Comment

    by:Bob Learned
    1) What type of application are you creating?
    2) What .NET version do you have?
    3) Are you going to distribute this to many different types of users?
    4) Would you be willing to interoperate with a COM library.

    Here is a previous C# question, but I also have a VB.NET class that handles doing very fast LogParser file queries:


    Author Comment

    Hi tolg,

    Only one user going to use my program and basically the program should copy all the files from c:\A folder into C:\B folder and while coping each files in c:\A
     need to compare agianst all the files in c:\B , if same text file exist in c:\B then i got move the existing file into C:\archive folder and new file will be copied to the c:\B folder.

    My c:\A can have max of 200 files need to copy to C:\B and need check agianst all the filename in C:\B(1200 files), pls explain in detail how can i use array list for this scenario.

    If the same text file exist in c:\B folder, then i need to copy the existing files into c:\archive folder. i also have issue with "concurrency" cos while moving file into archive folder, the file might be opened by some of my user, so what will happen  to the program, how can i over this issue. Since i am very new programming pls explain everything in detail .. Thanks

    Hi LearnedOne,
    1) windows application
    3)No ,Single user
    4)not really know much about com library.


    LVL 9

    Accepted Solution

    Here is my solution for the case the copied file is not in use:

        Private theFiles As New ArrayList()
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim temp() As String = System.IO.Directory.GetFiles("c:\B")
            For i As Integer = 0 To temp.Length - 1
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim candidateFileToCopy As String = "SD1245-4424.txt"
            If theFiles.Contains(candidateFileToCopy) Then 'This file is in the folder. Son copy it to archive first
                System.IO.File.Copy("c:\b\" & candidateFileToCopy, "c:\archive\" & candidateFileToCopy)
                System.IO.File.Copy("c:\a\" & candidateFileToCopy, "c:\b\" & candidateFileToCopy)
            Else ' there is no file named like that
                System.IO.File.Copy("c:\a\" & candidateFileToCopy, "c:\b\" & candidateFileToCopy)
            End If
        End Sub

    If it is in use? I think the best method is try again later ;)
    For this purpose, My advice is to
    1) create a thread.  => easy
    2) Send the file name as parameter => difficult, you should use delegate and I'm not familiar with that stuff. I hope somebody else can help us
    3) try the above two line code in a try catch. If there is an access violation (file in use) then the catch statement will catch it and you can sleep the tread for 3 minutes lets say and try again when it is alive again. and you can continue the usaul program flow as it is copied.

    This will work if your program does not end before the copy operation ends.
    You may find better solutions (Writing to a file or registry) but you will agian use "try again later" method.


    Author Comment

    Could you elucidate the term try again later" method clearly.


    Author Comment

    Hi Bob,

    What will be the advange of using logparsor for my scenario. Kindly advice.
    LVL 9

    Expert Comment

    By saying try again later
    I mean catch the exception and try again :)
    Here is a sample code

    private void copyFunction
       System.IO.File.Copy("c:\b\" & candidateFileToCopy, "c:\archive\" & candidateFileToCopy)
    catch exp as exception
        'Suppose the file is inuse and the copy operation could not be completed. Then use the below code
        system.threading.thread.sleep(5000) ' Sleep for 5 seconds.
       copyFunction() 'İt will call itself recursively until the copy operation is successfully completed.
    end try
    end function

    If you can put the above code in a different thread it will be better ;)

    Author Comment

    Hi tog,

    Kindly explain the use of arraylist in detail for the below scenario . c:\projects folder files need to move into C:\SE or c:\SA
    base on the text filenames in c:\projects folder.

    My filename will be as follows SA12345_07-Feb-06_0831.txt
    so if my file starts with SE then need to move the files into C:\SA folder and if the filename starts with SE then i need to move the file into C:\SE. While moving fle into designated  folders i need to check whether same file exist in desidnated folder or not.

    The current method is i will all the files from both SA and SE into an arraylist and while moving files from c:\projects,
    i will check against my arraylist and files which is of a earlier data need to move into c:\archive folder.

    For ex my exiting file in c:\SE folder is SA12345_07-Feb_06_0831
    My file need to move from c:\project also SA12345_07-Feb_06_1031. earliest time and date is obviously SA12345_07-Feb_06_0831, moving into c:\archive.

    Inorder to do this task, i need to store all the filenames from C:\se and c:\SA into an arraylist while moving files from c:\projects, i need to compare with the arraylist also .

    Which method is faster? storing in sql server database table or storing in arraylist. File size in c:\SE and c:\SA growing also.  Kindly advice


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now