• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

add text filename into dataset.

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 vb.net 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
  • 4
  • 3
1 Solution
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
Bob LearnedCommented:
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:


nyee84Author Commented:
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)vb.net windows application
3)No ,Single user
4)not really know much about com library.


Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.

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

nyee84Author Commented:
Hi Bob,

What will be the advange of using logparsor for my scenario. Kindly advice.
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 ;)
nyee84Author Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now