Jump to content

Recommended Posts

Posted

Hallo Members,

I'm looking for a good regex to get the drive letter and the last two folders from a file path,

If the path is to long for the label width then show drive +   ellipses and two last folders.

Drive:\(ellipses)\folder\folder

ex. D:\...\folder\folder

and when the folder is in the root of the drive then show D:\Folder


The test GUI

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.12.0
 Author:         Mecano

 Script Function: ELLIPSIS
        Long path:   Drive:\...\Folder\Folder
           if root then Drive:\Folder

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
$sFile = "F:\Just a folder\in a another folder\and another\This-dir\And-this-dir"
$EllipsisPath = StringRegExpReplace($sFile, '\w[a-zA-Z \\]+\\', '') ; <- This needs another regex

;no ellipsis needed, for testing purposes only 
$sUSB = "K:\Just a folder"
$PathforUSB = StringRegExpReplace($sUSB, '\w[a-zA-Z \\]+\\', '')

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Drive + Two last folders of path", 327, 236, 192, 124)
$Button1 = GUICtrlCreateButton("Test label", 40, 168, 97, 33)
$Label1 = GUICtrlCreateLabel("F:\...\This-dir\And-this-dir", 40, 12, 200, 40) ; <- Looks good but not dynamic
GUICtrlSetColor($Label1, 32768)
$Label2 = GUICtrlCreateLabel($sFile, 40, 40, 200, 40,  $DT_END_ELLIPSIS) ; <- not the last two directorys
GUICtrlSetColor($Label2, 16711680)
$Label3 = GUICtrlCreateLabel("Var label1", 40, 72, 200, 40)
GUICtrlSetColor($Label3, 16711680)
$Label4 = GUICtrlCreateLabel("Var label", 40, 104, 200, 40)
GUICtrlSetColor($Label4, 16711680)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            GUICtrlSetData($Label3, $EllipsisPath) ; $DT_END_ELLIPSIS <- works only on GUICtrlCreateLabel
            GUICtrlSetData($Label4, $PathforUSB) 

    EndSwitch
WEnd

 

 

Thanks in advance

 

 

Posted

and without regex

$sFile1 = "F:\Just a folder\in a another folder\and another\This-dir\And-this-dir"
;~ $sFile1 = "G:\This-dir\And-this-dir\"   ; final "\" optional
;~ $sFile1 = "K:\Just a folder"



$sOut = ubound(stringsplit($sFile1 , "\" , 2)) > 4 ? stringleft($sFile1 , 3) & "...\" & stringreverse(stringsplit(stringreverse($sFile1) , "\" , 2)[0] & "\" & stringsplit(stringreverse($sFile1) , "\" , 2)[1]) : $sFile1

msgbox(0, '' , $sOut)

 

  Reveal hidden contents

Posted

Oh Yeah, mikell you saved my day again :)

boththose, thanks for your contribution, but I need the regex

Big thanks for the solution.

Posted (edited)
  On 8/6/2015 at 5:11 PM, Mecano said:

boththose, thanks for your contribution, but I need the regex

Why so? Is it to show off to your coding buddies?

Though the native version could be improved a little bit and the regex version is alot neater, using native string functions doesn't necessarily mean bad programming. So please enlighten me, why do you need a regex version? What is your reason or thought process as to why that is?

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

boththose,
StringSplit is not a handy way to manage an optional final "\" (i.e. your code fails on "G:\test\This-dir\And-this-dir\" )
Assuming that this final "\" does not exist, StringSplit works nice but it's much better to call it only once

$sFile1 = "F:\Just a folder\in a another folder\and another\This-dir\And-this-dir"
$sFile2 = "G:\test\This-dir\And-this-dir"
$sFile3 = "K:\Just a folder"

Msgbox(0,"", _ShortPath($sFile1) & @crlf & _ShortPath($sFile2) & @crlf & _ShortPath($sFile3) )

Func _ShortPath($path)
   Local $tmp = StringSplit($path, "\")
   Return ($tmp[0] > 3) ? ($tmp[1] & "\...\" & $tmp[$tmp[0]-1] & "\"  & $tmp[$tmp[0]]) : $path
EndFunc

 

Posted

you gave your edge case both option and finality?   As you evidenced, I also didnt really need to flip it around and take it from behind..... but the ladies love it.

  Reveal hidden contents

Posted


@guinness,

"Though the native version could be improved a little bit and the regex version is alot neater"

I totally agree with that

"Why so? Is it to show off to your coding buddies?"

Not at all, just want to learn regex and try to understand the regex
I was practicing regex and get stuck, I'm not a regex guru

this was no variable enough:

$FullPath    = "F:\Just a folder\in a another folder\and another\This-dir\And-this-dir"
$StringLeft  = StringLeft($FullPath, 2)
$StringRight = StringRight($FullPath, 21);
MsgBox(0, "ShortPath", $StringLeft & "\...\" & $StringRight)

So searching for examples I found this topic https://www.autoitscript.com/forum/topic/166301-short-path-anywhere-at-the-string/?do=findComment&comment=1214608

$string = 'regedit.exe /e:a "D:\data\backup\laptop\CCleaner\CCleaner.reg" "HKEY_CURRENT_USER\Software\Piriform\CCleaner"'
$short = StringRegExpReplace($string, '("[^\\]+\\)(?:[^\\"]+\\)*([^"]+")', "$1...\\$2")
ConsoleWrite($short)

Tried to understand the regex, thats why I wrote "you saved my day"

K.* was something new for me

http://www.regular-expressions.info/keep.html

 

 

Posted

Oh you are learning ! Sorry, here are the comments :

'^\w:\\\K.*(?=\\[^\\]+\\[^\\]+\\?$)'

^        : beginning of string
\w:\\    : a word char + colon + antislash
\K       : escape sequence meaning 'forget what you've seen before'
.*       : the part to replace
(?=      : lookahead 'followed by'
\\[^\\]+  : antislash + one or more non-antislash char. Used twice 
\\?      : the optional final antislash
$        : end of string

:)

Posted

mikell, good explanation, as I wrote earlier \K is something new for me. :D

With this fantastic forum and for example SRETester I learn every time something new.

Next thing is improve my skills with AutoIt/Microsoft.XML DOM like jdelaney wrote

:bye:

 

 

Posted

@Mecano, fair enough with the explanation.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
×
×
  • Create New...