; Include Version:1.66 (17 July 2006)
#include-once

; ------------------------------------------------------------------------------
;
; AutoIt Version: 3.0
; Language:       English
; Description:    Functions that assist with files and directories.
;
; ------------------------------------------------------------------------------


;===============================================================================
;
; Description:      Returns the number of lines in the specified file.
; Syntax:           _FileCountLines( $sFilePath )
; Parameter(s):     $sFilePath - Path and filename of the file to be read
; Requirement(s):   None
; Return Value(s):  On Success - Returns number of lines in the file
;                   On Failure - Returns 0 and sets @error = 1
; Author(s):        Tylo <tylo at start dot no>
; Note(s):          It does not count a final @LF as a line.
;
;===============================================================================
Func _FileCountLines($sFilePath)
	Local $N = FileGetSize($sFilePath) - 1
	If @error Or $N = -1 Then Return 0
	Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1
EndFunc   ;==>_FileCountLines


;===============================================================================
;
; Description:      Creates or zero's out the length of the file specified.
; Syntax:           _FileCreate( $sFilePath )
; Parameter(s):     $sFilePath - Path and filename of the file to be created
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets:
;                                @error = 1: Error opening specified file
;                                @error = 2: File could not be written to
; Author(s):        Brian Keene <brian_keene at yahoo dot com>
; Note(s):          None
;
;===============================================================================
Func _FileCreate($sFilePath)
	;==============================================
	; Local Constant/Variable Declaration Section
	;==============================================
	Local $hOpenFile
	Local $hWriteFile
	
	$hOpenFile = FileOpen($sFilePath, 2)
	
	If $hOpenFile = -1 Then
		SetError(1)
		Return 0
	EndIf
	
	$hWriteFile = FileWrite($hOpenFile, "")
	
	If $hWriteFile = -1 Then
		SetError(2)
		Return 0
	EndIf
	
	FileClose($hOpenFile)
	Return 1
EndFunc   ;==>_FileCreate

