0

What I have is a listbox being populated by a textbox.

I want to search in a specific directory for all files that match the listbox criteria. I want do do this for each listing in the listbox, then I want to copy out all the matching files to another directory.

So Listbox contains: Apple Orange Fruit

i want to copy apple*.txt to destiondirectory, then copy orange*.txt to destination directory, and fruit*.txt to destinationdirectory.

After everything has been copied i want to create a text file of each thing being copied to it's own text file. So a directory listing from the destinationdirectory.

So i would just get a text file of all the files that match a specific criteria IE apple*

Thanks for the help and advice.

   string[] filesToCopy = listBox1.Items.
        string sourcefolder1 = @"K:\rkups";
        string destinationfolder = @"K:\g_aa_ge\qc";
        {
            string source = Path.Combine(sourcefolder1, filesToCopy[] + ".ann");
            string target = Path.Combine(destinationfolder, filesToCopy[] + ".ann");
            File.Copy(source,target);

 DirectoryInfo di = new DirectoryInfo(destinationfolder);
        FileInfo[] annfiles = di.GetFiles(string+"*.txt);
        foreach(FileInfo fi in annfiles)

the string+ is where i dont understand where/how to list each item in the listbox, and where string[] filesToCopy = listBox1.Items. not sure how to list each item in the string

updated: 1) read each item in listbox
2) try to copy from a sourcedirectory to a destinationdirecory the item in listbox
3) repeat

thats it

6
  • 3
    too many requirements... can you re-tool your question a little better? Commented Dec 18, 2009 at 17:07
  • Are you trying to get a list of the files that matches a pattern or or you trying to copy files and have a log of the files copied? It sounds like you're copying the files to generate the list, not because you want the files copied. Commented Dec 18, 2009 at 17:08
  • 2
    Adding to what Roboto said, this feels like you're asking us to do your work for you rather than asking about a specific issue you are stuck on. Commented Dec 18, 2009 at 17:09
  • I get the basic design, but can you let us know where you are stuck? As Eric says this seems like your asking the community to write it for you. Commented Dec 18, 2009 at 17:16
  • Sorry for all the confusion. What i dont understand is how to list the items in a listbox in a string and do a foreach loop. Commented Dec 18, 2009 at 17:18

2 Answers 2

1

I made a small example which is doing more or less what you wanted except generateing the log file.

You should be able to work it from there.

In my example, the code was just populating a second text box with the names of the copied files. It was tested and compiled.

Hope this helps ! Anthony

private void button1_Click(object sender, EventArgs e)
{
    string dirInput = "c:/test";
    string dirOutput = "c:/test2";
    listBox2.Items.Clear();

    bool overwriteFilesInOutputDir = true;

    if (Directory.Exists(dirInput))
    {
        if (!Directory.Exists(dirOutput))
            Directory.CreateDirectory(dirOutput);

        DirectoryInfo di = new DirectoryInfo(dirInput);
        foreach (string filterItem in listBox1.Items)
        {
            FileInfo[] rgFiles = di.GetFiles(filterItem);
            foreach (FileInfo fi in rgFiles)
            {
                File.Copy(fi.FullName, dirOutput + Path.DirectorySeparatorChar + fi.Name, overwriteFilesInOutputDir);
                listBox2.Items.Add(fi.Name);
            }
        }
    }
}

Like other people mentioned, it would help if you would try to do it yourself first and ask when you are stuck.

listBox1 contains the filters such as ".xls" or ".asp", listBox2 was just for me to check the names of the files copied.

Anthony

Sign up to request clarification or add additional context in comments.

1 Comment

absolutely, i was missing such a small piece i thought it would be a 10 second answer, but i gotta learn how to ask it better
0

I'm still a little confused on what you want to do, but I fixed up your code for you...

string source, fileToCopy, target;
string sourcefolder1 = @"K:\rkups";
string destinationfolder = @"K:\g_aa_ge\qc";
DirectoryInfo di = new DirectoryInfo(destinationfolder);
FileInfo[] annfiles;

foreach (string s in listBox1.Items)
{
     fileToCopy = s;
     source = Path.Combine(sourcefolder1, fileToCopy + ".ann");
     target = Path.Combine(destinationfolder, fileToCopy + ".ann");
     File.Copy(source, target);

     annFiles = di.GetFiles("*.txt");

     // Do whatever you need to do here...

}

2 Comments

annFiles will be overwritten for each string in the listbox with this code.
@AC: Not sure on his implementation, so this may be preferred.. I dont see the code beneath this so it is a stab in the dark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.