Jump to content

StringInStr doesn't match


Recommended Posts

Hey people.

 

I'm iterating over a list of files from all drives and want to know if it matches on a certain extension.

I'm getting a 0 (no match), where there clearly is a match.

 

For $i = 1 To $aFiles[0]

    $exceptionIndex = StringInStr($aFiles[$i], ".txt")
    If $exceptionIndex > 0 Then
        ;do stuff
    EndIf

Next

 

This is happening on a drive which is a VirtualBox mounted CD (this case the VirtualBox tools). It's read only.

I'm mentioning it because I had some strange behavior on VirtualBox mounted drives in the past (memory leaks when iterating over mounted VirtualBox shared folders).

Has anyone seen any similar issues? Have the same problem if I use StringRegExp.

Edited by kadanefewi
Link to comment
Share on other sites

How do you populate the $aFiles array?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I'd suggest you use the _FileListToArray or _FileListToArrayRec functions to search for the text files first.  This should give better performance by the fact that the array will only be populated with ".txt" files versus having non ".txt" files in the array and having to evaluate every file.  
 

#include <File.au3>

_Demo()

Func _Demo()
    $sSourcePath = @ScriptDir

    $aFiles = _FileListToArrayRec($sSourcePath, "*.txt",$FLTAR_FILES,$FLTAR_RECUR)
    If UBound($aFiles) <= 1 Then Return SetError(1, 0, 0)

    For $iX = 1 to $aFiles[0]
        ;Do something
        ConsoleWrite($aFiles[$iX] & @CRLF)
    Next
EndFunc



 

Edited by spudw2k
Link to comment
Share on other sites

58 minutes ago, water said:

How do you populate the $aFiles array?

Hey man! You always reply to my posts, thank you.

I was going to answer your question, but I started thinking about this... I have several conditions that might impact in whether the file is matched or not, not just this, it's another list.

The problem was that I have a list of conditions that I took from a .txt file and converted to a binary string (if that's a thing). This ".txt" is one of them.

I had several such lists, obtained from different strings. The problem was that some were formatted with Windows, other Linux EOL. I used @CR to populate the array from the string, because @CRLF failed with another array, so I didn't think much about it and just used it for all. So, basically I was telling autoit to match on ".txt" & @LF, that's why it wasn't finding the match. Thank you!

Edited by kadanefewi
Clarifications
Link to comment
Share on other sites

17 minutes ago, spudw2k said:

I'd suggest you use the _FileListToArray or _FileListToArrayRec functions to search for the text files first.  This should give better performance by the fact that the array will only be populated with ".txt" files versus having non ".txt" files in the array and having to evaluate every file.  
 

#include <File.au3>

_Demo()

Func _Demo()
    $sSourcePath = @ScriptDir

    $aFiles = _FileListToArrayRec($sSourcePath, "*.txt",$FLTAR_FILES,$FLTAR_RECUR)
    If UBound($aFiles) <= 1 Then Return SetError(1, 0, 0)

    For $iX = 1 to $aFiles[0]
        ;Do something
        ConsoleWrite($aFiles[$iX] & @CRLF)
    Next
EndFunc



 

Hey Spud, thanks.

Yes, I'm doing that, but this is a blacklist match, meaning I need to remove several extensions from a _FileListToArrayRec, to I had to come up way to filter on several conditions. See my other reply for what was the problem.

Link to comment
Share on other sites

Ah, another problem solved by asking the right questions, make the user think about it and letting him find the solution himself ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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