Skullomania
Skullomania

Reputation: 2215

How do I properly auto append to filename if file exist

Here is what I have tried, please note that lblImageAlt.Text property has been set to 'Images/'

        string ImgPath1 = lblImageAlt1.Text.ToString();
        string ImgPath2 = lblImageAlt2.Text.ToString();
        string ImgPath3 = lblImageAlt3.Text.ToString();

        string filename1 = "";
        string filename2 = "";
        string filename3 = "";


        if (fileuploadimages1.HasFile)
        {
            if (File.Exists(Server.MapPath(ImgPath1 + filename1)))
            {
                string extension = Path.GetExtension(filename1);
                string name = Path.GetFileNameWithoutExtension(filename1);
                int fileMatchCount = 1;
                while (File.Exists(Server.MapPath(ImgPath1 + name + "(" + fileMatchCount + ")" + extension)))
                    fileMatchCount++;

                fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + name + "(" + fileMatchCount + ")" + extension));
            }
            else
            {
                fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + filename1));
            }
        }
        else
        {
            filename1 = "noImage.jpg";
        }

but the same image does not get a number appended to it. What am I doing wrong here?

Upvotes: 1

Views: 1219

Answers (3)

Steve
Steve

Reputation: 216293

Path.GetFileName returns the whole filename with the extension.
Thus your code is checking if a file exists with a name like this:

 image.jpg1

You should change the code to split the filename in two parts, the base filename and the extension, then check if the filename exists and then rebuild the filename from its parts adding the increment number until you find a non existant filename

// Extract just the filename from the posted file removing the path part (image.jpg)
filename1 = Path.GetFileName(fileuploadimages1.PostedFile.FileName);
baseFile = Path.GetFileNameWithoutExtension(fileuploadimages1.PostedFile.FileName);
extension = Path.GetExtension(fileuploadimages1.PostedFile.FileName);
int fileMatchCount = 1;

// Check if a file with the given name exists in the Images1 subfolder of the root folder of your site
while(File.Exists(Server.MapPath(Path.Combine(ImgPath1, filename1)))
{
    // The given file exists already, so we now need to build
    // a different (but related) filename using a counter....

    // This will create a filename like 'image(001).jpg'
    // and then we will restart the loop        
    fileName1 = string.Format("{0}({1:D3}){2}", baseFile, fileMatchCount, extension);

    // ... but first increment the counter in case even the new name exists
    fileMatchCount++;
}

// We exit the loop with a name that should not exists in the destination folder
fileuploadimages1.SaveAs(Server.MapPath(Path.Combine(ImgPath1, filename1));

Upvotes: 3

Mike Precup
Mike Precup

Reputation: 4218

Try using

    if(File.Exists(Server.MapPath(ImgPath1 + filename1)))
{
    string extension = Path.GetExtension(filename1);
    string name = Path.GetFileNameWithoutExtension(filename1);
    int fileMatchCount = 1;
    while(File.Exists(Server.MapPath(ImgPath1 + name + "(" + fileMatchCount + ")" + extension)))
        fileMatchCount++;

    fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + name + "(" + fileMatchCount + ")" + extension));
}
else
    fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + filename1));

Upvotes: 2

Matt Houser
Matt Houser

Reputation: 36073

You're not actually modifying filename1. You're checking if it ends in a (0), (1), etc. and incrementing your index, but never actually modifying the variable.

Upvotes: 2

Related Questions