Jump to content

Extract full path from string


Recommended Posts

I've read several threads here regarding identifying/capturing special-purpose file paths or subcomponents, but I'm afraid I still haven't figured out how best to extract a full file path from a string, such that the result either includes existing quoatation marks or adds them if necessary due to embedded spaces. I'm just not getting it. I don't think I'll need to validate the path, with the exception of the quote marks., but someone might. They might be in UNC format. They will all have extensions, so there's no need to worry about names without them. I guess this will need a regular expression?

Thank you for your help.

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...