kadanefewi

StringInStr doesn't match

7 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



How do you populate the $aFiles array?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
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 ;)

2 people like this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


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