Jump to content

Spliting file path into drive letter, directory path and filename


Recommended Posts

Hi!

I needed to split filepath into path and filename, so I wrote a function:

; Split path, returns: [1] = drive letter, [2] = diretory path, [3] = filename
Func SplitPath($path)
    Local $SplitedPath, $Return[4], $ThereIsFilename, $ThereIsNoBackslashAtBeginning, $print
    $Return[1] = ""
    $Return[3] = ""
    $ThereIsFilename = false
    $ThereIsNoBackslashAtBeginning = true
    ; get drive letter
    If Not StringInStr($path, ":") = 0 Then 
        $Return = StringSplit($path, ":")
    Else
        $Return[2] = $path
    EndIf
    ; split the path
    $SplitedPath = StringSplit($Return[2], "\")
    ; get filename, if there is one
    If StringInStr(FileGetAttrib($path), "D") = 0 Then 
        $Return[3] = $SplitedPath[UBound($SplitedPath)-1]
        $ThereIsFilename = True
    EndIf
    ; get directory path
    $Return[2] = ""
    If Not $SplitedPath[1] = "" Then $ThereIsNoBackslashAtBeginning = false
    For $i = (1+$ThereIsNoBackslashAtBeginning) to UBound($SplitedPath) - (1 + $ThereIsFilename) 
            ; We sometimes need to start at [2] because [1]=="" after split since there is no character before first "\"
        $Return[2] = $Return[2] & "\" & $SplitedPath[$i]
    Next
#cs 
; Split path, returns: [1] = drive letter, [2] = diretory path, [3] = filename
Func SplitPath($path)
    Local $SplitedPath, $Return[4], $ThereIsFilename, $ThereIsNoBackslashAtBeginning, $print
    $Return[1] = ""
    $Return[3] = ""
    $ThereIsFilename = false
    $ThereIsNoBackslashAtBeginning = true
    ; get drive letter
    If Not StringInStr($path, ":") = 0 Then 
        $Return = StringSplit($path, ":")
    Else
        $Return[2] = $path
    EndIf
    ; split the path
    $SplitedPath = StringSplit($Return[2], "\")
    ; get filename, if there is one
    If StringInStr(FileGetAttrib($path), "D") = 0 Then 
        $Return[3] = $SplitedPath[UBound($SplitedPath)-1]
        $ThereIsFilename = True
    EndIf
    ; get directory path
    $Return[2] = ""
    If Not $SplitedPath[1] = "" Then $ThereIsNoBackslashAtBeginning = false
    For $i = (1+$ThereIsNoBackslashAtBeginning) to UBound($SplitedPath) - (1 + $ThereIsFilename) 
            ; We sometimes need to start at [2] because [1]=="" after split since there is no character before first "\"
        $Return[2] = $Return[2] & "\" & $SplitedPath[$i]
    Next
#cs
    $print = ""
    For $i = 0 to UBound($Return) -1
        $print = $print & @LF & $Return[$i]
    Next
    MsgBox(0, "Debugging info", $path & @LF & $print)
#ce 
    Return $Return      
EndFunc$print = ""
    For $i = 0 to UBound($Return) -1
        $print = $print & @lf & $Return[$i]
    Next
    MsgBox(0, "", $path&@lf&$print)
#ce 
    Return $Return      
EndFunc

I've just started scripting with AutoIt and all comments are welcome, especially those which would simplify my code..

Cheers,

Bostjan

Link to comment
Share on other sites

I didn't look at the details of your code, but I think _PathSplit covers what you are trying to do. Details are in the help file.

Thanks!

I relied on http://www.autoitscript.com/autoit3/docs/ but there are no _Functions listed.... :-(

My code works, but I'm always looking how to simplify it, so, BIG thanks for the _PathSplit!

Anyway, I was wondering if there is a way to tell what device is used to map a share when DriveMapAdd ( "*", "\\someserver\someshare") is used?

It could be useful to do some stealth backing up data using any free device and not bothering user if the device of choice is already used...

Link to comment
Share on other sites

Thanks!

I relied on http://www.autoitscript.com/autoit3/docs/ but there are no _Functions listed.... :-(

Function Reference<br><br>_PathSplit is a UDF not covered by online docs, but is detailed in the local AutoIt help file<br>

Edited by snowmaker

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

My code works, but I'm always looking how to simplify it, so, BIG thanks for the _PathSplit!

My code works, but _PathSplit does not.:-( It splits allright, but it doesn't check if the last file in path is a file or directory! So instead of empty string for the filename I get last directory name and I'm that directory short in path....

I'll try to code a workaround which should be simple enough, but stil...

Edited by BoskoBuha
Link to comment
Share on other sites

I'll try to code a workaround which should be simple enough, but stil...

Here we go:

_PathSplit($source, $SourceDrive, $SourceDir, $SourceFileName, $SourceFileExtension)

; restore directory path if $SourceFileName is a directory!
If Not StringInStr(FileGetAttrib($source), "D") = 0 Then 
    $SourceDir = $SourceDir&$SourceFileName&"\"
    $SourceFileName = ""
EndIf

Cheers,

Bostjan

p.s.:

Why can't I edit my previous posts?

Link to comment
Share on other sites

I also checked _PathSplit source and found a bug (or maybe a feature;-)):

; Set the directory and file name if present
    Local $nPosForward = StringInStr($szPath, "/", 0, -1)
    Local $nPosBackward = StringInStr($szPath, "\", 0, -1)
    If $nPosForward >= $nPosBackward Then
        $pos = $nPosForward
    Else
        $pos = $nPosBackward
    EndIf
    $dir = StringLeft($szPath, $pos)
    $fname = StringRight($szPath, StringLen($szPath) - $pos)

Function assumes a directory path ends with \. Well, what if it doesn't?!?

In my program I transform all paths in absolute paths:

$source = _PathFull($source)
, but _PathFull does not add "\" at the end.

I guess I could modify the file.au3 library:

; Set the directory and file name if present
    Local $nPosForward = StringInStr($szPath, "/", 0, -1)
    Local $nPosBackward = StringInStr($szPath, "\", 0, -1)
    If $nPosForward >= $nPosBackward Then
        $pos = $nPosForward
    Else
        $pos = $nPosBackward
    EndIf
    $dir = StringLeft($szPath, $pos)
    $fname = StringRight($szPath, StringLen($szPath) - $pos)
    
    ; But, what if it is a directory ending without "\"?!?
    If (Not StringInStr(FileGetAttrib($szPath), "D") = 0) And Not (StringLen($szPath) = $pos) Then
        $dir = $dir & $fname & "\"
        $fname = ""
    EndIf

Bostjan

Edited by BoskoBuha
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...