Jump to content
AntiVirusGuy

Separate or split path from file name from extension

Recommended Posts

AntiVirusGuy

Apparently I am not smart enough to use _PathSplit so I made this simple function

;#FUNCTION# ====================================================================================================

================
; Name...........: _SBfpsplit
; Description ...: Splits a file path into Drive, path, filename, or extension.
; Syntax.........: _SBfpsplit($SBPath, $SBType);$SBpath (file path to evaluate) , $SBType (option) 
; Parameters ....:
;|1 = Drive                 ex. D:
;|2 = Path                   ex. D:\1111\dir\file.txt               
;|3 = File name          ex. file.txt
;|4 = Extension           ex. txt
;|5 = Drive letter only ex. D
; Author ........: Scott E. Brown ripandreplace@eset.com
; Modified.......:04/03/2009
; Remarks .......: 
; Related .......:
; Link ..........;
; Example .......; _SBfpsplit("D:\1111\dir\file.txt, 1)
;
;$path = "D:\1111\dir\test.txt"
;ConsoleWrite(_SBfpsplit($path, 1) & @CRLF)
;ConsoleWrite(_SBfpsplit($path, 2) & @CRLF)
;ConsoleWrite(_SBfpsplit($path, 3) & @CRLF)
;ConsoleWrite(_SBfpsplit($path, 5) & @CRLF)
; ====================================================================================================

===========================
Func _SBfpsplit($SBPath, $SBType);$SBpath = file path to evaluate , $SBType = 1 for Drive, 2 for Path, 3 for File name, 4 for extension, 5 for drive letter only
    Local $SBfile, $SBSplit, $SBdrive, $SBfilepath, $SBnumber 
    $SBSplit = StringSplit($SBPath, "\"); split into array
    $SBnumber = $SBSplit[0]; the number of strings returned 
    $SBfilepath = ""
    for $1 = 1 to $SBnumber -1
        $SBfilepath = $SBfilepath & $SBSplit[$1] & "\"; path
    Next
    $SBfile = $SBSplit[($SBsplit[0])]; file
    $SBdrive = $SBSplit[1]; drive
    $SBfs = StringSplit($SBPath, "."); split into array
;MsgBox(4096, "Path", $SBSfs[0])
    if $SBfs[0] = 1 then 
        $SBExt = ""; no extension found
    Else
        $SBExt = $SBfs[($SBfs[0])]; last . extentsion
    EndIf

    If $SBType = 1 then Return $SBdrive
    If $SBType = 2 then Return $SBfilepath
    If $SBType = 3 then Return $SBfile
    If $SBType = 4 then Return $SBExt 
    If $SBType = 5 then Return StringLeft($SBdrive, 1) 
EndFunc

file_path_include.au3

Edited by ssebrownatcolby

Share this post


Link to post
Share on other sites
ReCoder

Nice work.

But you can create a wrapper for _PathSplit(...) (include <File.au3>) like this ...

#cs
    String _GetFilename(<FullPath+Filename>)
    returns the filename without extension from full path
    used include    <File.au3>
    used func       _PathSplit(...)
#ce
func _GetFilename($psFilename)
    local $szDrive, $szDir, $szFName, $szExt
    _PathSplit($psFilename, $szDrive, $szDir, $szFName, $szExt)
    return $szFName
EndFunc

#cs
    String _GetExtension(<FullPath+Filename>)
    returns the extension from filename
    used include    <File.au3>
    used func       _PathSplit(...)
#ce
func _GetExtension($psFilename)
    local $szDrive, $szDir, $szFName, $szExt
    _PathSplit($psFilename, $szDrive, $szDir, $szFName, $szExt)
    return $szExt
EndFunc

#cs
    String _GetDirname(<FullPath+Filename>)
    returns the drive+directory from full path
    used include    <File.au3>
    used func       _PathSplit(...)
#ce
func _GetDirname($psFilename)
    local $szDrive, $szDir, $szFName, $szExt
    _PathSplit($psFilename, $szDrive, $szDir, $szFName, $szExt)
    return $szDrive & $szDir
EndFunc

Share this post


Link to post
Share on other sites
nobbe

after all its not so complicated? after the function call PathSplit "ALL" parameter are already filled (byref)

#include <File.au3>

local $szDrive, $szDir, $szFName, $szExt

$full = "c:\dir1\dir2\test.wav"

_PathSplit($full, $szDrive, $szDir, $szFName, $szExt)
; after the function call "ALL" parameter are already filled

MsgBox(0, $full, "Drive = "& $szDrive & @crlf & "Dir = "& $szDir & @crlf &  "Filename = "& $szFName & @crlf &   "Extension = "& $szExt);

exit
Edited by nobbe

Share this post


Link to post
Share on other sites
AntiVirusGuy

after all its not so complicated? after the function call PathSplit "ALL" parameter are already filled (byref)

#include <File.au3>

local $szDrive, $szDir, $szFName, $szExt

$full = "c:\dir1\dir2\test.wav"

_PathSplit($full, $szDrive, $szDir, $szFName, $szExt)
; after the function call "ALL" parameter are already filled

MsgBox(0, $full, "Drive = "& $szDrive & @crlf & "Dir = "& $szDir & @crlf &  "Filename = "& $szFName & @crlf &   "Extension = "& $szExt);

exit

I didn't understand what byref meant but a coworker explained it. So now I get how it works blond moment I guess.

Share this post


Link to post
Share on other sites
HardXOR
$timer = TimerInit()

Local $P_drive, $P_dir, $P_filename, $P_ext
_PathSplit($FilePath, $P_drive, $P_dir, $P_filename, $P_ext)

MsgBox(0, "", TimerDiff($timer))

this took on my machine 0.09 - 0.11 ms

$timer = TimerInit()

$Filename = StringRegExp($FilePath, '.*\\(.*)', 1)

MsgBox(0, "", TimerDiff($timer))

and this code took 0.01 - 0.02 ms

i know its ms, but still like faster code, especially for files... noone knows if you want use it thousands or milions times, and 10x faster code is quite usefull

Share this post


Link to post
Share on other sites
iamtheky

same speed increase getting filename without regexp, just for sport

stringright($FilePath , StringLen($FilePath) - StringInStr($FilePath , "\" , 0, -1))

and then dir, naturally

stringleft($FilePath , StringInStr($FilePath , "\" , 0, -1))

and ext

stringright($FilePath , StringLen($FilePath) - StringInStr($FilePath , "." , 0, -1))

 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
jchd

HardXOR,

Resurrecting a 6-year old thread for cutting milliseconds is indeed something.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
iamtheky

egad, necromancer gets me again!


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.