Sign in to follow this  
Followers 0
phew

string/array filter

7 posts in this topic

hi,

question again:

#include <File.au3>
#include <Array.au3>

_writeFolders()

Func _writeFolders()
    $Flst = _FileListToArray('C:\', '*', 2)
    For $h = 1 To $Flst[0]
Next
    $string = _ArrayToString($Flst, " ")
    $array = StringSplit($string, " ")
    $acc = FileOpen("result.txt", 2)
    $counter = $array[0] - 1
    If ($counter = 1) Then
    FileWriteLine($acc, "(" & $counter & " FOLDER FOUND)" & @CRLF & @CRLF)
    FileWriteLine($acc, "----[ACCOUNT]----")
    ElseIf (($counter <> 1) And ($counter <> 0)) Then
    FileWriteLine($acc, "(" & $counter & " FOLDERS FOUND)" & @CRLF & @CRLF)
    FileWriteLine($acc, "----[ACCOUNTS]----")
    Else
    FileWriteLine($acc, "(COULD NOT FIND ANY FOLDERS)" & @CRLF & @CRLF)
    EndIf
    For $i = $array[0] To 2 Step -1
    $no = $i - 1
    FileWriteLine($acc, "#" & $no & " " & $array[$i])
    Next
    FileClose($acc)
EndFunc

is working so far. but now i want to filter out the folders "WINDOWS" and "OTHER", so that if there is a folder named "WINDOWS" or "OTHER" on "C:\" it is not written to the "result.txt" - someone can give me a point in the right direction?

Share this post


Link to post
Share on other sites



#include <File.au3>
#include <Array.au3>

_writeFolders()

Func _writeFolders()
    $Flst = _FileListToArray('C:\', '*', 2)
;~   For $h = 1 To $Flst[0]
;~ Next
    $string = _ArrayToString($Flst, " ")
    $array = StringSplit($string, " ")
    $acc = FileOpen("result.txt", 2)
    $counter = $array[0] - 1
    If ($counter = 1) Then
    FileWriteLine($acc, "(" & $counter & " FOLDER FOUND)" & @CRLF & @CRLF)
    FileWriteLine($acc, "----[ACCOUNT]----")
    ElseIf (($counter <> 1) And ($counter <> 0)) Then
    FileWriteLine($acc, "(" & $counter & " FOLDERS FOUND)" & @CRLF & @CRLF)
    FileWriteLine($acc, "----[ACCOUNTS]----")
    Else
    FileWriteLine($acc, "(COULD NOT FIND ANY FOLDERS)" & @CRLF & @CRLF)
    EndIf
    For $i = $array[0] To 2 Step -1
    If  $array[$i]<>"WINDOWS" AND $array[$i]<>"OTHER" Then
    $no = $i - 1
    FileWriteLine($acc, "#" & $no & " " & $array[$i])
    EndIf
    Next
    FileClose($acc)
EndFunc

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Just test the string at $array[$i] before you write it to the file. Do a 'ContinueLoop' to skip the current entry and go on to the next without exiting your For/Next loop.

And your first For/Next loop at the top of the function is useless. It doesn't do anything.

:)

Edit: Nahuel beat me to it... ;)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

sometimes i'm thinking too complicated, would not have guessed that it's so easy.

thanks man :)

Share this post


Link to post
Share on other sites

It doesn't work right though.. this is how it writes the file:

(13 FOLDERS FOUND)

----[ACCOUNTS]----

#12 Information

#11 Volume

#10 System

#9 RECYCLER

#8 MSOCache

#7 MSDOS7

#6 Settings

#5 and

#4 Documents

#3 programa

#2 de

#1 Archivos

When it should be

(5 FOLDERS FOUND)

----[ACCOUNTS]----

#5 System Volume Information

#4 RECYCLER

#3 MSOCache

#2 MSDOS7

#1 Documents and Settings

Here's a fix, I suppose:

#include <File.au3>
#include <Array.au3>

_writeFolders()

Func _writeFolders()
    $Flst = _FileListToArray('C:\', '*', 2)
    $acc = FileOpen("result.txt", 2)
    $counter = $Flst[0]
    If ($counter = 1) Then
        FileWriteLine($acc, "(" & $counter & " FOLDER FOUND)" & @CRLF & @CRLF)
        FileWriteLine($acc, "----[ACCOUNT]----")
    ElseIf (($counter <> 1) And ($counter <> 0)) Then
        FileWriteLine($acc, "(" & $counter-1 & " FOLDERS FOUND)" & @CRLF & @CRLF)
        FileWriteLine($acc, "----[ACCOUNTS]----")
    Else
        FileWriteLine($acc, "(COULD NOT FIND ANY FOLDERS)" & @CRLF & @CRLF)
    EndIf
    For $i =$Flst[0] to 1  Step -1
        If  $Flst[$i]<>"WINDOWS" AND $Flst[$i]<>"OTHER" Then
            $no = $i
            FileWriteLine($acc, "#" & $no & " " & $Flst[$i])
        EndIf
    Next
    FileClose($acc)
EndFunc

Share this post


Link to post
Share on other sites

Nahuel got to it first but:

#include <File.au3>
#include <Array.au3>

_writeFolders()

Func _writeFolders()
    $Flst = _FileListToArray('C:\', '*', 2)
    
    $acc = FileOpen("result.txt", 2)
    
    If ($Flst[0] > 0) Then
        FileWriteLine($acc, "(" & $Flst[0] & " FOLDER(S) FOUND)" & @CRLF & @CRLF)
        FileWriteLine($acc, "----[ACCOUNT]----")
    Else
        FileWriteLine($acc, "(COULD NOT FIND ANY FOLDERS)" & @CRLF & @CRLF)
    EndIf
    
    For $h = 1 To $Flst[0]
        If $Flst[$h] <> "OTHER" AND $Flst[$h] <> "WINDOWS" Then
            FileWriteLine($acc, "#" & $h & " " & $Flst[$h])
        EndIf        
    Next
    FileClose($acc)
EndFunc

Share this post


Link to post
Share on other sites

Hi,

Try my FilelistToArray too, which has excludes, to get your array first? [link in sig]

#include<_FileListToArrayNew2h.au3>; _FileListToArray3($sPath, $sFilter = "*", $iFlag = 0, $iRecurse = 0, $iBaseDir = 1, $sExclude = "")
local $ar_Array = _FileListToArray3 ("c:", "*", 2, 0, 0,"Windows|Backup"),$c= _ArrayDisplay($ar_Array, "File List")
Best, Randall

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
Sign in to follow this  
Followers 0