Sign in to follow this  
Followers 0
failedtocompile

Help with filtering multiple strings from file

8 posts in this topic

Hi guys,

I am currently using the below method which doesn't look that appealing to remove certain strings from a text file and then remove blank spaces.

$filelist = _FileListToArray('c:\Documents and Settings', "*", 2)
Sleep(1000)
_FileWriteFromArray('D:\test.txt', $filelist, 1)
Sleep(30)
$File = 'D:\test.txt'
$text = FileRead($File, FileGetSize($File))
$text = StringReplace($text, "Administrator", " ", 0, 0)
$text = StringReplace($text, "Default User", " ", 0, 0)
$text = StringReplace($text, "All Users", " ", 0, 0)
$text = StringReplace($text, "LocalService", " ", 0, 0)
$text = StringReplace($text, "NetworkService", " ", 0, 0)
$text = StringStripWS($text, 1)
FileDelete($File)
FileWrite($File, $text)

I have read on the forums that it could be done using Arrays (i think: http://www.autoitscript.com/forum/index.php?showtopic=111646) but I am a real novice with Arrays, anyone able to post a link on examples?

Thanks in advance

ftc

Share this post


Link to post
Share on other sites



Like this ?

Local $_Filter[6] = ["Administrator", "Default User", "All Users", "LocalService", "NetworkService"]
    For $_I = 0 To UBound ( $_Filter ) -1
        $text = StringReplace ( $text, $_Filter[$_I], ' ' )
    Next

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Try this.

Local $File, $text, $File

$File = 'D:\test.txt'
$text = FileRead($File)
$text = StringRegExpReplace($text, "(Administrator|Default User|All Users|LocalService|NetworkService)", "")
;$text = StringStripWS($text, 7) ; Strip leading, trailing, and double (or more) white space. 1+2+4 = 7
;ConsoleWrite($text & @CRLF)

$File = FileOpen($File, 2) ; 2 = Write mode (erase previous contents).
FileWrite($File, $text)
FileClose($File)

Share this post


Link to post
Share on other sites

@Malkey

Your solution is case sensitive ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Thanks allot to both of you guys,

Just learn few new things from your examples ;) .

Have a good weekend.

ftc

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@Malkey

Your solution is case sensitive ! Posted Image

wakillon

"(?i)" is the case-insensitivity flag.

If case insensitivity is required, then replace:-

$text = StringRegExpReplace($text, "(Administrator|Default User|All Users|LocalService|NetworkService)", "")

with:-

$text = StringRegExpReplace($text, "(?i)(Administrator|Default User|All Users|LocalService|NetworkService)", "")

Edit: Put codes in code boxes.

Edited by Malkey

Share this post


Link to post
Share on other sites

Thanks allot to both of you guys,

Just learn few new things from your examples ;) .

Have a good weekend.

ftc

Thanks !


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Another alternative might be to keep the unwanted entries out of your original array, rather than removing them later, by using an enhanced version of _FileListToArray(), one that has an "exclude list" parameter:

#include <Array.au3>
Local $sFilter = "Administrator;Default User;All Users;LocalService;NetworkService"

$array = _FileListToArray_Recursive("C:\Documents and Settings", "", "*", $sFilter, 2, 0, False)
_ArrayDisplay($array)



