-4

This is challenging for me ,hope so not for you, please give me solution..

I have images in folder like :(D:\Pictures\MyImages\logo.png & D:\Pictures\MyImages\u1.png)and like this i have 20 folder in Picture folder, in this 20 folder, there are 4 images(with random name like 1.png, w1.png, e3.jpeg) and one logo in each.

Is there a way to change the name of all images(1.png, w1.png, e3.jpeg) into "image.jpg, image1.jpg, image2.jpg" at a time apart from logo.

Thanks Waiting for positive response.

Christian MICHON
  • 2,140
  • 2
  • 19
  • 30
  • 3
    Welcome to StackOverflow. This is not a code-writing service. You have a requirement, now you need to research how to do it, and attempt a solution yourself. Post additional questions *if you run into problems writing code*. – Chris Mantle Jul 03 '15 at 07:04
  • See [here](http://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp) for renaming. See directory.GetFiles for getting the filenames... – TaW Jul 03 '15 at 07:05
  • Why would you rename 1.png as image.jpg? It would not be working at all, it's not just about how to ask questions, your example must be legit. – Christian MICHON Jul 03 '15 at 07:35

1 Answers1

0

All the above comments are respected. This code will change all the files naming (1.png, w1.png, e3.jpeg) into "image.jpg, image1.jpg, image2.jpg" inside the directory D:\Pictures.

 string path = @"D:\Pictures";
                DeleteFolderAndFile(new DirectoryInfo(path));

Put the Above Code on Page_Load or at button Click

static public void DeleteFolderAndFile(DirectoryInfo directoryInfo)
    {
        try
        {
            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                try
                {
                    string sourceFolder = directoryInfo.ToString();
                    string filename = file.ToString();
                    string filenamepath = sourceFolder + "\\" + filename;
                    string newNamepath;
                    if (filename == "1.png")
                    {
                        newNamepath = sourceFolder + "\\" + "Image.jpg";
                        File.Copy(filenamepath, newNamepath, true);
                        File.Delete(filenamepath);
                    }
                    if (filename == "w1.png")
                    {
                        newNamepath = sourceFolder + "\\" + "Image1.jpg";
                        File.Copy(filenamepath, newNamepath, true);
                        File.Delete(filenamepath);
                    }
                    if (filename == "e3.jpeg")
                    {
                        newNamepath = sourceFolder + "\\" + "Image2.jpg";
                        File.Copy(filenamepath, newNamepath, true);
                        File.Delete(filenamepath);
                    }
                }
                catch
                {
                    //Do Nothing
                }
            }
            foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories())
            {
                try
                {
                    DeleteFolderAndFile(subfolder);
                }
                catch
                {
                    //Do Nothing
                }
            }
        }
        catch
        {
            //Do Nothing
        }
    }
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40