Jump to content

StringRegExp and path


Recommended Posts

Well I certainly can see why Melba23 may be starting to get frustrated here.

You are asking for too many things that just can't be done readily in a single expression using the examples you have provided.

  • How are you getting this string that has to be parsed?
  • Are these files only local or is there a possibility that they will be a UNC path?
  • Do you want just Files or Files and Folders?
  • Will any of the paths be relative?
The first question is very important. If it's comming from a file then you may be able to pre-process the file before you start looking for the file paths. This is also possible with a simple string input but may or may not require more effort.

I did manage to search one of the regex forums that I'm on and had no working hits on there either.

I'll ask about it there if I have to but I think for now you should just answer the questions above.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

There are filenames that don't have an extension (like C:\Windows\System32\drivers\etc\hosts), so you can't rely on just regular expressions to do the job. A regular expression would either only look for files that have an extension, or also match folder names. To catch all files, you have to also collect directory names and filter them out later.

dim $aFiles[7] = [6, "Hi, C:\WINDOWS\explorer.exe", "C:\WINDOWS\system32\shell32.dll", "C:\", "C:\WINDOWS\", "Hi, run C:\WINDOWS\svchost.exe", "hardcore sample C:\Windows\System32\drivers\etc\hosts"]
;_arraydisplay($aFiles)

for $i = 1 to $aFiles[0]
    $path_and_file = stringregexp($aFiles[$i], "[a-z A-Z]\:\\.*", 1)
    if isarray($path_and_file) Then
        $filecheck = _isfile($path_and_file[0])
        if $filecheck = 1 then
            msgbox(0, "is a file", $path_and_file[0])
        Else
            msgbox(0, "is a directory", $path_and_file[0])
        EndIf
    EndIf
Next

func _isfile($string)
    $attrib = FileGetAttrib($string)
    If StringInStr($attrib, "D") Then return(0); return 0 if directory
    return(1); otherwise return 1
EndFunc

Thanks, but I already solved
Link to comment
Share on other sites

Well I certainly can see why Melba23 may be starting to get frustrated here.

You are asking for too many things that just can't be done readily in a single expression using the examples you have provided.

  • How are you getting this string that has to be parsed?
  • Are these files only local or is there a possibility that they will be a UNC path?
  • Do you want just Files or Files and Folders?
  • Will any of the paths be relative?
The first question is very important. If it's comming from a file then you may be able to pre-process the file before you start looking for the file paths. This is also possible with a simple string input but may or may not require more effort.

I did manage to search one of the regex forums that I'm on and had no working hits on there either.

I'll ask about it there if I have to but I think for now you should just answer the questions above.

Have solved
Link to comment
Share on other sites

  • Moderators

Snoomi,

Could you please explain how you solved it so that others might learn from your experience? :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 1 year later...

I have a similar problem and I'm running out of ideas! Sadly Snoomi didn't explained how he solved his problem, so I ask again...

How can I find all Pathes from this string:

'Path1="ServerNameTest File.txt" Path2=C:TestFolder Path3=D:testTestFileWithoutExtension'

 

I try to realize a function, which really finds all Pathes in a given String, doesn't matter if it's a File/Folder or if there are some quotes around.

Could anyone give me a helping hand?

Link to comment
Share on other sites

I have a similar problem and I'm running out of ideas! Sadly Snoomi didn't explained how he solved his problem, so I ask again...

 

How can I find all Pathes from this string:

'Path1="ServerNameTest File.txt" Path2=C:TestFolder Path3=D:testTestFileWithoutExtension'

 

 

I try to realize a function, which really finds all Pathes in a given String, doesn't matter if it's a File/Folder or if there are some quotes around.

 

Could anyone give me a helping hand?

Try this.

#include <Array.au3>

Local $sTestStr = _
        'Path1="\\ServerName\Test File.txt" Path2=C:\TestFolder\ ' & _
        'Path3=D:\test\TestFileWithoutExtension Path4=''\\AnotherServerName\Test File2.txt'''

$aRes = StringRegExp($sTestStr, "(?i)(?:Path\d+\=((?:""|')[^""']+(?:""|')|[^\h]+) ?)", 3)
_ArrayDisplay($aRes, "With Quotation Marks")

For $i = 0 To UBound($aRes) - 1
    $aRes[$i] = StringRegExpReplace($aRes[$i], "([""'])", "")
Next
_ArrayDisplay($aRes, "Without Quotation Marks")


; Or,  no arrays method.

Local $sWithQuotes = StringRegExpReplace($sTestStr, "(?i)(?:Path\d+\=((?:""|')[^""']+(?:""|')|[^\h]+) ?)", "\1" & @CRLF) ; One path per line.
;Local $sWithQuotes = StringRegExpReplace($sTestStr, "(?i)(?:Path\d+\=((?:""|')[^""']+(?:""|')|[^\h]+) ?)", "\1 ") ; Space separation
MsgBox(0, " With Quotation Marks ", $sWithQuotes, 4)

Local $sWithOutQuotes = StringRegExpReplace($sWithQuotes, "([""'])", "")
MsgBox(0, "** Without Quotation Marks **", $sWithOutQuotes, 5)
Lazaegalli's and my test string only encloses a path containing spaces within quotes.
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...