Jump to content

RegExp assistance please


Recommended Posts

I have a terrible relationship with this function, so looking for some assistance please. I need 4 codes.

1/ I have this, but it is unnecessarily complicated, and I believe it could be done easier and faster if a regexp function can be put inside an _ArraySearch function fo look for a numeric value greater than 0.

$u += 1
    If $u = 15 Then
        For $u = 0 to UBound($aDiskLoad) - 1
            If $aDiskLoad[$u] > 0 Then
                $sDiskLoad = False
            EndIf
        Next
    EndIf

2/ I have this, but it doesn't seem to be working right. 

Global $sAcceptedFiles = ".jpg|.mp4"
        ;If the file is anything except mp4, jpg skip and continue loop
        If Not StringRegExp($sExtension, "\b(" & $sAcceptedFiles & ")\b") Then
            ConsoleWrite(@CRLF & "> Invalid File Type." & @CRLF)
            ContinueLoop
        EndIf

3/ I have an array with a file list and need to extract any file that doesn't have a specific file name format of "YYYY-MM-DD_HHMMSS_P" (That "P" could also be a "V") and a file extension.

4/ The last one is not coming to my mind right now....I'll have to come back to this one.

 

Thanks.

Link to post
Share on other sites

1

If _ArraySearch($aDiskLoad) <> -1 Then $sDiskLoad = False

2

