Jump to content

is it available? FileGetOnlyName()


 Share

Recommended Posts

  • Moderators

$explorer = "C:\WINDOWS\explorer.exe"
ConsoleWrite($explorer&@CRLF&"FileName:  "&getFileName($explorer)&@CRLF&"ParentDir: "&getParentDir($explorer)&@CRLF&"Extension: "&getExtension($explorer)&@CRLF)

Func getFileName($fpath)
    Return StringMid($fpath,StringInStr($fpath,"\",0,-1)+1)
EndFunc

Func getParentDir($fpath)
    Return StringMid($fpath,1,StringInStr($fpath,"\",0,-1))
EndFunc

Func getExtension($fpath)
    Return StringMid($fpath,StringInStr($fpath,".",0,-1)+1)
EndFunc

another way :P

That's just a sloooooooooooow way of doing a _PathSplit().

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

ahhhhm... look at file.au3 in the includedirectory.. i don´t think this is faster, it uses the same functions :P

Func _PathSplit($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 $pos

    ; Create an array which will be filled and returned later
    Local $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
    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)

    ; 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

    $pos = StringInStr($fname, ".", 0, -1)
    If $pos Then
        $ext = StringRight($fname, StringLen($fname) - ($pos - 1))
        $fname = StringLeft($fname, $pos - 1)
    EndIf

    ; 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   ;==>_PathSplit
Link to comment
Share on other sites

  • Moderators

I used _PathSplit(), I meant to Say StringRegExp's ... however, now that we are on the subject... _PathSplit() does do much more and has been tested to be proficient.

I have my own _PathSplit() function using nothing but RegEx that's much faster than that even ... but it's always safe to use something that's been used and trusted over the years.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I used _PathSplit(), I meant to Say StringRegExp's ... however, now that we are on the subject... _PathSplit() does do much more and has been tested to be proficient.

It does things that sometimes aren´t needed

I have my own _PathSplit() function using nothing but RegEx that's much faster than that even ... but it's always safe to use something that's been used and trusted over the years.

Cdma1X asked for alternatives, I gave him one :P
Link to comment
Share on other sites

I have my own _PathSplit() function using nothing but RegEx that's much faster than that even ... but it's always safe to use something that's been used and trusted over the years.

SmOke_N, can i see your function?
Link to comment
Share on other sites

  • Moderators

SmOke_N, can i see your function?

Func _SplitPath($s_path, ByRef $s_drive, ByRef $s_dir, ByRef $s_fname, ByRef $s_ext)
    If $s_path = "" Or StringInStr($s_path, '<>"|?*') Then Return SetError(1, 0, 0)
    
    Local $a_split = StringRegExp($s_path, "^(\w*:*)([\w\W]*[\\/]+)([\w\W]*)(\.[\w\W]*)\z", 1)
    
    If @error Then
        $a_split = StringRegExp($s_path, "^(\w*:*)([\w\W]*[\\/]+)([\w\W]*)(\.*[\w\W]*)\z", 1)
        If @error Then
            $a_split = StringRegExp($s_path, "^(\w*:*)\z", 1)
            If @error Then Return SetError(2, 0, 0)
        EndIf
    EndIf
    
    If UBound($a_split) = 1 Then
        $s_drive = $a_split[0]
        $s_dir = ""
        $s_fname = ""
        $s_ext = ""
    Else
        $s_drive = $a_split[0]
        $s_dir = $a_split[1]
        $s_fname = $a_split[2]
        $s_ext = $a_split[3]
    EndIf
    
    Local $avArray[5] = [$s_path, $s_drive, $s_dir, $s_fname, $s_ext]
    Return $avArray
EndFunc

Edit:

Removed emoticons

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I have one of my own to: _PathSplitByRegExp() :P

Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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...