0

I'm trying to generate unique names. I know about UUID and I have generated my own plugin to generate unique IDs. But this question applies to multiple cases.

How do I create a WHILE that checks if a generated file name exists in a folder. if it exists, we generate a new file name.

Let's say we have a generator that generates: IOX and checks that folder (or mysql database) and the file IOX.jpg already exists. so we generate another file name. this time we get: PQE... and so on, until we find a free entry in the database or in the filesystem.

Alex Laz
  • 339
  • 2
  • 6
  • When you have a huge amount of files, this is not a fast and good approach. Try to to utilize a timestamp + random thing, a hash of the file or a static incrementing counter somewhere. Imagine when you make an appointment, do you say "do you have time at 2:30? No? 2:31 maybe? No? 2:32?" you don't do this. You ask "When do you have time to meet?". – Daniel W. Mar 27 '17 at 13:06
  • Yes. You use UUID for that. I just mentioned that. But strictly from a coding point of view, how would i achieve what I asked? – Alex Laz Mar 27 '17 at 16:42
  • Use some sort of [`fs.fileExists()`](http://stackoverflow.com/a/4482701/1948292) and if it does, generate a new random ABC string. – Daniel W. Mar 28 '17 at 08:23
  • Yes, tried to use async.until ... that doesn't work. Or I don't know how to use it. Because that's how i thought of checking it, but the check function for async.until doesn't wait for the async function to finish so i can return true or false so it doesn't reiterate – Alex Laz Mar 29 '17 at 09:02
  • Don't use async then. You want to check for one file after the other, not check xx filenames in parallel. – Daniel W. Mar 29 '17 at 11:26

0 Answers0