Sign in to follow this  
Followers 0
ken82m

Stringinstr - Multiple Instances

4 posts in this topic

Here's my problem first a user browses for a file using FileOpenDialog. I will be using that in my script somewhere but I also want to use the path to the file somwehere else. So I want to create a new string with the filename cut off the end. so I could search for the backslash but there are going to be more than one and I need the last one. One thing that may help is in this case I know the extension will always be .CAZ

any Ideas?

Thanks,

Kenny


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

stringsplit might work for ya, ex:

$x="C:\windows\bob\fred gonzalles\joe.txt"
$file=StringSplit($x,"\"); split by slashes
; info $file[0] is the amount of items besides itself
MsgBox(1,"file is", $file[$file[0]]); last item

edit oops, you wanted everything but that:

$x="C:\windows\bob\fred gonzalles\joe.txt"
$file=StringSplit($x,"\"); split by slashes
$path=""
For $i=1 To $file[0]-1
    $path=$path&$file[$i]&"\"
Next
MsgBox(1,"file is "& $file[$file[0]], "Path is "& $path)
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Use this function to split a path into it's 4 key components (Watch wordwrap):

; ===================================================================
; _SplitPath($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 _SplitPath($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  ; For Opt("MustDeclareVars", 1)
    
  ; Create an array which will be filled and returned later
    Dim $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  ; _SplitPath()
; ===================================================================

Edit: Replaced tabs with spaces so indentation looks right and added below example:

$path = "C:\Program Files\AutoIt3\AutoIt3.exe"
Local $drive, $dir, $fName, $ext
_SplitPath($path, $drive, $dir, $fName, $ext)
MsgBox(4096, "", "Drive=" & $drive & @LF & _
    "Dir=" & $dir & @LF & "File=" & $fName & @LF & _
    "Ext=" & $ext)
Edited by Valik

Share this post


Link to post
Share on other sites

perfect thanks alot guys!

i'm running around so much right now I can barely remember how to create a message box :huh2::D

and I like the part about the pocket knife hehe :)


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
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
Sign in to follow this  
Followers 0