Sign in to follow this  
Followers 0
sHaDYvB

Question , Filegetparentfolder Is There Is Something To Do It ?

14 posts in this topic

I'm been developing a script which i will use later as a Silent Batch file ( .BAT ) Parser ..

and i need to launch the file in it's parent directory as a WORKING DIR ..

till now i've not find a way to get the parent dir. of the file passed the the script ..

i need something like [ FileGetParentFolder ] or something , which i give a file name , and outputs the parent dir. name ..

Thank you for your help ..

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

...

...

Big banner, top of the page

This is not a general support forum!

But to answer your question

Func _FileGetPrevFolder($file, $num = 1)
    $file = StringSplit($file, "\")
    If $num > UBound($file) - 2 Then
        SetError(1)
        Return 0
    EndIf
    Return $file[UBound($file, 1) - $num - 1]
EndFunc

To get the folder the file is in use _FileGetPrevFolder($file, 1) to get the parent folder use _FileGetPrevFolder($file, 2) etc.

~cdkid

Edited by 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!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

1st thank you for replying ..

and .. sry i asked the wrong question ..

i wanted to know the full path ( to pass it to ( WorkingDir ) in RUN command ) ..

THE FULL PATH , how can i do this , i've been trying since long and haven't figured a way .. :s

Edited by sHaDYvB

Share this post


Link to post
Share on other sites

1st thank you for replying ..

and .. sry i asked the wrong question ..

i wanted to know the full path ( to pass it to ( WorkingDir ) in RUN command ) ..

THE FULL PATH , how can i do this , i've been trying since long and haven't figured a way .. :s

MsgBox(0,"",@WorkingDir)

MsgBox(0,"",_FileGetPrevFolder(@WorkingDir,1))

Func _FileGetPrevFolder($file, $num = 1)
    $file = StringSplit($file, "\")
    If $num > UBound($file) - 2 Then
        SetError(1)
        Return 0
    EndIf
    Return $file[UBound($file, 1) - $num - 1]
EndFunc

Share this post


Link to post
Share on other sites

No , u didn't get me right

i want to know the FULL PATH of the file specified , to use it AS WorkingDir ,

not wanting to know the actual working file of the script ..

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

No , u didn't get me right

i want to know the FULL PATH of the file specified , to use it AS WorkingDir ,

not wanting to know the actual working file of the script ..

No idea .. but if you know the file :

FileGetLongName

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

Returns the long path+name of the path+name passed.

FileGetLongName ( "file" )

i know how to do it in Vbscript (*.vbs)

Set oShell = CreateObject("Wscript.Shell")

sCD =oShell.CurrentDirectory

WScript.Echo sCD

sCD="c:\windows\"

WScript.Echo sCD

EDIT:

added comspec sample

Edited by Lapo

Share this post


Link to post
Share on other sites

I know also how to do it in VB ,

but it is about AutoIT

and BTW , the VBScript u put is the same as the first one , it gets the current working dir , not the path of the file ...

Waiting for someone to help :)

Share this post


Link to post
Share on other sites

Like this?

#include <file.au3>
$fullfilepath = "C:\Windows\explorer.exe"
Dim $szDrive, $szDir, $szFName, $szExt
$TestPath = _PathSplit($fullfilepath, $szDrive, $szDir, $szFName, $szExt)

Msgbox(0,"Parent Folder",$TestPath[1] & $TestPath[2])

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Thank you kandie man ,

but there is a problem :

C:\DocS\Administrator\Desktop\Script.au3 (8) : ==> Unknown function name.: 
$TestPath = _PathSplit($fullfilepath, $szDrive, $szDir, $szFName, $szExt) 
$TestPath = ^ ERROR

Share this post


Link to post
Share on other sites

$fullfilepath = "C:\DocS\Administrator\Desktop\Script.au3"
Dim $szDrive, $szDir, $szFName, $szExt
$TestPath = _PathSplit($fullfilepath, $szDrive, $szDir, $szFName, $szExt)

Msgbox(0,"Parent Folder",$TestPath[1] & $TestPath[2])


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 $i, $pos; For Opt("MustDeclareVars", 1)
    
; 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
    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  ;==>_PathSplit

Share this post


Link to post
Share on other sites

Thanks Lapo. The reason why it wasn't working shady is because it wasn't including the file.au3 correctly. If it doesn't include the file.au3 the function is left undefined.


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

thank you very very much for your help

this time it worked ,

i'll try to make use of it , and will tell u the result ..

Share this post


Link to post
Share on other sites

how can i include the file , and does i have to include it also when i compile the script ??

Share this post


Link to post
Share on other sites

Have a look at #include <Somename.au3>

Another possible one (since I think DOS-Apps don't like to be run from UNC). :)

Dim $Prefix, $Suffix

_StringSplit ('E:\Scripts\Helpers\DApwal\Dapwal.exe', '\', $Prefix, $Suffix)
ConsoleWrite ( $Prefix & @CR & $Suffix&@CR)

_StringSplit ('E:\Dapwal.exe', '\', $Prefix, $Suffix)
ConsoleWrite ( $Prefix & @CR & $Suffix&@CR)

Func _StringSplit ($a, $b, ByRef $c, ByRef $d); Split input at the last devider Param.: $a=var, $b=devider, $c=Prefix, $d=suffix
    Local $counter
    $counter = 1
    While 1
        $Error = StringInStr($a, $b, 0, $counter)
        If $Error <> 0 Then
            $counter = $counter + 1
        Else
            $Pos = StringInStr($a , $b, 0, $counter - 1)
            ExitLoop
        EndIf
    WEnd
    $c = StringLeft($a, $Pos - 1)
    $d = StringTrimLeft($a, $Pos)
EndFunc

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