scripting@wanadoo.nl




Unique()

http://www.scriptlogic.com/kixtart/FunctionLibrary_FunctionList.aspx


actual FunctionLibrary list on 'Scriptlogic' site printer-friendly mirror of UDF topic on 'scripting@wanadoo.nl' site close
22 April 2002 Unique()
AuthorW.M. Hinsch (New Mexico Mark)
Action Creates a zero-length unique-named file in the specified directory and returns the fully qualified file spec.
SyntaxUnique ("DirSpec") 
Parameters
  • DirSpec (Optional / String) Valid directory. A trailing backslash is optional. If not supplied or an empty string is supplied, the current directory will be used. If a folder is not specified, the file will be created in the current directory and the function will return directory and file spec of created file or empty string if no file was created. 
  • RemarksThis function supports creating up to 32767 unique files in any directory. 
    ReturnsReturns the complete filespec of the file created. If the directory is invalid or the file cannot be created, the function will return an empty string.
    The function also sets an errorlevel based on either the CreateObject(FSO) function or OpenTextFile method. 
    DependenciesNone. 
    Examples
    'Creating 10 unique-named files in the temp directory for further testing.' ?
    FOR $i = 1 TO 10
      Unique('%temp%')?
    NEXT
    ? 'Creating and using a temp file without worrying about the name.' ?
    $TempFile = Unique('%temp%')
    IF $TempFile <> ''
      'Opening ' + $TempFile ?
      $RC = Open(1,$TempFile,5)
      'Writing: This is a test.' ?
      $RC = WriteLine(1,'This is a test.')
      'Closing ' + $TempFile ?
      $RC = Close(1)
      'Deleting ' + $TempFile ?
      DEL $TempFile
    ELSE
      'Problem with temp file in the %temp% directory.' ?
    ENDIF
    
    ; Console output example
    
    ;Creating 10 unique-named files in the temp directory for further testing.
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX07FE.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX081F.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX0840.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX433D.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX0860.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX6719.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX0881.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX08A2.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX2ED1.TMP
    ;C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX08C2.TMP
    
    ;Creating and using a temp file without worrying about the name.
    ;Opening C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX08E3.TMP
    ;Writing: This is a test.
    ;Closing C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX08E3.TMP
    ;Deleting C:\DOCUME~1\whinsch\LOCALS~1\Temp\~KIX08E3.TMP
    
     
    Source
    FUNCTION Unique (OPTIONAL $sds)
      ; Create a unique-named file in a given (or current) directory. Supports up
      ; to 32767 unique files in one directory.
      ;
      ; Syntax:  Unique(DirectorySpec)
      ; Returns: Directory and file spec of created file or empty string if no
      ;          file was created.
      ; Error level returned is that of the CreateObject() or the OpenTextFile method in FSO.
    
      DIM $ofs, $ofile
      Srnd(@msecs)
      IF ($sds = '')
        $sds=@curdir
      ENDIF
      IF (Right($sds,1) <> '\')
        $sds=$sds+'\'
      ENDIF
      IF GetFileAttr($sds) & 16 ; If sDS is a directory
        DO
          $unique=DecToHex(Rnd())
          $unique=$sds+'~KIX'+Right('0000'+$unique,4)+'.TMP'
        UNTIL NOT Exist($unique)
        $ofs=CreateObject("Scripting.FileSystemObject")
        IF @error
          EXIT @error
        ENDIF
        $ofile=$ofs.opentextfile($unique,2,1,0)
        IF @error
          EXIT @error
        ENDIF
        $ofile.close
        EXIT @error
      ELSE
        EXIT 267
      ENDIF
    ENDFUNCTION ; - Unique -
     
      original source of UDF topic. show actual FunctionLibrary list on Scriptlogic site close top
              printer-friendly mirror of UDF topic on scripting@wanadoo.nl site  




    Copyright © 2003 www.scriptlogic.com & scripting@wanadoo.nl - last updated on 20 May 2003


    Site Meter