Jump to content

_FileGetDir


cdkid
 Share

Recommended Posts

Anywhoo, i got bored so i decided to make this. retrieves the directory of a given file.

Func _FileGetDir($sFile)
    Local $s, $i
    $s = ""
    If FileExists($sFile) Then
        $sFile = StringSplit($sFile, "\")
        For $i = 1 To $sFile[0] - 1
            If $i > 1 Then
                $s = $s & "\" & $sFile[$i]
            Else
                $s = $s & $sFile[$i]
            EndIf
        Next
        Return $s
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc  ;==>_FileGetDir

I dont know if this already exists, but i couldnt find anything of the sort. I found this useful for using FileOpenDialogs, and using the previous directory.

:o

~cdkid

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

Hi,

I think there are many ways to do something like that, but I like your way.

Maybe I can use it sometimes. :o

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Here's my go at it...

Func _FileGetDir($File)
    For $i = 1 To StringLen($File)
        If StringMid($File,$i,1) = "\" Then $Path_Len = $i
    Next
        
    $File = StringLeft($File,$Path_Len)
    Return($File)
EndFunc

Enjoy!

*EDIT* - snipped out the part for the UNC conversion ... makes the function a little smaller (changed the function name too).

Edited by cjconstantine
Link to comment
Share on other sites

HI,

or just something I found elsewhere in the forum. :o

$path = "C:\folder 1\folder 2\autoit.exe"

$array = StringSplit($path, "\")
$path = StringTrimRight($path, StringLen($array[$array[0]]))
MsgBox(0,"Path", $path)

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • Developers

_PathSplit

--------------------------------------------------------------------------------

Splits a path into the drive, directory, file name and file extension parts. An empty string is set if a part is missing.

#include <File.au3>

_PathSplit($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi,

hmmh that is also cool. Then it could be something like:

#include <file.au3>
#include <array.au3>
Dim $szDrive, $szDir, $szFName, $szExt, $path = "C:\folder 1\folder 2\autoit.exe"
$array = _PathSplit ($path, $szDrive, $szDir, $szFName, $szExt)
MsgBox(0, "Demo _PathSplit()", $array [1] & $array [2])

So long,

Mega

P.S.: But why do I have to declare all these variables? Dim $szDrive, $szDir, $szFName, $szExt? If only want to split a complete path given by $path = "C:\folder 1\folder 2\autoit.exe" ?

Anyway, nice to know function.

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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