Poco Script FAQ - Generating unique file names

Poco Script FAQ - Generating unique file names

Q: Is there a way to create random file names for exporting files to a directory. The external program does not care what the file name is, just the extension. The only thing that I can think of is to have a numeric file name and have POCO check to see if it exists. If yes, increment the number. If no, use the number as the file name.
 
A: To my knowledge there isn't a variable that will return you a random file name, however you could use one of the following techniques:
  1. Use the random function to derive a random number and then check to see if the file exists using the FileExists function, looping until you get a new file

    OR

  2. Use a combination of date and time variables (obtained from GetDate and GetTime), manipulating them to remove illegal characters (/ and :) to derive a file name. (Again I'd check to ensure the file didn't exist before using the derived name, if it did I'd append a sequence number to ensure uniqueness).

    OR

  3. If no other process writes to the directory where you will store these files AND you do not delete files from the directory, then you could use the DirList function to return you the number of files in the directory (even specifying a matching pattern if necessary), count the number of lines (using the LineCount) function and then derive a unique name. The following (untested) script commands should accomplish this task: { Initialize the "$dir" variable to the drive and path to which the files are to be saved. Set $dir "C:\my documents\" { Obtain a list of the ".txt" files currently in the directory. DirList $list $dir "*.txt" { Count the number of ".txt" files currently in the directory. LineCount #l $list { Increment the count by 1 Inc #l { Initialize the file name to the drive and path Set $filename $dir { Generate the file name, all files will be sequentially numbered AddStrings $filename #l ".txt"