如何将一个特定的扩展名更改为另一个? [英] how do you change a specific extension to another?

查看:127
本文介绍了如何将一个特定的扩展名更改为另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我知道如何将文件夹中的所有文件更改为扩展名.但是现在,我正在尝试查找具有特定扩展名的文件夹中的所有文件,并将其更改为另一个文件.谁能告诉我如何?谢谢.


由于某种原因,我无法在我的应用程序中实现OriginalGriff的代码.这是我的代码.

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)
        {

        }
    }
}

推荐答案

在命令提示符下使用FOR:
Use FOR in the command prompt:
c:\> for /r %1 in (*.pdb) do rename "%1" "%~n1.ppp"


上面的代码将递归搜索所有具有PDB扩展名的文件,并将它们重命名为PPP扩展名.


The above will search for all files with the PDB extension recursively and rename them to a PPP extension.


这很简单:Directory.GetFiles有一个接受模式的重载:
It''s pretty simple: Directory.GetFiles has an overload which accepts a pattern:
string[] files = Directory.GetFiles(@"D:\Temp\", "*.jpg");


然后,您可以像以前一样使用File.Move.

但是您确实意识到,更改扩展名并不能像新扩展名一样神奇地可读,不是吗?即使您将"AVI"文件的扩展名重命名为"DOCX",它仍然是视频文件吗?那个Word无法打开它?


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?


我认为这段伪代码将帮助您

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
}



更正了我对Wes ADay的建议的解决方案



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

}


这篇关于如何将一个特定的扩展名更改为另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