Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
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 21-Aug-12 5:26am
Edited 21-Aug-12 10:41am
v2
Comments
MR. AngelMendez at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Marcus Kramer at 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 at 21-Aug-12 11:50am
   
Thanks Marcus!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

It's pretty simple: Directory.GetFiles has an overload which accepts a pattern:
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?
  Permalink  
Comments
MR. AngelMendez at 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 at 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 at 21-Aug-12 13:37pm
   
yeah I was one of those people lol!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I think this pseudo code will help you
 
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
 
foreach (File file in Directory.GetFiles("D:\.....","*.jpg"))
{
 
//Change the extension using the logic you already know

}
  Permalink  
v2
Comments
Wes Aday at 21-Aug-12 10:41am
   
Why not just get the .jpg files with GetFiles and eliminate the if statement?
aspnet_regiis at 21-Aug-12 10:43am
   
Yes. You are right.. Thanks again :).. I will post the edit...
aspnet_regiis at 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... :(
Wes Aday at 21-Aug-12 10:55am
   
It's not wrong. Do not worry about it.
aspnet_regiis at 22-Aug-12 2:06am
   
Thank you Wes :)
aspnet_regiis at 22-Aug-12 4:58am
   
Somebody is enjoying in downvoting me again...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 200
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100