Here is a UDF I wrote for work purposes which may help:
#include-once
#include <Array.au3>
#include <File.au3>
Global $g_sScitePath = RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Scite.exe", "")
If @error Then $g_sScitePath = _FullPath(@AutoItExe, True, True, False) & "Scite\Scite.exe"
ConsoleWrite(";~ Example 1 : Return Scite file path, with quotes if folder path has any whitespace." & @CRLF)
ConsoleWrite(";~ Result 1 : " & _FullPath("'" & $g_sScitePath & "'", Default, False, False) & @CRLF & @CRLF)
ConsoleWrite(";~ Example 2 : Return Scite folder path down one PARENT level, with trailing backslash and without quotes." & @CRLF)
ConsoleWrite(";~ Result 2 : " & _FullPath("'" & $g_sScitePath & "'", False, True, True, -1) & @CRLF & @CRLF)
ConsoleWrite(";~ Example 3 : Return Scite folder path up one ROOT level, with trailing backslash, enclosed in quotes" & _
";~ (uses single quote detemined by $_sFullPath quotes)." & @CRLF)
ConsoleWrite(";~ Result 3 : " & _FullPath("'" & $g_sScitePath & "'", True, True, True, 1) & @CRLF & @CRLF)
ConsoleWrite(";~ Example 4 : Return 3rd level ROOT folder Path with trailing backslash, not enclosed in quotes" & @CRLF)
ConsoleWrite(";~ Result 4 : " & _FullPath("C:\Users\Michael O'Brien\AppData\Roaming\Documents\Filename.doc", False, True, True, 3) & @CRLF)
ConsoleWrite(";~ Full Path : " & _FullPath("'C:\Users\Michael O'Brien\AppData\Roaming\Documents\Filename.doc'", False, True, False) & @CRLF & @CRLF)
ConsoleWrite(";~ Example 5 : Return 3rd level PARENT folder without trailing backslash, enclosed in quotes" & _
";~ (enclosed in double quotes, since folder includes single quote)." & @CRLF)
ConsoleWrite(";~ Result 5 : " & _FullPath("'C:\Users\Michael O'Brien\AppData\Roaming\Documents\Filename.doc'", True, True, False, -3) & @CRLF)
ConsoleWrite(";~ Full Path : " & _FullPath("'C:\Users\Michael O'Brien\AppData\Roaming\Documents\Filename.doc'", True, True, False) & @CRLF & @CRLF)
; #FUNCTION# ====================================================================================================================
; Name ..........: _FullPath
; Description ...: Get full or partial folder/file path with/without quotes, trailing backslash. Can also return different
; : folder levels either from root or parent (folders only).
; Syntax ........: _FullPath($_sFullPath[, $_bQuotes = False[, $_bDirPath = False[, $_bPathAddBackslash = False[,
; $_iDirLevel = Default]]]])
; Parameters ....: $_sFullPath - Folder or file path
; $_bQuotes - [optional] Return Path with or without quotes. Default is False.
; - If set and $_sFullPath begins and ends with single quotes, the path will be returned with single
; - quotes (unless any folders include any single quotes), otherwise return path with double quotes.
; $_bDirPath - [optional] Return folder path. Default is False.
; $_bPathAddBackslash - [optional] Add trailing backslash. Default is False (ignored if $_bDirPath = False).
; $_iDirLevel - [optional] Return Folder Level. Default is Default (ignored if $_bDirPath = False).
; - Default = Full Folder Path
; - -1 = Return Number of Folder Levels from Parent (e.g. C:\...\Level -2\Level -1\Filename.exe
; - 0 = Root Path (Drive or UNC Server name)
; - 1+ = Return Number of Folder Levels from Root (e.g. C:\Level 1\Level 2\...\Filename.exe
; - Will return Full Folder Path if level is equal or greater than $_iDirLevel
; Return values .: Full Folder/File Path with or without trailing backslash and with or without quotes
; Author ........: Subz
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _FullPath($_sFullPath, $_bQuotes = Default, $_bDirPath = False, $_bPathAddBackslash = False, $_iDirLevel = Default)
Local $sDrive = "", $sDirPath = "", $sFileName = "", $sExtension = "", $sQuote = ""
If $_bQuotes = True Then
If StringLeft($_sFullPath, 1) = "'" And StringRight($_sFullPath, 1) = "'" Then
$sQuote = "'"
Else
$sQuote = '"'
EndIf
EndIf
;~ Configure $sPathAddBackSlash variable based upon $_bPathAddBackslash
Local $sPathAddBackSlash = $_bPathAddBackslash ? "\" : ""
;~ Replace any double quotes in the path
Local $sFullPath = StringReplace($_sFullPath, '"', "")
;~ Remove single quotes only if first character equals a single quote (Files/Folders names can include single quotes)
If StringLeft($sFullPath, 1) = "'" Then
$sFullPath = StringTrimLeft($sFullPath, 1)
If StringRight($sFullPath, 1) = "'" Then $sFullPath = StringTrimRight($sFullPath, 1)
EndIf
;~ Remove trailing backslash
If StringRight($sFullPath, 1) = "\" Then $sFullPath = StringTrimRight($sFullPath, 1)
;~ If $_bQuotes = True and full path includes single quote(s) use double quotes
If $_bQuotes And StringInStr($sFullPath, "'") Then $sQuote = '"'
;~ If $_bQuotes = Default and full path includes space(s) use double quotes
If $_bQuotes = Default And $sQuote = "" And StringRegExp($sFullPath, "\s", 0) > 0 Then $sQuote = '"'
;~ Return Full Path
If $_bDirPath = False Then Return SetError(0, 0, $sQuote & $sFullPath & $sQuote)
Local $aFilePath = _PathSplit($sFullPath, $sDrive, $sDirPath, $sFileName, $sExtension)
If $sDirPath = "\" And $sFileName <> "" And $sExtension = "" Then $sDirPath = "\" & $sFileName & "\"
If StringLeft($sDirPath, 1) = "\" Then $sDirPath = StringTrimLeft($sDirPath, 1)
If StringRight($sDirPath, 1) = "\" Then $sDirPath = StringTrimRight($sDirPath, 1)
;~ If $_iDirLevel equals 1 Return Drive + Folder Path
If $_iDirLevel = Default Then Return SetError(0, 0, $sQuote & $sDrive & "\" & $sDirPath & $sPathAddBackSlash & $sQuote)
;~ If $_iDirLevel equals 0 Return Drive
If $_iDirLevel = 0 Then Return SetError(0, 0, $sQuote & $sDrive & $sPathAddBackSlash & $sQuote)
;~ Split Folder Path
Local $aDirLevel = StringSplit($sDirPath, "\")
;~ If $_iDirLevel is equal or greater than the number of folders Return Drive + Folder Path
If Abs($_iDirLevel) >= $aDirLevel[0] Then Return SetError(0, 0, $sQuote & $sDrive & "\" & $sDirPath & $sPathAddBackSlash & $sQuote)
;~ If $_iDirLevel is negative number remove paths from end to beginning (instead of beginning to end)
If $_iDirLevel < 0 Then $_iDirLevel = $aDirLevel[0] + $_iDirLevel
;~ Return Drive + Folder $_iDirLevel
Return SetError(0, 0, $sQuote & $sDrive & "\" & _ArrayToString($aDirLevel, "\", 1, $_iDirLevel) & $sPathAddBackSlash & $sQuote)
EndFunc