C# search for file and move-rename

Raul77
Raul77 used Ask the Experts™
on
I have a directory that is similar to below

c:\docs\P232\\_DOC (NO EXTENSION)
c:\docs\P445\_DOC (NO EXTENSION)
C:\docs\P898\test\393   (notice no _DOC)

and so on, now what i need to do is have my program go through ALL the directories in C:\docs\ and fine all files, if the file name is _Doc , take the file copy it to another directory and rename it. for example for the first one
file will be moved to
c:\rename and will be called P445.doc
and second one will be P898.Doc
third one will not be copied since no _doc.

any idea to get me started?
(Code will be in C#)

Thanks,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
In addtion to my comments on the original question, I noticed that GetDirectories() can recurse for you, making it much simpler.

Explore System.IO, particularly File, Directory and Path. They allow you do a lot of useful things.
Sub SearchFolder(ByVal startingFolder As String)
   For Each dir As String In System.IO.Directory.GetDirectories(startingFolder, "*doc*", System.IO.SearchOption.AllDirectories)
       Console.WriteLine(dir)
 
   Next
End Sub

Open in new window

Author

Commented:
this is it in C# correct?
public void SearchFolder(string startingFolder)
{
foreach (string dir in System.IO.Directory.GetDirectories(startingFolder, "*doc*", System.IO.SearchOption.AllDirectories)) {
Console.WriteLine(dir);
}
}

Open in new window

Doh. Why did I think you were using VB? I actually went out of my way to figure it out in VB, because I'm a c# guy. :)

It's easy enough to convert it, though!

You might want to whip up a quick console application to help you work through this, building each layer on top of the previous concept that you figure out.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Adapted from the previously asked question...
DirectoryInfo di = new DirectoryInfo(@"C:\Docs");
FileInfo[] fis = di.GetFiles(@"*.*", SearchOption.AllDirectories);
foreach (FileInfo fi in fis) 
{
    if (fi.DirectoryName.ToLowerInvariant().Contains("_doc")) 
    {
        fi.CopyTo(Path.Combine(
            @"x:\target_folder\",  
            "P" + fi.Directory.Parent.Name + ".doc"
        ));
    }
}

Open in new window

Sorry, I've just seen that the file name is _doc, not the parent folder.  This makes it a little easier:
DirectoryInfo di = new DirectoryInfo(@"C:\Docs");
FileInfo[] fis = di.GetFiles(@"_doc", SearchOption.AllDirectories);
foreach (FileInfo fi in fis) 
{
    fi.CopyTo(Path.Combine(
        @"x:\target_folder\",
        "P" + fi.Directory.Name + ".doc"
    );
}

Open in new window

Author

Commented:
Jim: the problem is what if the directory does NOT start with P ? for example

c:\docs\K485\_DOC (NO EXTENSION)

also your code doesn't compile.

Thanks,
>> also your code doesn't compile.
Probably a missing ), but it would be helpful to know what the error was.  

>> the problem is what if the directory does NOT start with P ? for example
Not explicitly specified in your examples.  Lose the '"P" + ' bit from the example, since the letter/number combo is the directory name, anyway...
DirectoryInfo di = new DirectoryInfo(@"C:\Docs");
FileInfo[] fis = di.GetFiles(@"_doc", SearchOption.AllDirectories);
foreach (FileInfo fi in fis) 
{
    fi.CopyTo(Path.Combine(
        @"x:\target_folder\",
        fi.Directory.Name + ".doc"
    ));
}

Open in new window

Author

Commented:
List of errors :

error CS1519: Invalid token 'foreach' in class, struct, or interface member declaration
error CS1002: ; expected
error CS1519: Invalid token ')' in class, struct, or interface member declaration
error CS1519: Invalid token '(' in class, struct, or interface member declaration
error CS1519: Invalid token '(' in class, struct, or interface member declaration
error CS1519: Invalid token '+' in class, struct, or interface member declaration
error CS1022: Type or namespace definition, or end-of-file expected
You do realise that I posted a method that you need to put inside of a class, don't you...?
Have you done much C# development before?

Author

Commented:
Thanks Jim, we are almost there, what it does not is if the file is in
c:\docs\P445\_DOC (NO EXTENSION)
it renames it P445.DOC (WHICH is good) BUT what should i do if if the file is here (we could have a mix of the 2. what i would want is ALWAYS name the file STARTING with P445 up to _ then add - then add the name of the last folder then .doc  

c:\docs\P445_DOC\test\docs\_DOC (NO EXTENSION)

so above file name will be P445-docs.doc

OR

c:\docs\P4885\test\last\_DOC
would be P4885-last.doc

Thanks.

Author

Commented:
nvm got it :)

Author

Commented:
Thanks guys.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial