locked
Help needed generating filenames RRS feed

  • Question

  • User712038305 posted

    I have a sorted array of files saved in the following format of filenames:

    XXX

    XXX_XX

    XXX_XX_XX

    I want to insert a new file (with a unique filename) between two given filenames (which could be in any of the 3 formats above).

    For eg., a new file between 401 and 403 could be inserted with name 402

    and a new file between 401_01 and 401_02 could be inserted with name 401_01_01. etc

    The idea is to conform to the sorted list. Is there a way I could generate filenames for the files to be inserted, given the two filenames between which it needs to be inserted? Please help.

    PS: would it be helpful to use regex?

    Tuesday, August 31, 2010 9:26 PM

Answers

  • User-660870441 posted

    Hi,

    As you said the file names are from a sorted array, so I assumed name1 is always smaller than name2. Here is the method made by me (I assumed you are using C#). Please have try on it.

    protected void Page_Load(object sender, EventArgs e)
    {
        // Test the method by all cases
        Response.Write(GenerateFileName("401_01", "402") + "<br />");
        Response.Write(GenerateFileName("401", "403") + "<br />");
        Response.Write(GenerateFileName("401", "402") + "<br />");
        Response.Write(GenerateFileName("401", "402_01") + "<br />");
        Response.Write(GenerateFileName("401", "403_01") + "<br />");
        Response.Write(GenerateFileName("401_01_03", "401_01_04") + "<br />");
        Response.Write(GenerateFileName("401", "401_01") + "<br />");
    }
    public string GenerateFileName(string name1, string name2)
    {
        // turn names into parts
        List<string> list1 = name1.Split('_').ToList();
        string[] list2 = name2.Split('_');
    
        if (list1.Count > list2.Length) // match 401_01 and 402
        {
            list1[list1.Count - 1] = (int.Parse(list1[list1.Count - 1]) + 1).ToString("D2");
        }
        else if (list1.Count <= list2.Length)
        {
            // only need to compare the last number of name1
            int last1 = int.Parse(list1[list1.Count - 1]);
            int last2 = int.Parse(list2[list1.Count - 1]);
    
            if (last1 < last2 - 1) // match 401 and 403
            {
                list1[list1.Count - 1] = (last1 + 1).ToString("D2");
            }
            else if (last1 == last2 && list1.Count < list2.Length) // match 401 and 401_01
            {
                list1.Add("00");
                list1.Add("01");
            }
            else // match 401 and 402
            {
                list1.Add("01");
            }
        }
    
        // generate the file name
        string result = string.Empty;
        for (int i = 0; i < list1.Count; i++)
        {
            result += list1[i] + '_';
        }
        result = result.Substring(0, result.Length - 1);
        return result;
    }
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 3, 2010 5:28 AM

All replies

  • User-1828494216 posted

    If i understand you... You wish to insert new FileName with name 402 between Filenames 401 and 403 that allready exists in your array?

    I would not complicate an easy thing and would insert new file at the end of array and then i would sort the array again.


    array is indexed (i don't know if this is the right word) and you can't insert new record between existing ones, becouse there is no place... look:

    Filenames(0) = 400

    Filenames(1) = 401

    Filenames(2) = 403

    Filenames(3) = 404

    Now you can't insert new filename between 401 and 403 becouse there is no emtpy "space".

    (You could do that, but you would have to loop trough array, search for the item and relocate exist. items and i just don't see the point of doing that)

    Tuesday, August 31, 2010 9:49 PM
  • User712038305 posted



    array is indexed (i don't know if this is the right word) and you can't insert new record between existing ones, becouse there is no place... look:

    Filenames(0) = 400

    Filenames(1) = 401

    Filenames(2) = 403

    Filenames(3) = 404

    Now you can't insert new filename between 401 and 403 becouse there is no emtpy "space".

    (You could do that, but you would have to loop trough and relocate exist. items and i just don't see the point of doing that)


    I can insert a new filename because the array is not limited in size. So a new filename = 402 can be generated.

    Now suppose if we want to insert a filename between 403 and 404, we can insert 403_01.

    I want to automatically generate the new filename based on the 2 filenames between which it has to be inserted.


    Thanx

    Tuesday, August 31, 2010 9:59 PM
  • User-1828494216 posted

    I can insert a new filename because the array is not limited in size. So a new filename = 402 can be generated.

    Yes, you can insert new item at the end of the array not in the middle ;)

    Now suppose if we want to insert a filename between 403 and 404, we can insert 403_01.

    I want to automatically generate the new filename based on the 2 filenames between which it has to be inserted.

    Ok, now i get the point. You wish to auto generate unique FileNames. And if the FileName gets erased from array you wish to insert new one on that place... something like that?

    May i ask why you need this? Maybe there is an easier solution, becouse i don't know how to help you with this one, becouse i don't know the whole thing.

    For generating FileNames from array i'm sure you know how to write your own logic for this to work. Inserting the file in the "middle" of an array... again, why don't you sort the array after you insert new item at the end?


    Tuesday, August 31, 2010 10:08 PM
  • User-1828494216 posted

    take a look at this:

    http://msdn.microsoft.com/en-us/library/ms132474.aspx

    Tuesday, August 31, 2010 10:18 PM
  • User712038305 posted

    I can insert it anywhere, the array is sorted at a high freq so the naming needs to be very important and it will be placed in the right spot.

    Ok, now i get the point. You wish to auto generate unique FileNames. And if the FileName gets erased from array you wish to insert new one on that place... something like that?

     

    Yes, you hit it right on. However, being a newbie, I do not know the logic to generate the new filename. I have the sorting taken care of which will place it in the exact spot (provided the name is correct).

    Tuesday, August 31, 2010 11:07 PM
  • User-1828494216 posted

     

    I can insert it anywhere, the array is sorted at a high freq so the naming needs to be very important and it will be placed in the right spot.

    Could you please post me an working example of how you insert new item in the middle of array?

    Yes, you hit it right on. However, being a newbie, I do not know the logic to generate the new filename. I have the sorting taken care of which will place it in the exact spot (provided the name is correct).


    I can't write you the logic and all the code for your project. Even if i wanted to, i can't, becouse i don't know the logic behind... i don't have enough details ;)

    Tuesday, August 31, 2010 11:17 PM
  • User-547121016 posted

    hi

    Here a example

    public string fileUpload(FileUpload fileUpload, string savePath, string file_name)
            {
                //flag variables
                //string savePath;
                string filename = null;
                //application form variables
                //string appsavePath;
                //string appfilename = null; ;

                //savePath = "~/Admin/uploadFiles/flags/";
                //appsavePath = "~/Admin/uploadFiles/appforms/";

                if (fileUpload.PostedFile != null)
                {
                    HttpPostedFile myfile = fileUpload.PostedFile;
                    int filelen = myfile.ContentLength;
                    if (filelen == 0)
                    {
                        filename = "No file was uploaded";
                        return filename;
                    }

                    byte[] myData = new Byte[filelen];
                    myfile.InputStream.Read(myData, 0, filelen);
                    // Make sure a duplicate file doesn’t exist.  If it does, keep on appending an
                    // incremental numeric until it is unique
                    //string sFilename = System.IO.Path.GetFileName(myfile.FileName);
                    string sFilename = file_name;
                    string sFileExtention = System.IO.Path.GetExtension(myfile.FileName);
                    int file_append = 0;

                    while (System.IO.File.Exists(Server.MapPath(savePath + sFilename)))
                    {
                        file_append++;
                        //sFilename = System.IO.Path.GetFileNameWithoutExtension(myfile.FileName)
                        //                 + file_append.ToString() + sFileExtention;
                        sFilename = sFilename + "_" + file_append.ToString();
                    }

                    // Save the stream to disk

                    try
                    {
                        System.IO.FileStream newFile
                                = new System.IO.FileStream(Server.MapPath(savePath + sFilename + sFileExtention),
                                                           System.IO.FileMode.Create);
                        newFile.Write(myData, 0, myData.Length);
                        newFile.Close();

                        filename = savePath + sFilename + sFileExtention;
                    }
                    catch
                    {

                    }
                }
                return filename;
            }

    Tuesday, August 31, 2010 11:30 PM
  • User712038305 posted

    Lasantha: I guess your code assumes I am passing the file_name.

     

    My problem is to generate the file_name for a given set of 2 file names such that the file_name value should be between the two given file names.

    So there should be 2 arguments fname1, fname2 and the function should return fname. The fname should be such that upon sorting, it should look like:

    fname1

    fname

    fname2

     

     

    Tuesday, August 31, 2010 11:52 PM
  • User187056398 posted

    Could you please post me an working example of how you insert new item in the middle of array?

     

    I don't think inserting an item into an array is the problem.  The problem is given two file names, create a third file name based on a set of rules.

    There is always the brute force method using a lot of ifs and elses.

    You may be able to use a tree structure to simulate a directory structure.  It might simplify the code.

    401         (401)
        01       (401_01)
            01   (401_01_01)
            02   (401_01_02)
        02
    402
    403

     

     

    Tuesday, August 31, 2010 11:53 PM
  • User-1828494216 posted

    I don't think inserting an item into an array is the problem.

    True, true. But now i'm curious how to do it so easily :) You can send me pm, so we don't spam this topic.

    Wednesday, September 1, 2010 12:02 AM
  • User712038305 posted

    SGW you got the crux of the problem. I am willing to go the brute force way. Can you show how the if/else's would work? My key problem is to parse the names and compare them and do the math to generate the name.

     

    Thanx 

    Wednesday, September 1, 2010 12:03 AM
  • User-660870441 posted

    Hi,

    As you said the file names are from a sorted array, so I assumed name1 is always smaller than name2. Here is the method made by me (I assumed you are using C#). Please have try on it.

    protected void Page_Load(object sender, EventArgs e)
    {
        // Test the method by all cases
        Response.Write(GenerateFileName("401_01", "402") + "<br />");
        Response.Write(GenerateFileName("401", "403") + "<br />");
        Response.Write(GenerateFileName("401", "402") + "<br />");
        Response.Write(GenerateFileName("401", "402_01") + "<br />");
        Response.Write(GenerateFileName("401", "403_01") + "<br />");
        Response.Write(GenerateFileName("401_01_03", "401_01_04") + "<br />");
        Response.Write(GenerateFileName("401", "401_01") + "<br />");
    }
    public string GenerateFileName(string name1, string name2)
    {
        // turn names into parts
        List<string> list1 = name1.Split('_').ToList();
        string[] list2 = name2.Split('_');
    
        if (list1.Count > list2.Length) // match 401_01 and 402
        {
            list1[list1.Count - 1] = (int.Parse(list1[list1.Count - 1]) + 1).ToString("D2");
        }
        else if (list1.Count <= list2.Length)
        {
            // only need to compare the last number of name1
            int last1 = int.Parse(list1[list1.Count - 1]);
            int last2 = int.Parse(list2[list1.Count - 1]);
    
            if (last1 < last2 - 1) // match 401 and 403
            {
                list1[list1.Count - 1] = (last1 + 1).ToString("D2");
            }
            else if (last1 == last2 && list1.Count < list2.Length) // match 401 and 401_01
            {
                list1.Add("00");
                list1.Add("01");
            }
            else // match 401 and 402
            {
                list1.Add("01");
            }
        }
    
        // generate the file name
        string result = string.Empty;
        for (int i = 0; i < list1.Count; i++)
        {
            result += list1[i] + '_';
        }
        result = result.Substring(0, result.Length - 1);
        return result;
    }
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 3, 2010 5:28 AM