|
|
|
| 22 April 2002 | Unique() |
| Author | W.M. Hinsch (New Mexico Mark) |
| Action | Creates a zero-length unique-named file in the specified directory and returns the fully qualified file spec. |
| Syntax | Unique ("DirSpec") |
| Parameters | |
| Remarks | This function supports creating up to 32767 unique files in any directory. |
| Returns | Returns 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. |
| Dependencies | None. |
| 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 -
|
|
|
|