Jump to content

_FileGetCommonPath


TheSaint
 Share

Recommended Posts

_FileGetCommonPath - This function would be a nice addition to the include file - File.au3.

I haven't got time to go into all the necessaries required for inclusion, but I see it as something like the following -

1) The function being passed two paths and a switch to maybe indicate trailing backslash (is required, but optional) - i.e.

_FileGetCommonPath(C:\Windows\Notepad.exe, C:\Windows\System32\Calc.exe, 1)

2) I see the function returning the common path - i.e.

C:\Windows or C:\Windows\

3) I also see it returning the two intial paths minus the common element - i.e.

Notepad.exe & System32\Calc.exe

Obviously these would all be returned in an array.

This would be a really nice addition, if someone wants to have a go at it?

:mellow:

Here is the code I've always used a version of in the past -

Func FileGetCommonPath($p1, $p2)
     $fnam = StringSplit($p1, "\")
     $fnam = $fnam[$fnam[0]] 
     For $p = 1 To StringLen($p1)
         If StringLeft($p1, $p) = StringLeft($p2, $p) Then
             $common = StringLeft($p1, $p)
             If StringTrimLeft($p1, StringLen($common)) = $fnam Then ExitLoop
         Else
             ExitLoop
         EndIf
     Next
     $f1 = StringTrimLeft($p1, StringLen($common))
     $f2 = StringTrimLeft($p2, StringLen($common))
 EndFunc;=> FileGetCommonPath
Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Did you think of a func like this?

#include <Array.au3>
Dim $Files[3] = ["C:\Windows","C:\Windows\Notepad.exe", "C:\Windows\System32\Calc.exe"]
$path = _FileGetCommonPath($Files)
_ArrayDisplay($path)
Dim $Files[2] = ["C:\Windows\file.1","C:\Windows\file.1.2"]
$path = _FileGetCommonPath($Files)
_ArrayDisplay($path)

Dim $Files[2] = ["C:\Windows\same.Name","C:\Windows\same.name"]
$path = _FileGetCommonPath($Files)
_ArrayDisplay($path)

Dim $Files[1] = ["C:\Windows\fg.h"]
$path = _FileGetCommonPath($Files)
MsgBox(0, '', $path)
;===============================================================================
;
; Function Name:   _FileGetCommonPath
; Description::    Takes an Array of files and returns the common Path
; Parameter(s):    $arFiles - the array of files
; Requirement(s):  
; Return Value(s): Array: array[0] contains the common path
;                         array[i] contains the remains of the paths
;                  If only 1 file given: the file as String
;                  Error: 0 and sets @error:
;                            1 - not an Array
; Author(s):       Prog@ndy
;
;===============================================================================
;
Func _FileGetCommonPath($arFiles)
    If Not IsArray($arFiles) Then Return SetError(1,0,0)
    If UBound($arFiles)=1 Then Return $arFiles[0]
    For $i = 0 To UBound($arFiles)-1
        $arFiles[$i] = StringReplace($arFiles[$i],"/","\")
    Next
    Local $Letters = StringSplit($arFiles[0],"\"),$CommonPath,$Len,$Pos=1,$Part,$PartLen
    For $i = 1 To UBound($Letters)-1
        $Len = StringLen($Letters[$i])
        ConsoleWrite($Letters[$i] & @CRLF)
        For $j=1 To UBound($arFiles)-1
            ConsoleWrite(StringMid($arFiles[$j],$Pos,$Len) & @CRLF)
            $Part = StringMid($arFiles[$j],$Pos,$Len+1) 
            $PartLen = StringLen($Part)
            Select
                Case $PartLen <= $Len
                    ExitLoop 2
                Case $Part <> ($Letters[$i]&"\")
                    ExitLoop 2
            EndSelect
        Next
        $CommonPath &= $Letters[$i]&"\"
        $Pos += $Len+1
    Next
    ReDim $arFiles[UBound($arFiles)+1]
    For $i = UBound($arFiles)-2 To 0 Step -1
        $arFiles[$i+1] = StringTrimLeft($arFiles[$i],StringLen($CommonPath))
    Next
    $arFiles[0] = $CommonPath
    Return $arFiles
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Did you think of a func like this?

Thanks @ProgAndy - that will probably do the trick, I'll try it shortly. :)

You didn't think an optional switch for a trailing backslash a good idea?

By default, I'd leave the backslash off (i.e. 0), but having it available (i.e. 1) would save on coding when needed. :mellow:

How do you now get it included in future versions of File.au3 ... request to Jon, etc I suppose?

:(

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Hmm.. I don't know, where to post a request. Possibly requests should be added to bugtracker? And to remove trailing backslash... this is just one more line :mellow:

;===============================================================================
;
; Function Name:   _FileGetCommonPath
; Description::    Takes an Array of files and returns the common Path
; Parameter(s):    $arFiles   - the array of files
;                  $AddBSlash - [optional] add a backslash to the common path (default = False)
; Requirement(s):  
; Return Value(s): Array: array[0] contains the common path
;                         array[i] contains the remains of the paths
;                  If only 1 file given: the file as String
;                  Error: 0 and sets @error:
;                            1 - not an Array
; Author(s):       Prog@ndy
;
;===============================================================================
;
Func _FileGetCommonPath($arFiles,$AddBSlash=False)
    If Not IsArray($arFiles) Then Return SetError(1,0,0)
    If UBound($arFiles)=1 Then Return $arFiles[0]
    For $i = 0 To UBound($arFiles)-1
        $arFiles[$i] = StringReplace($arFiles[$i],"/","\")
    Next
    Local $Letters = StringSplit($arFiles[0],"\"),$CommonPath,$Len,$Pos=1,$Part,$PartLen
    For $i = 1 To UBound($Letters)-1
        $Len = StringLen($Letters[$i])
        ConsoleWrite($Letters[$i] & @CRLF)
        For $j=1 To UBound($arFiles)-1
            ConsoleWrite(StringMid($arFiles[$j],$Pos,$Len) & @CRLF)
            $Part = StringMid($arFiles[$j],$Pos,$Len+1) 
            $PartLen = StringLen($Part)
            Select
                Case $PartLen <= $Len
                    ExitLoop 2
                Case $Part <> ($Letters[$i]&"\")
                    ExitLoop 2
            EndSelect
        Next
        $CommonPath &= $Letters[$i]&"\"
        $Pos += $Len+1
    Next
    ReDim $arFiles[UBound($arFiles)+1]
    For $i = UBound($arFiles)-2 To 0 Step -1
        $arFiles[$i+1] = StringTrimLeft($arFiles[$i],StringLen($CommonPath))
    Next
    If $AddBSlash = False Then $CommonPath = StringTrimRight($CommonPath,1)
    $arFiles[0] = $CommonPath
    Return $arFiles
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

@ProgAndy - while what you've done works, you want an array to be sent to the function. I would prefer just to send paths, i.e.

$path_1 = "C:\Windows\Notepad.exe"

$path_2 = @SystemDir & "\Calc.exe"

$results = _FileGetCommonPath($path_1, $path_2, 0)

$results[0] = "C:\Windows"

$results[1] = "Notepad.exe"

$results[2] = "System32\Calc.exe"

etc

or for a trailing backslash -

$results = _FileGetCommonPath($path_1, $path_2, 1)

$results[0] = "C:\Windows\"

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

That's not to say I don't see the value in using an array, when using more than two paths. If both methods could somehow be implemented, that would be great?

Or use another function - _FileArrayGetCommonPath

:(

P.S. I haven't tested the idea, but obviously you need to cater for different drives - either just return full paths and $path[0] is empty, or have another switch to ignore drive letters, or both. :mellow:

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

well, rename _FileGetCommonPath to _FileArrayGetCommonPath

and use this as _FileGetCommonPath:

Func _FileGetCommonPath($path1,$path2,$BS=0)
   Local $array[2] = [$path1,$path2]
   $array = _FileArrayGetCommonPath($array,$BS)
   Return Seterror(@error,0,$array)
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

well, rename _FileGetCommonPath to _FileArrayGetCommonPath

and use this as _FileGetCommonPath:

Func _FileGetCommonPath($path1,$path2,$BS=0)
    Local $array[2] = [$path1,$path2]
    $array = _FileArrayGetCommonPath($array,$BS)
    Return Seterror(@error,0,$array)
 EndFunc
You're the goods ProgAndy, and I should have seen that coming (above), but I'm just a little s l o w sometimes? Could be age or lack of sleep or ....

You may have missed the little addendum to my last post?

Thanks again!

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

ATM, array[0] is empty when the Paths are on different drives :mellow:

igonre drive letters, i don't see the point in it .. if you need it, just delete or replace them before you call the func :(

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

ATM, array[0] is empty when the Paths are on different drives :)

igonre drive letters, i don't see the point in it .. if you need it, just delete or replace them before you call the func ;)

@ProgAndy - you don't need to answer this, as I already PM'd you with my answer the other day, but it looks bad if I don't respond here ... now that I can! ;)

You may be right in what you say above, but I was thinking along the lines of comparing 2 paths minus their drive letters - like what I made the code in my first post for (above). My thinking was that it would save a few lines of code to add a switch to remove drive letters each time. Anyway like I said to you, that idea was very much an afterthought and the functions are just great without it ... I'm just one of those people who like to keep adding features ... but then I curse Microsoft for making Office so complex ... the trick is knowing when to stop I guess. :mellow:

:(

P.S. I also put in a request (ticket) for these 2 Functions to be added to future versions of File.au3

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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