; #FUNCTION# ===========================================================================================
; Name:             _FileListToArray_Recursive
; Description:      Lists files and\or folders in specified path
; Syntax:           _FileListToArray_Recursive([$sPath, [$sExcludeFolderList, $sIncludeList, $sExcludeList, $iReturnType, $iReturnFormet, $bRecursive]
; Parameter(s):     $sPath = Base path of search (Required)
;                   $sExcludeFolderList = List of folders to exclude from search, semicolon delimited. Wildcards allowed. (Default = "")
;                   $sIncludeList = List of files to include in search, semicolon delimited. Wildcards allowed. (Default = "*")
;                   $sExcludeList = List of file/folder names to exclude from returned array, semicolon delimited. Wildcards allowed. (Default = "")
;                   $iReturnType = Include in search: 0 = Files and Folder, 1 = Files Only, 2 = Folders Only (Default = 0)
;                   $iReturnFormat = Returned element format: 0 = file/folder name only, 1 = relative path, 2 = full path (Default = 0)
;                   $bRecursive = Recursively search subfolders (Default = False)
; Return Value(s):  On success: A 0-based array containing matching folders/files
; Author(s):        Half the AutoIt Community (Thread 96952)
; ====================================================================================================
Func _FileListToArray_Recursive($sPath, $sExcludeFolderList = "", $sIncludeList = "*", $sExcludeList = "", $iReturnType = 0, $iReturnFormat = 0, $bRecursive = False)
    Local $sRet = "", $sReturnFormat = ""

    ; Edit include path (strip trailing slashes, and append single slash)
    $sPath = StringRegExpReplace($sPath, "[\\/]+\z", "") & "\"
    If Not FileExists($sPath) Then Return SetError(1, 1, "")

    ; Edit exclude folders list
    If $sExcludeFolderList Then
        ; Strip leading/trailing spaces and semi-colons, any adjacent semi-colons, and spaces surrounding semi-colons
        $sExcludeFolderList = StringRegExpReplace(StringRegExpReplace($sExcludeFolderList, "(\s*;\s*)+", ";"), "\A;|;\z", "")
        ; Convert to Regular Expression, step 1: Wrap brackets around . and $ (what other characters needed?)
        $sExcludeFolderList = StringRegExpReplace($sExcludeFolderList, '[.$]', '\[\0\]')
        ; Convert to Regular Expression, step 2: Convert '?' to '.', and '*' to '.*?'
        $sExcludeFolderList = StringReplace(StringReplace($sExcludeFolderList, "?", "."), "*", ".*?")
        ; Convert to Regular Expression, step 3; case-insensitive, convert ';' to '|', match from first char, terminate strings
        $sExcludeFolderList = "(?i)\A(?!" & StringReplace($sExcludeFolderList, ";", "$|")  & "$)"
    EndIf

    ; Edit include files list
    If $sIncludeList ="*" Then
        $sIncludeList = ""
    Else
        If StringRegExp($sIncludeList, "[\\/ :> <\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
        ; Strip leading/trailing spaces and semi-colons, any adjacent semi-colons, and spaces surrounding semi-colons
        $sIncludeList = StringRegExpReplace(StringRegExpReplace($sIncludeList, "(\s*;\s*)+", ";"), "\A;|;\z", "")
        ; Convert to Regular Expression, step 1: Wrap brackets around . and $ (what other characters needed?)
        $sIncludeList = StringRegExpReplace($sIncludeList, '[.$]', '\[\0\]')
        ; Convert to Regular Expression, step 2: Convert '?' to '.', and '*' to '.*?'
        $sIncludeList = StringReplace(StringReplace($sIncludeList, "?", "."), "*", ".*?")
        ; Convert to Regular Expression, step 3; case-insensitive, convert ';' to '|', match from first char, terminate strings
        $sIncludeList = "(?i)\A(" & StringReplace($sIncludeList, ";", "$|")  & "$)"
    EndIf

    ; Edit exclude files list
    If $sExcludeList Then
        ; Strip leading/trailing spaces and semi-colons, any adjacent semi-colons, and spaces surrounding semi-colons
        $sExcludeList = StringRegExpReplace(StringRegExpReplace($sExcludeList, "(\s*;\s*)+", ";"), "\A;|;\z", "")
        ; Convert to Regular Expression, step 1: Wrap brackets around . and $ (what other characters needed?)
        $sExcludeList = StringRegExpReplace($sExcludeList, '[.$]', '\[\0\]')
        ; Convert to Regular Expression, step 2: Convert '?' to '.', and '*' to '.*?'
        $sExcludeList = StringReplace(StringReplace($sExcludeList, "?", "."), "*", ".*?")
        ; Convert to Regular Expression, step 3; case-insensitive, convert ';' to '|', match from first char, terminate strings
        $sExcludeList = "(?i)\A(?!" & StringReplace($sExcludeList, ";", "$|")  & "$)"
    EndIf

;   MsgBox(1,"Masks","File include: " & $sIncludeList & @CRLF & "File exclude: " & $ExcludeList & @CRLF _
;           & "Dir include : " & $FolderInclude & @CRLF & "Dir exclude : " & $ExcludeFolderList)

    If Not ($iReturnType = 0 Or $iReturnType = 1 Or $iReturnType = 2) Then Return SetError(3, 3, "")

    Local $sOrigPathLen = StringLen($sPath), $aQueue[64] = [1,$sPath], $iQMax = 63
    While $aQueue[0]
        $WorkFolder = $aQueue[$aQueue[0]]
        $aQueue[0] -= 1
        $search = FileFindFirstFile($WorkFolder & "*")
        If @error Then ContinueLoop
        Switch $iReturnFormat
            Case 1 ; relative path
                $sReturnFormat = StringTrimLeft($WorkFolder, $sOrigPathLen)
            Case 2 ; full path
                $sReturnFormat = $WorkFolder
        EndSwitch
        While 1
            $file = FileFindNextFile($search)
            If @error Then ExitLoop
            If @extended Then ; Folder
                If $sExcludeFolderList And Not StringRegExp($file, $sExcludeFolderList) Then ContinueLoop
                If $bRecursive Then
                    If $aQueue[0] = $iQMax Then
                        $iQMax += 128
                        ReDim $aQueue[$iQMax + 1]
                    EndIf
                    $aQueue[0] += 1
                    $aQueue[$aQueue[0]] = $WorkFolder & $file & "\"
                EndIf
                If $iReturnType = 1 Then ContinueLoop
            Else ; File
                If $iReturnType = 2 Then ContinueLoop
            EndIf
            If $sIncludeList And Not StringRegExp($file, $sIncludeList) Then ContinueLoop
            If $sExcludeList And Not StringRegExp($file, $sExcludeList) Then ContinueLoop
            $sRet &= $sReturnFormat & $file & "|"
        WEnd
        FileClose($search)
    WEnd
    If Not $sRet Then Return SetError(4, 4, "")
    Return StringSplit(StringTrimRight($sRet, 1), "|")
EndFunc

edit: changed the udf's documentation

Edited by Spiff59

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