AET
AET

Reputation: 1

Stepping through directories and sub-directories

I have code that steps through a main directory and all the sub directories. The images in each sub directories needs to be renamed as per the folder it is ins name.

C:\Users\alle\Desktop\BillingCopy\uploaded 27-02\\Batch002-190227010418829\PPA14431564096\File1.png

should rename to

C:\Users\alle\Desktop\BillingCopy\uploaded 27-02\Batch002-190227010418829\PPA14431564096\PPA14431564096.png

I can see the code is stepping through every thing but the image isn't beeing renamed and I can't see where I went wrong

while(isTrue)
            {
                try
                {
                    //write your code here
                    string filename1 = "1.tif";
                    string newFileName = "allen.tif";

                    string[] rootFolder = Directory.GetDirectories(@"C:\Users\alle\Desktop\BillingCopy");

                    foreach(string dir in rootFolder)
                    {
                        string[] subDir1 = Directory.GetDirectories(dir);

                        foreach(string subDir in subDir1)
                        {
                            string[] batchDirList = Directory.GetDirectories(subDir);

                            foreach(string batchDir in batchDirList)
                            {
                                string[] waybillNumberDir = Directory.GetDirectories(batchDir);

                                foreach(string hawbDir in waybillNumberDir)
                                {
                                    string waybillNumber = Path.GetDirectoryName(hawbDir);

                                    string[] getFileimages = Directory.GetFiles(hawbDir);

                                    foreach(string imgInDir in getFileimages)
                                    {
                                        File.Copy(imgInDir, Path.Combine(@"C:\Users\alle\Desktop\Copy", string.Format("{0}.{1}", waybillNumber, Path.GetExtension(imgInDir))));
                                    }
                                }
                            }
                        }
                    }

                    File.Copy(Path.Combine("source file", filename1), Path.Combine("dest path", 
                        string.Format("{0}{1}", Path.GetFileNameWithoutExtension(newFileName), Path.GetExtension(newFileName))), true);
                }
                catch { }
            }

Upvotes: 0

Views: 73

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186823

When querying you can try using Linq to obtain the required data:

   // All *.png files in all subdirectories
   string rootDir = @"C:\Users\alle\Desktop\BillingCopy";

   var agenda = Directory
    .EnumerateFiles(rootDir, "*.png", SearchOption.AllDirectories)
    .Select(file => new {
      oldName = file,
      newName = Path.Combine(
        Path.GetDirectoryName(file), 
        new DirectoryInfo(Path.GetDirectoryName(file)).Name + Path.GetExtension(file))
    })
    .ToArray();

Then we can move (not copy) the files:

  foreach (var item in agenda)
    File.Move(item.oldName, item.newName);

Upvotes: 1

Related Questions