;===============================================================================
;
; Description:      lists all files and folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax:           _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
; Parameter(s):    	$sPath = Path to generate filelist for
;                   $iFlag = determines weather to return file or folders or both
;					$sFilter = The filter to use. Search the Autoit3 manual for the word "WildCards" For details
;						$iFlag=0(Default) Return both files and folders
;                       $iFlag=1 Return files Only
;						$iFlag=2 Return Folders Only
;
; Requirement(s):   None
; Return Value(s):  On Success - Returns an array containing the list of files and folders in the specified path
;                        On Failure - Returns the an empty string "" if no files are found and sets @Error on errors
;						@Error=1 Path not found or invalid
;						@Error=2 Invalid $sFilter
;                       @Error=3 Invalid $iFlag
;                 @Error=4 No File(s) Found
;
; Author(s):        SolidSnake <MetalGearX91 at Hotmail dot com>
; Note(s):			The array returned is one-dimensional and is made up as follows:
;					$array[0] = Number of Files\Folders returned
;					$array[1] = 1st File\Folder
;					$array[2] = 2nd File\Folder
;					$array[3] = 3rd File\Folder
;					$array[n] = nth File\Folder
;
;					Special Thanks to Helge and Layer for help with the $iFlag update
;===============================================================================
Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
	Local $hSearch, $sFile, $asFileList[1]
    If Not FileExists($sPath) Then Return SetError(1,1,"")
    If (StringInStr($sFilter, "\")) or (StringInStr($sFilter, "/")) or (StringInStr($sFilter, ":")) or (StringInStr($sFilter, ">")) or (StringInStr($sFilter, "<")) or (StringInStr($sFilter, "|")) or (StringStripWS($sFilter, 8) = "") Then Return SetError(2,2,"")
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3,3,"")
	$hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch = -1 Then Return SetError(4,4,"")
	While 1
		$sFile = FileFindNextFile($hSearch)
        If @error Then 
            SetError(0)
            ExitLoop
		EndIf
        If $iFlag = 1 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
        If $iFlag = 2 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
		ReDim $asFileList[UBound($asFileList) + 1]
		$asFileList[0] = $asFileList[0] + 1
		$asFileList[UBound($asFileList) - 1] = $sFile
	WEnd
	FileClose($hSearch)
	Return $asFileList
EndFunc   ;==>_FileListToArray

;===============================================================================
; Function Name:   _FilePrint()
; Description:     Prints a plain text file.
; Syntax:          _FilePrint ( $s_File [, $i_Show] )
;
; Parameter(s):    $s_File     = The file to print.
;                  $i_Show     = The state of the window. (default = @SW_HIDE)
;
; Requirement(s):  External:   = shell32.dll (it's already in system32).
;                  Internal:   = None.
;
; Return Value(s): On Success: = Returns 1.
;                  On Failure: = Returns 0 and sets @error according to the global constants list.
;
; Author(s):       erifash <erifash [at] gmail [dot] com>
;
; Note(s):         Uses the ShellExecute function of shell32.dll.
;
; Example(s):
;   _FilePrint("C:\file.txt")
;===============================================================================
Func _FilePrint($s_File, $i_Show = @SW_HIDE)
	Local $a_Ret = DllCall("shell32.dll", "long", "ShellExecute", _
			"hwnd", 0, _
			"string", "print", _
			"string", $s_File, _
			"string", "", _
			"string", "", _
			"int", $i_Show)
	If $a_Ret[0] > 32 And Not @error Then
		Return 1
	Else
		SetError($a_Ret[0])
		Return 0
	EndIf
EndFunc   ;==>_FilePrint

;===============================================================================
;
; Description:      Reads the specified file into an array.
; Syntax:           _FileReadToArray( $sFilePath, $aArray )
; Parameter(s):     $sFilePath - Path and filename of the file to be read
;                   $aArray    - The array to store the contents of the file
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error = 1
; Author(s):        Jonathan Bennett <jon at hiddensoft dot com>
; Note(s):          None
;
;===============================================================================
Func _FileReadToArray($sFilePath, ByRef $aArray)
	;==============================================
	; Local Constant/Variable Declaration Section
	;==============================================
	Local $hFile
	
	$hFile = FileOpen($sFilePath, 0)
	
	If $hFile = -1 Then
		SetError(1)
		Return 0
	EndIf
	
	$aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF)
	
	FileClose($hFile)
	Return 1
EndFunc   ;==>_FileReadToArray

;===============================================================================
;
; Description:      Write array to File.
; Syntax:           _FileWriteFromArray( $sFilePath, $aArray )
; Parameter(s):     $sFilePath - Path and filename of the file to be written
;                   $a_Array   - The array to retrieve the contents
;                   $i_Base    - Start reading at this Array entry.
;                   $I_Ubound  - End reading at this Array entry.
;                                Default UBound($a_Array) - 1
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error = 1
; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
; Note(s):          None
;
;===============================================================================
Func _FileWriteFromArray($sFilePath, $a_Array, $i_Base = 0, $i_UBound = 0)
	;==============================================
	; Local Constant/Variable Declaration Section
	;==============================================
	Local $hFile
	; Check if we have a valid array as input
	If Not IsArray($a_Array) Then
		SetError(2)
		Return 0
	EndIf
	; determine last entry
	Local $last = UBound($a_Array) - 1
	If $i_UBound < 1 Or $i_UBound > $last Then $i_UBound = $last
	If $i_Base < 0 Or $i_Base > $last Then $i_Base = 0
	; Open output file
	$hFile = FileOpen($sFilePath, 2)
	
	If $hFile = -1 Then
		SetError(1)
		Return 0
	EndIf
	;
	FileWrite($hFile, $a_Array[$i_Base])
	For $x = $i_Base + 1 To $i_UBound
		FileWrite($hFile, @CRLF & $a_Array[$x])
	Next
	
	FileClose($hFile)
	Return 1
EndFunc   ;==>_FileWriteFromArray

;===============================================================================
;
; Description:      Writes the specified text to a log file.
; Syntax:           _FileWriteLog( $sLogPath, $sLogMsg )
; Parameter(s):     $sLogPath - Path and filename to the log file
;                   $sLogMsg  - Message to be written to the log file
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets:
;                                @error = 1: Error opening specified file
;                                @error = 2: File could not be written to
; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
; Note(s):          If the text to be appended does NOT end in @CR or @LF then
;                   a DOS linefeed (@CRLF) will be automatically added.
;
;===============================================================================
Func _FileWriteLog($sLogPath, $sLogMsg)
	;==============================================
	; Local Constant/Variable Declaration Section
	;==============================================
	Local $sDateNow
	Local $sTimeNow
	Local $sMsg
	Local $hOpenFile
	Local $hWriteFile
	
	$sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
	$sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
	$sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg
	
	$hOpenFile = FileOpen($sLogPath, 1)
	
	If $hOpenFile = -1 Then
		SetError(1)
		Return 0
	EndIf
	
	$hWriteFile = FileWriteLine($hOpenFile, $sMsg)
	
	If $hWriteFile = -1 Then
		SetError(2)
		Return 0
	EndIf
	
	FileClose($hOpenFile)
	Return 1
EndFunc   ;==>_FileWriteLog

;========================================
;Function name:       _FileWriteToLine
;Description:         Write text to specified line in a file
;Parameters:
;                     $sFile - The file to write to
;                     $iLine - The line number to write to
;                     $sText - The text to write
;                     $fOverWrite - if set to 1 will overwrite the old line
;                     if set to 0 will not overwrite
;Requirement(s):      None
;Return Value(s):     On success - 1
;                      On Failure - 0 And sets @ERROR
;                                @ERROR = 1 - File has less lines than $iLine
;                                @ERROR = 2 - File does not exist
;                                @ERROR = 3 - Error opening file
;                                @ERROR = 4 - $iLine is invalid
;                                @ERROR = 5 - $fOverWrite is invalid
;                                @ERROR = 6 - $sText is invalid
;Author(s):           cdkid
;Note(s):
;=========================================
Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0)
	If $iLine <= 0 Then
		SetError(4)
		Return 0
	EndIf
	If Not IsString($sText) Then
		SetError(6)
		Return 0
	EndIf
	If $fOverWrite <> 0 And $fOverWrite <> 1 Then
		SetError(5)
		Return 0
	EndIf
	If Not FileExists($sFile) Then
		SetError(2)
		Return 0
	EndIf
	Local $filtxt = FileRead($sFile, FileGetSize($sFile))
	$filtxt = StringSplit($filtxt, @CRLF, 1)
	If UBound($filtxt, 1) < $iLine Then
		SetError(1)
		Return 0
	EndIf
	Local $fil = FileOpen($sFile, 2)
	If $fil = -1 Then
		SetError(3)
		Return 0
	EndIf
	For $i = 1 To UBound($filtxt) - 1
		If $i = $iLine Then
			If $fOverWrite = 1 Then
				If $sText <> '' Then
					FileWrite($fil, $sText & @CRLF)
				Else
					FileWrite($fil, $sText)
				EndIf
			EndIf
			If $fOverWrite = 0 Then
				FileWrite($fil, $sText & @CRLF)
				FileWrite($fil, $filtxt[$i] & @CRLF)
			EndIf
		ElseIf $i < UBound($filtxt, 1) - 1 Then
			FileWrite($fil, $filtxt[$i] & @CRLF)
		ElseIf $i = UBound($filtxt, 1) - 1 Then
			FileWrite($fil, $filtxt[$i])
		EndIf
	Next
	FileClose($fil)
	Return 1
