Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I know how to change all files in a folder to an extension. But now I'm trying to look for all of the files in a folder with a specific extension and changing them to another. Can anyone show me how? Thanks.


For some reason I can't implement OriginalGriff's code to my application. Here is my code.

C#
private void BrowseB_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                FolderPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void ReplaceB_Click(object sender, EventArgs e)
        {

// folder is the directory's path.
// find is the extension to find.
// replace is the new extension.

// pretty much the user writes into these textboxes and presses a button to change the extensions.

            string folder = FolderPath.Text;
            string find = FindExtension.Text;
            string replace = ReplaceExtension.Text;

           foreach(string files in Directory.GetFiles(folder,find))
           {
               Path.ChangeExtension(files, replace);
           }




         if (Directory.Exists(folder) == false)
         {
             MessageBox.Show(folder + " could not be found!");
         }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 21-Aug-12 9:41am
v2
Comments
MR. AngelMendez 21-Aug-12 15:42pm    
sorry everyone, it seemed to work but when I tried to implement it in my application it doesn't work. I posted my code above.

Use FOR in the command prompt:
c:\> for /r %1 in (*.pdb) do rename "%1" "%~n1.ppp"

The above will search for all files with the PDB extension recursively and rename them to a PPP extension.
 
Share this answer
 
Comments
fjdiewornncalwe 21-Aug-12 10:50am    
I like this answer, but I have a feeling the OP wants to do this through code and not the command line. (+5 because I like it)
Mehdi Gholam 21-Aug-12 11:50am    
Thanks Marcus!
It's pretty simple: Directory.GetFiles has an overload which accepts a pattern:
C#
string[] files = Directory.GetFiles(@"D:\Temp\", "*.jpg");

You can then use File.Move as before.

But you do realize that changing an extension doesn't magically make it readable as the new extension, don't you? That an "AVI" file is still a video file even if you rename it's extension to "DOCX"? And that Word won't be able to open it?
 
Share this answer
 
Comments
MR. AngelMendez 21-Aug-12 11:11am    
I do, thanks for asking though. The reason why I am doing this is for a graphic design class. I have to make several designs in adobe illustrator and then send them as a .pdf so changing these .ai files are tedious for me. And yes, I know that .ai files are compatible with .pdf. thanks for your help :P
OriginalGriff 21-Aug-12 11:20am    
:laugh:
You'd be surprised how many people asking questions here think that the magic does happen: "I changed my ".DWG" files to ".DOC" but word won't read them. Help!" - so I find it worth making sure right at the beginning!
It's not meant to be patronising, just to cope with...um...the hard of thinking.
MR. AngelMendez 21-Aug-12 13:37pm    
yeah I was one of those people lol!
I think this pseudo code will help you

C#
foreach (File file in Directory.GetFiles("D:\....."))
{
if(file.EndWith(".jpg")
//Change the extension using the logic you already know
}


Edit: Corrected my solution on Wes ADay's suggestion

C#
foreach (File file in Directory.GetFiles("D:\.....","*.jpg"))
{

//Change the extension using the logic you already know

}
 
Share this answer
 
v2
Comments
[no name] 21-Aug-12 10:41am    
Why not just get the .jpg files with GetFiles and eliminate the if statement?
bbirajdar 21-Aug-12 10:43am    
Yes. You are right.. Thanks again :).. I will post the edit...
bbirajdar 21-Aug-12 10:53am    
Okay.. Somebody downvoted my solution. I feel bad ...not for downvote but for not able to know the reason. I will never know where I was wrong... :(
[no name] 21-Aug-12 10:55am    
It's not wrong. Do not worry about it.
bbirajdar 22-Aug-12 2:06am    
Thank you Wes :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900