Function:
; #FUNCTION# ==================================================================================================================== ; Name ..........: _GetDirectoryFormat ; Description ...: Formats a directory string with either the trailing '\' removed or included. It can also check whether the directory exists and create if not present. ; Syntax ........: _GetDirectoryFormat(Byref $sDirectory[, $iAppend = 1[, $iFlag = 0]]) ; Parameters ....: $sDirectory - [in/out] A variable containing a directory string with either the trailing '\' included or excluded. ; $fAppend - [optional] True - Append a backslash or False- Remove the appending backslash. Default is True. ; $fFlag - [optional] Check if the directory exists and create if not present. True - Check & create or False - Don't check. Default is False. ; Return values .: Success - Returns directory string with the correct format. ; Failure - None ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GetDirectoryFormat(ByRef $sDirectory, $fAppend = True, $fFlag = False) Local $sAppend = '' If $fAppend Or $fAppend = Default Then $sAppend = '\' EndIf $sDirectory = StringRegExpReplace($sDirectory, '[\\/]+$', '') & $sAppend If FileExists($sDirectory) = 0 And $fFlag Then DirCreate($sDirectory) EndIf Return $sDirectory EndFunc ;==>_GetDirectoryFormat
Example use of Function:
#include <Constants.au3> Local $sExampleDirectory = 'C:\Example_Folder\\\' _GetDirectoryFormat($sExampleDirectory, True) ; Change to 'C:\Example_Folder\' & don't check if directory exists. ConsoleWrite($sExampleDirectory & @CRLF) _GetDirectoryFormat($sExampleDirectory, False) ; Change to 'C:\Example_Folder' & don't check if directory exists. ConsoleWrite($sExampleDirectory & @CRLF) _Example_Old($sExampleDirectory) ; This was the old way of checking whether a directory has a backslash. _Example_New($sExampleDirectory) ; This is the new way of checking whether a directory has a backslash. Func _Example_Old($sDirectory) If Not (StringRight($sDirectory, StringLen('\')) == '\') Then $sDirectory &= '\' Return MsgBox($MB_SYSTEMMODAL, '_Example_Old()', $sDirectory) EndFunc ;==>_Example_Old Func _Example_New($sDirectory) _GetDirectoryFormat($sDirectory, True) Return MsgBox($MB_SYSTEMMODAL, '_Example_New()', $sDirectory) EndFunc ;==>_Example_New
Edited by guinness, 09 April 2013 - 04:18 PM.