Global $sAcceptedFiles = ".jpg|.mp4"
;If the file is anything except mp4, jpg skip and continue loop
If Not StringRegExp($sExtension, StringReplace($sAcceptedFiles, ".", "\.") & "$" Then
ConsoleWrite(@CRLF & "> Invalid File Type." & @CRLF)
ContinueLoop
EndIf

3

If Not StringRegExp($string, "^\d{4}(-\d{2}){2}_\d{6}_[PV]$") Then ConsoleWrite("error")

 

Link to post
Share on other sites

Thanks for 2 and 3, I'll see how they work out when I get home. But I don't understand how what you are doing with 1.

As far as 2 is concerned with my original version, is it not working because I have the "." in the extensions?

Edit:

2 seems to be case sensitive, how do i make it none sensitive?

Edited by Champak
Link to post
Share on other sites

This isn't working for me as I want it to. If I feed an array directly to it, no issue. But if I do it from the file open dialogue it doesn't see the files it should match. I tried stripping WS and CR and put the quotes around the variable just to make sure. I don't see what could be the issue.

$FileSelect = FileOpenDialog("File Rename", "F:\", "Images (*.jpg)|Videos (*.mp4)", $FD_MULTISELECT, "", $hForm)

$aDirContent_New = StringSplit($sFileSelect, "|", 2)

_ArrayDisplay($aDirContent_New)

    For $i = UBound($aDirContent_New) - 1 To 0 Step - 1
        ConsoleWrite($i & "'" & $aDirContent_New[$i] & "'" & @CRLF)
        If StringRegExp($aDirContent_New[$i], "^\d{4}(-\d{2}){2}_\d{6}_[PV]$") Then
            ConsoleWrite("> " & $i & @CRLF)
            _ArrayDelete($aDirContent_New, $i)
            ContinueLoop
        EndIf
        ;$aDirContent_New[$i] = $aDirContent_New[0] & "\" & $aDirContent_New[$i]
    Next
    
_ArrayDisplay($aDirContent_New)

Note: I took out the "Not" because I was just testing some things.

Link to post
Share on other sites

maybe this help's:

#include <File.au3>
#include <WinAPISysWin.au3>
Global $GText
Local $hTimerProc = DllCallbackRegister('_TimerProc', 'none', 'hwnd;uint;uint_ptr;dword')
Global $g_iCount = 0
Local $iTimerID = _WinAPI_SetTimer(0, 0, 1000, DllCallbackGetPtr($hTimerProc))
$title = "File Rename"
$sFileSelect = FileOpenDialog($title, "F:\", "Images (*.jpg)|Videos (*.mp4)", $FD_MULTISELECT, "", 0)
$aDirContent_New = StringSplit($GText, "''", 2)
_ArrayDisplay($aDirContent_New)

    For $i = UBound($aDirContent_New) - 1 To 0 Step - 1
        ConsoleWrite($i & "'" & $aDirContent_New[$i] & "'" & @CRLF)
        If StringRegExp($aDirContent_New[$i], "^\d{4}(-\d{2}){2}_\d{6}_[PV]$") Then
            ConsoleWrite("> " & $i & @CRLF)
            _ArrayDelete($aDirContent_New, $i)
            ContinueLoop
        EndIf
        ;$aDirContent_New[$i] = $aDirContent_New[0] & "\" & $aDirContent_New[$i]
    Next

_ArrayDisplay($aDirContent_New)
Func _TimerProc($hWnd, $iMsg, $iTimerID, $iTime)
$GText = ControlGetText($title, "", "Edit1")
EndFunc   ;==>_TimerProc

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

Link to post
Share on other sites
  • Moderators

ad777,

When you reply in future, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - responders know what they wrote and it just pads the thread unnecessarily. Thanks in advance for your cooperation.

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 post
Share on other sites

@mikell That wasn't a typo, it was being passed through a function.

@ad777 Thanks, that worked, just had to change:

$aDirContent_New = StringSplit($GText, """", 2)

 

If StringRegExp($aDirContent_New[$i], "^\d{4}(-\d{2}){2}_\d{6}_[PV]$") Or $aDirContent_New[$i] = " " Then

 

So is this a bug with FileOpenDialog or is it known issue that can't be fixed?

Link to post
Share on other sites
  • Developers

Please start by posting a proper replication script showing your issue with some debug output before creating a bug report.
I see a lot of "it doesn't work" but have no idea what the real issue is, so start adding debugging to show the EXACT strings being returned in the different cases.

Bug report will be closed until there is a proper case to make for a dug.

Jos

Edited by Jos

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 post
Share on other sites

@Champak
I said this because using the provided script, with :
- typo corrected
- file extension included in the regex
then for me it works (the following is runnable)

#Include <Array.au3>
#include <FileConstants.au3>

$sFileSelect = FileOpenDialog("File Rename", @desktopdir & "\", "Images (*.jpg)|Videos (*.mp4)", $FD_MULTISELECT, "")

$aDirContent_New = StringSplit($sFileSelect, "|", 2)

_ArrayDisplay($aDirContent_New)

    For $i = UBound($aDirContent_New) - 1 To 0 Step - 1
        ConsoleWrite($i & "'" & $aDirContent_New[$i] & "'" & @CRLF)
        If StringRegExp($aDirContent_New[$i], "^\d{4}(-\d{2}){2}_\d{6}_[PV]\.(?:jpg|mp4)$") Then
            ConsoleWrite("> " & $i & @CRLF)
            _ArrayDelete($aDirContent_New, $i)
            ContinueLoop
        EndIf
        ;$aDirContent_New[$i] = $aDirContent_New[0] & "\" & $aDirContent_New[$i]
    Next
    
_ArrayDisplay($aDirContent_New)

 

Link to post
Share on other sites

Different approach

  • No UI
  • Using 2 cmd process runs to get a certain list of extensions (1 per run)
  • Build the output based on whats found with regex (using consolewrite)
  • Create the array with all files ending on jpg or mp4 
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3> ; Required for _ArrayDisplay only.

; Recursively display a list of files in a directory.
Example()

Func Example()
    Local $sFilePath = @ScriptDir ; Search the current script directory.
    Local $sFilter[2] = ["*.jpg","*.mp4"]
    Local $iPid[2], $sOut[2]

    ; If the file path isn't a directory then return from the 'Example' function.
    If Not StringInStr(FileGetAttrib($sFilePath), "D") Then
        Return SetError(1, 0, 0)
    EndIf

    ; Remove trailing backslashes and append a single trailing backslash.
    $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+\z", "") & "\"

    #cs
        Commandline parameters for DIR:
        /B - Simple output.
        /A-D - Search for all files, minus folders.
        /S - Search within subfolders.
    #ce
    $iPID[0] = Run(@ComSpec & ' /C DIR "' & $sFilePath & $sFilter[0] & '" /B /A-D /S', $sFilePath, @SW_HIDE, $STDOUT_CHILD +  $STDERR_CHILD)
    $iPID[1] = Run(@ComSpec & ' /C DIR "' & $sFilePath & $sFilter[1] & '" /B /A-D /S', $sFilePath, @SW_HIDE, $STDOUT_CHILD +  $STDERR_CHILD)
    ; If you want to search with files that contains unicode characters, then use the /U commandline parameter.
    ; Wait until the process has closed using the PID returned by Run.
    local $retVal=ProcessWaitClose($iPID[1])
    $retVal=ProcessWaitClose($iPID[0])

    ; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead.
    $sOut[0] = StdoutRead($iPID[0])
    $sOut[1] = StdoutRead($iPID[1]) & @CRLF

    ;Output as 1 string
    local $sOutput=$sOut[0] & $sOut[1]
    ;consolewrite($sOutput)

    Local $aArray = StringRegExp($sOutput, '(\d{4}-\d{2}-\d{2}_\d{6}_[PV]\..{3})',$STR_REGEXPARRAYGLOBALMATCH)

    For $i = 0 To UBound($aArray) - 1
        consolewrite($i & " : " & $aArray[$i] & @CRLF)
    Next

    ; Use StringSplit to split the output of StdoutRead to an array. All carriage returns (@CR) are stripped and @CRLF (line feed) is used as the delimiter.
    Local $aArray = StringSplit(stringstripws($sOutput,    $STR_STRIPTRAILING ), @crLF,$STR_ENTIRESPLIT)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "It appears there was an error trying to find all the files in the current script directory.")
    Else

        ; Display the results.
        _ArrayDisplay($aArray)
    EndIf
EndFunc   ;==>Example

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...