EndFunc   ;==>_FileWriteToLine

; ===================================================================
; Author: Valik
	;		updated 2006/01/07
	;
	; _PathFull($szRelPath)
	;
	; Creates a path based on the relative path you provide.  For example, if you specify:
	;	"C:\test\folder\..\file.txt", the output would be "C:\test\file.txt" because of the ..\.
	;	If no relative path is specified, the current working directory is returned.  If no drive is specified,
	;	the new path is relative to the current working directory.  Passing only a drive will return the
	;	current working directory for that drive.  This function depends on _PathSplit and _PathMake.
	; Parameters:
	; 	$szRelPath - IN - The relative path
	; Returns:
	;	The newly created absolute path is returned (Same as $szABSPath)
	; ===================================================================
Func _PathFull($szRelPath)
	Local $drive, $dir, $file, $ext, $wDrive, $NULL
	Local $i, $nPos; For Opt("MustDeclareVars", 1)
	; Let's use _PathSplit to make this a hell of a lot easier to work with
	_PathSplit($szRelPath, $drive, $dir, $file, $ext)
	_PathSplit(@WorkingDir, $wDrive, $NULL, $NULL, $NULL)
	
	; If no drive specified, then we use the working directory to set our path relative to
	If Not StringLen($drive) Then
		$drive = $wDrive
		If StringLen(@WorkingDir) = StringLen($drive) + 1 Then
			$dir = StringTrimLeft(@WorkingDir, StringLen($drive)) & $dir
		Else
			$dir = StringTrimLeft(@WorkingDir, StringLen($drive)) & "\" & $dir
		EndIf
	EndIf
	
	; If the only thing specified was the drive, then return the working directory for that drive
	If Not StringLen($dir) And Not StringLen($file) And Not StringLen($ext) Then
		If $drive = $wDrive Then Return @WorkingDir
		Return _PathMake($drive, "\", "", "")
	EndIf
	
	; Look for any .\ and ..\
	While StringInStr($dir, ".\") Or StringInStr($dir, "./")
		$nPos = StringInStr($dir, ".\")
		If $nPos = 0 Then $nPos = StringInStr($dir, "./")
		If $nPos = 0 Then ExitLoop
		If StringMid($dir, $nPos - 1, 1) = "." Then; It's ..\
			For $i = ($nPos - 3) To 0 Step - 1
				If StringMid($dir, $i, 1) = "\" Or StringMid($dir, $i, 1) = "/" Then ExitLoop
			Next
			If $i > 0 Then
				$dir = StringLeft($dir, $i) & StringRight($dir, StringLen($dir) - ($nPos + 1))
			Else
				$dir = StringRight($dir, StringLen($dir) - ($nPos + 1))
			EndIf
		Else; It's .\
			$dir = StringLeft($dir, $nPos - 1) & StringRight($dir, StringLen($dir) - $nPos - 1)
		EndIf
		If Not StringLen($dir) Then $dir = "\"
	WEnd
	Return _PathMake($drive, $dir, $file, $ext)
EndFunc   ;==>_PathFull

; ===================================================================
; Author: Valik
;
; _PathMake($szDrive, $szDir, $szFName, $szExt)
;
; Creates a string containing the path from drive, directory, file name and file extension parts.  Not all parts must be
;	passed. The path will still be built with what is passed.  This doesn't check the validity of the path
;	created, it could contain characters which are invalid on your filesystem.
; Parameters:
; 	$szDrive - IN - Drive (Can be UNC).  If it's a drive letter, a : is automatically appended
; 	$szDir - IN - Directory.  A trailing slash is added if not found (No preceeding slash is added)
; 	$szFName - IN - The name of the file
; 	$szExt - IN - The file extension.  A period is supplied if not found in the extension
; Returns:
;	The string for the newly created path
; ===================================================================
Func _PathMake($szDrive, $szDir, $szFName, $szExt)
	; Format $szDrive, if it's not a UNC server name, then just get the drive letter and add a colon
	Local $szFullPath
	;
	If StringLen($szDrive) Then
		If Not (StringLeft($szDrive, 2) = "\\") Then	$szDrive = StringLeft($szDrive, 1) & ":"
	EndIf
	
	; Format the directory by adding any necessary slashes
	If StringLen($szDir) Then
		If Not (StringRight($szDir, 1) = "\") And Not (StringRight($szDir, 1) = "/") Then $szDir = $szDir & "\"
	EndIf
	
	; Nothing to be done for the filename
	
	; Add the period to the extension if necessary
	If StringLen($szExt) Then
		If Not (StringLeft($szExt, 1) = ".") Then $szExt = "." & $szExt
	EndIf
	
	$szFullPath = $szDrive & $szDir & $szFName & $szExt
	Return $szFullPath
EndFunc   ;==>_PathMake

; ===================================================================
; Author: Valik
;
; _PathSplit($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)
;
; Splits a path into the drive, directory, file name and file extension parts.  An empty string is set if a
;	part is missing.
; Parameters:
;	$szPath - IN - The path to be split (Can contain a UNC server or drive letter)
;	$szDrive - OUT - String to hold the drive
; 	$szDir - OUT - String to hold the directory
; 	$szFName - OUT - String to hold the file name
; 	$szExt - OUT - String to hold the file extension
; Returns:
;	Array with 5 elements where 0 = original path, 1 = drive, 2 = directory, 3 = filename, 4 = extension
; ===================================================================
Func _PathSplit($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)
	; Set local strings to null (We use local strings in case one of the arguments is the same variable)
	Local $drive = ""
	Local $dir = ""
	Local $fname = ""
	Local $ext = ""
	Local $i, $pos; For Opt("MustDeclareVars", 1)
	
	; Create an array which will be filled and returned later
	Local $array[5]
	$array[0] = $szPath; $szPath can get destroyed, so it needs set now
	
	; Get drive letter if present (Can be a UNC server)
	If StringMid($szPath, 2, 1) = ":" Then
		$drive = StringLeft($szPath, 2)
		$szPath = StringTrimLeft($szPath, 2)
	ElseIf StringLeft($szPath, 2) = "\\" Then
		$szPath = StringTrimLeft($szPath, 2) ; Trim the \\
		$pos = StringInStr($szPath, "\")
		If $pos = 0 Then $pos = StringInStr($szPath, "/")
		If $pos = 0 Then
			$drive = "\\" & $szPath; Prepend the \\ we stripped earlier
			$szPath = ""; Set to null because the whole path was just the UNC server name
		Else
			$drive = "\\" & StringLeft($szPath, $pos - 1) ; Prepend the \\ we stripped earlier
			$szPath = StringTrimLeft($szPath, $pos - 1)
		EndIf
	EndIf
	
	; Set the directory and file name if present
	For $i = StringLen($szPath) To 0 Step - 1
		If StringMid($szPath, $i, 1) = "\" Or StringMid($szPath, $i, 1) = "/" Then
			$dir = StringLeft($szPath, $i)
			$fname = StringRight($szPath, StringLen($szPath) - $i)
			ExitLoop
		EndIf
	Next
	
	; If $szDir wasn't set, then the whole path must just be a file, so set the filename
	If StringLen($dir) = 0 Then $fname = $szPath
	
	; Check the filename for an extension and set it
	For $i = StringLen($fname) To 0 Step - 1
		If StringMid($fname, $i, 1) = "." Then
			$ext = StringRight($fname, StringLen($fname) - ($i - 1))
			$fname = StringLeft($fname, $i - 1)
			ExitLoop
		EndIf
	Next
	
	; Set the strings and array to what we found
	$szDrive = $drive
	$szDir = $dir
	$szFName = $fname
	$szExt = $ext
	$array[1] = $drive
	$array[2] = $dir
	$array[3] = $fname
	$array[4] = $ext
	Return $array
EndFunc   ;==>_PathSplit

; ===================================================================
; Author: Kurt (aka /dev/null) and JdeB
;
; _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString,$bCaseness = 0, $bOccurance = 0)
;
; Replaces a string ($szSearchString) with another string ($szReplaceString) the given TEXT file
; (via filename)
;
; Operation:
; The funnction opens the original file for reading and a temp file for writing. Then
; it reads in all lines and searches for the string. If it was found, the original line will be
; modified and written to the temp file. If the string was not found, the original line will be
; written to the temp file. At the end the original file will be deleted and the temp file will
; be renamed.
;
; Parameters:
; 	$szFileName 		name of the file to open.
;				ATTENTION !! Needs the FULL path, not just the name returned by eg. FileFindNextFile
; 	$szSearchString		The string we want to replace in the file
; 	$szReplaceString	The string we want as a replacement for $szSearchString
; 	$fCaseness		shall case matter?
;				0 = NO, case doe not matter (default), 1 = YES, case does matter
;	$fOccurance		shall we replace the string in every line or just the first occurance
;				0 = first only, 1 = ALL strings (default)
;
; Return Value(s):
;	On Success 		Returns the number of occurences of $szSearchString we found
;
;	On Failure 		Returns -1 sets @error
;					@error=1	Cannot open file
;					@error=2	Cannot open temp file
;					@error=3	Cannot write to temp file
;					@error=4	Cannot delete original file
;					@error=5	Cannot rename/move temp file
;
; ===================================================================
Func _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString, $fCaseness = 0, $fOccurance = 1)
	
	Local $iRetVal = 0
	Local $szTempFile, $hWriteHandle, $aFileLines, $nCount, $sEndsWith, $hFile
	;===============================================================================
	;== Read the file into an array
	;===============================================================================
	$hFile = FileOpen($szFileName, 0)
	If $hFile = -1 Then
		SetError(1)
		Return -1
	EndIf
	Local $s_TotFile = FileRead($hFile, FileGetSize($szFileName))
	If StringRight($s_TotFile, 2) = @CRLF Then
		$sEndsWith = @CRLF
	ElseIf StringRight($s_TotFile, 1) = @CR Then
		$sEndsWith = @CR
	ElseIf StringRight($s_TotFile, 1) = @LF Then
		$sEndsWith = @LF
	Else
		$sEndsWith = ""
	EndIf
	$aFileLines = StringSplit(StringStripCR($s_TotFile), @LF)
	FileClose($hFile)
	;===============================================================================
	;== Open the temporary file in write mode
	;===============================================================================
	$szTempFile = _TempFile()
	
	$hWriteHandle = FileOpen($szTempFile, 2)
	If $hWriteHandle = -1 Then
		SetError(2)
		Return -1
	EndIf
	;===============================================================================
	;== Loop through the array and search for $szSearchString
	;===============================================================================
	For $nCount = 1 To $aFileLines[0]
		If StringInStr($aFileLines[$nCount], $szSearchString, $fCaseness) Then
			$aFileLines[$nCount] = StringReplace($aFileLines[$nCount], $szSearchString, $szReplaceString, 1 - $fOccurance, $fCaseness)
			$iRetVal = $iRetVal + 1
			
			;======================================================================
			;== If we want just the first string replaced, copy the rest of the lines
			;== and stop
			;======================================================================
			If $fOccurance = 0 Then
				$iRetVal = 1
				ExitLoop
			EndIf
		EndIf
	Next
	;===============================================================================
	;== Write the lines to a temp file.
	;===============================================================================
	For $nCount = 1 To $aFileLines[0] - 1
		If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then
			SetError(3)
			FileClose($hWriteHandle)
			Return -1
		EndIf
	Next
	; Write the last record and ensure it ends wit hthe same as the input file
	If $aFileLines[$nCount] <> "" Then FileWrite($hWriteHandle, $aFileLines[$nCount] & $sEndsWith)
	FileClose($hWriteHandle)
	
	;======================================================================
	;== Delete the original file
	;======================================================================
	If FileDelete($szFileName) = 0 Then
		SetError(4)
		Return -1
	EndIf
	
	;======================================================================
	;== Rename the temp file to the original file
	;======================================================================
	If FileMove($szTempFile, $szFileName) = 0 Then
		SetError(5)
		Return -1
	EndIf
	
	Return $iRetVal
EndFunc   ;==>_ReplaceStringInFile

;========================================================================================================
;
; Function Name:    _TempFile([s_DirectoryName],[s_FilePrefix], [s_FileExtension], [i_RandomLength)
; Description:      Generate a name for a temporary file. The file is guaranteed not to already exist.
; Parameter(s):
;     $s_DirectoryName    optional  Name of directory for filename, defaults to @TempDir
;     $s_FilePrefix       optional  File prefixname, defaults to "~"
;     $s_FileExtension    optional  File extenstion, defaults to ".tmp"
;     $i_RandomLength     optional  Number of characters to use to generate a unique name, defaults to 7
; Requirement(s):   None.
; Return Value(s):  Filename of a temporary file which does not exist.
; Author(s):        Dale (Klaatu) Thompson
;                   Hans Harder - Added Optional parameters
; Notes:            None.
;
;========================================================================================================
Func _TempFile($s_DirectoryName = @TempDir, $s_FilePrefix = "~", $s_FileExtension = ".tmp", $i_RandomLength = 7)
	Local $s_TempName
	; Check parameters
	If Not FileExists($s_DirectoryName) Then $s_DirectoryName = @TempDir   ; First reset to default temp dir
	If Not FileExists($s_DirectoryName) Then $s_DirectoryName = @ScriptDir ; Still wrong then set to Scriptdir
	; add trailing \ for directory name
	If StringRight($s_DirectoryName, 1) <> "\" Then $s_DirectoryName = $s_DirectoryName & "\"
	;
	Do
		$s_TempName = ""
		While StringLen($s_TempName) < $i_RandomLength
			$s_TempName = $s_TempName & Chr(Random(97, 122, 1))
		WEnd
		$s_TempName = $s_DirectoryName & $s_FilePrefix & $s_TempName & $s_FileExtension
	Until Not FileExists($s_TempName)
	
	Return ($s_TempName)
EndFunc   ;==>_TempFile

