Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help incrementing, won't go past 1 in loop 1

Status
Not open for further replies.

DCSage

Programmer
Mar 5, 2010
74
US
I am getting an error that the file already exists. How can i successfully increment to the next letter? if the file with 'A' doesn't exist, the file will be written, but it it does, I get an an error that it exist. i need to go to the next letter if this is recognized. I tried, do loops, while, etc. I am including the code with the error

Code:
            string newFileName;

            char letter = 'A';
            letter = (char)((byte)letter + 1);

                if (!File.Exists(dupePathDir))
                {
                     while (true)               
                       {
                        newFileName = fname + "_" + letter.ToString() + extension;                    
                        return newfilename;
                       }
                }                
         
        return null;
           }
 
Corrected code for letter, but won't go past 'a' if file exists still:

Code:
string newfile;

            char letter = 'a'; 
            int i = 0;

            while (i < 26)
            {
                newfile = fname + "_" + letter.ToString() + extension;
                if (!File.Exists(dupePathDir))
                {
                    if (File.Exists(newfile))
                    {
                        letter++;                            

                        string whatfile = fname + "_" + letter.ToString()+1 + extension;
                        return whatfile;
                        i++;
                    }
                    else
                    {
                        return newfile;
                        i++;
                    }
                }
            }

            return null;
        }
 
I would get all the files in the directory, parse the file names for the pattern you are looking for, then increment by 1. how you increment a letter may be a little tricky, since you can do i++.
Code:
var letters = new[]{a,b,c,d,e...};
var lastfile = Directory
   .GetFiles()
   .Select(file=> {
                     return //log to parse file name
                  })
   .OrderBy(file=>file)
   .LastOrDefault();

if(lastfile == null)
{
   use letters[0] //a
}
else
{
   var index = letters.IndexOf(lastfile);
   if(index == letters.Length-1)
   {
       last letter. what next?
   }
   else
   {
       user letters[index+1] //next letter
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thank you jmeckley for your help!

I had a few problems with the function and it's finally resolved:

My main issue was the paths and my process. I failed to check the existence of the file in the destination directory.

Final Code:

Code:
public static string appendLetter(string somefile)
        {
            string dupePathDir = @"J:\Dealmaker\EDI_TG\Sent to MSA\";

            String[] thebigFiles = Directory.GetFiles(dupePathDir, "*.edi");

            string newfilename = Path.GetFileName(somefile);
            string extension = Path.GetExtension(somefile);
            string fname = newfilename.Replace(extension, String.Empty);
            string newfile;

            char letter = Char.Parse("a");
            char letterEnd = Char.Parse("z");

            for (char i = letter; i < letterEnd; i++)
            {
                newfile = dupePathDir + fname + "_" + i.ToString() + extension;

                if (!File.Exists(newfile))
                    return newfile;
            }
            return null;
        }

Thank you again!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top