Search Unity

check for file existence when generating a new File?

Discussion in 'Scripting' started by craig4android, Oct 17, 2019.

?

check if randomly generated File exists already?

  1. yes

    4 vote(s)
    80.0%
  2. no

    1 vote(s)
    20.0%
  1. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    I'm just thinking about this like for 1 hour now:

    Code (CSharp):
    1. public static string getFreeFile(string path)
    2.         {
    3.             string filename = Path.GetRandomFileName();
    4.             string random = path + filename;
    5. //check for existence or just return the random file?
    6.             if (File.Exists(random))
    7.             {
    8.                 return getFreeFile(path);
    9.             }
    10.             else
    11.             {
    12.                 return filename;
    13.             }
    14.         }
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    What's your question here?
     
  3. Elharter

    Elharter

    Joined:
    Sep 20, 2015
    Posts:
    58
    dont understand your question too
     
  4. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    if you receive a random file name there is a very slight chance it might exist already, so if you overwrite this file, chances are, that you destroy a file you didn't want to destory. To prevent this you can check for existence before you use the randomly generated filename. However the probability of getting the same name twice is almost zero. So the question is, if an existence check is worth it?
     
  5. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Sure. I think that function can generate 37^11 different filenames, but the possibility is still there, and I don't think it's an expensive check.
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Why not keep a collection of used named?

    Like start looping ..000 ...001 ...002 until you find a free one?
     
  7. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    good approach when there are only a few files in the folder, but let's assume you have 100 files in the folder you have to loop through all 100 files. I finally used this method:

    Code (CSharp):
    1.         public static string getFreeFolderName(string path, int length=2)
    2.         {
    3.             for (int i = 0; i < Mathf.Pow(5, length); i++)
    4.             {
    5.                 string name = StringTools.getRandomString(length) + "/";
    6.                 string random = path + name;
    7.  
    8.  
    9.                 if (!Directory.Exists(random))
    10.                 {
    11.                     return random;
    12.                 }
    13.             }
    14.             return getFreeFolderName(path, length + 1);
    15.         }
    16.  
    17. public class StringTools
    18. {
    19.     public static string getRandomString(int length)
    20.     {
    21.         var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    22.         var stringChars = new char[length];
    23.  
    24.         for (int i = 0; i < stringChars.Length; i++)
    25.         {
    26.             stringChars[i] = chars[UnityEngine.Random.Range(0, chars.Length - 1)];
    27.         }
    28.  
    29.         var finalString = new String(stringChars);
    30.         return new string(stringChars);
    31.     }
    32. }
    still not perfect but it reduces the amount of Exists calls, and keeps the folder name small, because some systems got limited path sizes.
     
    Last edited: Oct 17, 2019
  8. Well, the work on your code and after cleaning up your string-garbage and whatnot probably will be more than one or two File.Exists calls. And it is really unnecessarily complicated.
     
  9. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    File.Exists is way more expensive than millions of cpu ops, depending on the hard drive.
     
  10. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You can keep your own collection of what's taken, after querying it do a test, if it's a false positive - update the collection and search again..

    what exactly are you saving that requires all this?
     
  11. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    yeah would be better, but even more complex.

    nested dynamic level-maps, which potentially leads to very long path names.
    /level1/level11/level111/....
    /level1/level12
    ...
     
  12. I mean you still call "Directory.Exists(random)" and on the top of that you do a lot of stuff you didn't before and generate garbage. That's why I don't understand why is it better operation?
     
  13. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    it's just to keep the filename smaller than when using the default Path.GetRandomFileName(), probably a waste of coding time, just wanted to make sure, that the path name doesn't exceed any limits
     
  14. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    I think you're wasting your time trying to solve a problem that doesn't exist yet. You've made your code more complicated, and Exists get called more in your new code.