Jump to content

from select 1 file, to select all files

Recommended Posts

i have a script that selects  a text file and deletes a line (text input required) in the text file selected

i have to make it remove all lines found on a file i name, toRemoveLines.txt

it has to remove lines from all text files found in a folder

this is the script that has to be modified

where it says "select file" it has to be "select folder"

where it says "line text input" it has to be all lines from a text file

#Include <File.au3>
Global $success = False
$file_name = FileOpenDialog("Select file", @ScriptDir, "All files (*.*)", 1+4)
$line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text")
$file_count_lines = _FileCountLines($file_name)
for $i = 0 to $file_count_lines
    $Lines_text_output = FileReadLine($file_name, $i)
    if StringInStr($Lines_text_output, $line_text_input) then
        _FileWriteToLine($file_name, $i, "", 1)
        $success = True
if $success = True Then
    MsgBox(0, "Success", "Line has been deleted")
    MsgBox(0, "Failure", "Line wasn't found")


Share this post

Link to post
Share on other sites

@vin1 That is a great assessment of what you need - but please understand that this is not a forum where you put in an order and someone barfs up code for you. What you haven't provided us with is what you have tried on your own, and what trouble you are running into. Please do so, so we can assist in showing you how to improve your script.

Share this post

Link to post
Share on other sites

Not sure if this is what you meant, I've included two examples, just uncomment out one or the other, if you want to just replace lines with "" then use _Example1() or if you want to remove the lines then use _Example2().

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

Local $aSearchText ;~ Search Items
    _FileReadToArray(@ScriptDir & "\SearchText.txt", $aSearchText)
    If @error Then Exit MsgBox(4096, "Remove List", "Error: Reading SearchText.txt")

;~ Select Folder Path
Global $sFolderPath = FileSelectFolder("Select folder", @ScriptDir)
    If @error Then Exit MsgBox(4096, "Select Folder", "Error: No folder selected.")

;~ Folder Path Txt files to array
Global $aFileList = _FileListToArrayRec($sFolderPath, "*.txt", 1, 0, 0, 2)
    If @error Then Exit MsgBox(4096, "Folder List", "Error: Creating FileList.")

;~ _Example1()

;~ Overwrites lines with blank "" text, does not remove lines.
Func _Example1()
    Local $iLineCount
    ;~ Loop through each File Name
    For $i = 1 To $aFileList[0]
        $iLineCount = _FileCountLines($aFileList[$i])
        ;~ Loop through each file
        For $j = 0 To $iLineCount
            ;~ Loop through Search Text items
            For $k = 1 To $aSearchText[0]
                If StringInStr(FileReadLine($aFileList[$i], $j), $aSearchText[$k]) Then
                    ;~ Overwrite line data
                    _FileWriteToLine($aFileList[$i], $j, "", 1)

;~ Exampe2 Removes Lines from file
Func _Example2()
    Local $aFileRead, $aFindText, $hFileOpen, $bFindText = False
    ;~ Loop through each File Name
    For $i = 1 To $aFileList[0]
        _FileReadToArray($aFileList[$i], $aFileRead)
        If @error Then
            MsgBox(4096, "File Read to Array", "Error: Reading: " & $aFileList[$i] & " & to an Array")
        ;~ Loop through Search Text Array
        For $j = 1 To $aSearchText[0]
            ;~ Find all instances of Search Text item
            $aFindText = _ArrayFindAll($aFileRead, $aSearchText[$j])
            If @error Then ContinueLoop
            ;~ Loop through array and remove lines from the array
            For $k = UBound($aFindText) - 1 To 0 Step - 1
                _ArrayDelete($aFileRead, $aFindText[$k])
                $bFindText = True
        ;~ If replacements were made overwrite the file with the updated array data.
        If $bFindText Then
            $hFileOpen = FileOpen($aFileList[$i], 2)
            _FileWriteFromArray($hFileOpen, $aFileRead, 1)
            $bFindText = False


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

  • Similar Content

    • By Luigi
      I need write a command line to run an .exe with parameters, one this parameters is a password with special characters  like:   [ ] \ "  etc.
      The password is write between double cotes too.
      When run this command line, catch error.
      I buid this function, sometimes run ok, others show error.

      Someone can any idea?

      Best ragards
      #include <Array.au3> Global $G_AIX_DEFAULT_PASSWORD = "******" Func Scape_Word_To_MSDOS($var = "") ;~ https://www.robvanderwoude.com/escapechars.php Local $aCharacters[][2] = [ _ ["%", "%%"], _ ["^", "^^"], _ ["&", "^&"], _ ["<", "^<"], _ [">", "^>"], _ ["|", "^|"], _ ["'", "^'"], _ ["`", "^`"], _ [",", "^,"], _ [";", "^;"], _ ["=", "^="], _ ["(", "^("], _ [")", "^)"], _ ["!", "^^!"], _ ["\", "\\"], _ ["[", "\["], _ ["]", "\]"], _ ['"', '\"""'], _ [".", "\."], _ ["*", "\*"], _ ["?", "\?"] _ ] Local $iSearch $var = StringSplit($var, "", $STR_NOCOUNT) Local $iSize = UBound($var, $UBOUND_ROWS) - 1 For $ii = 0 To $iSize $iSearch = _ArraySearch($aCharacters, $var[$ii]) If $iSearch >= 0 Then $var[$ii] = $aCharacters[$iSearch][1] Next Return _ArrayToString($var, "") EndFunc ;==>Scape_Word_To_MSDOS Local $password = "-Dk5iFB2UjOt[-x|""" & "" Local $password_scape = Scape_Word_To_MSDOS($password) ConsoleWrite("[" & $password & "]" & @LF) ConsoleWrite("[" & $password_scape & "]" & @LF)  
    • By TheSaint
      TeraCopy Timer - A program of mine, front end really, that I have been working on (on & off) for some time, but not shared here before, that I recall. Third party program TeraCopy is required.
      Many of you will be familiar with TeraCopy, and it has been discussed here at AF on several occasions, in various sub forums, including Chat.
      BE ADVISED - I still use an older version of TeraCopy (v2.27), a goody but an oldy, so have only tested my program with that. It may work with the newer v3.xx, or may be easily adjusted to do so, but I have not yet gone that route. Likewise, I only use it with Win 7 and Win XP, but I don't imagine there should be any issues with newer Windows. Add to that, my program being devised and created in AutoIt v3.3.0.0.
      So what is TeraCopy Timer and why might you need it?
      Those familiar with TeraCopy use, know you can run multiple copy or move jobs either simultaneously or concurrently (one immediately after another has finished). With the free version at least, you have no control over precise order or timing, and no way to deal (without manual interaction) with the pesky Thumbs.db files in Win XP. TeraCopy Timer allows you to do all those things, and in a batch fashion.
      Some screenshots and brief explanation
      Main Window
      Minimalist Window
      This new WAIT feature is the simplest and best way to use the program generally (in my view) ... but check out the following, as it is not always the case.
      Advanced Delay Window
      WARNING - It should go without saying, that you take all the usual precautions. For instance, don't use move with the only copy you have of precious files. BACKUP BACKUP BACKUP! Don't hold me liable for anything, except for being a nice guy who likes to share.
      TeraCopy Timer v3.1.zip  (see detail at Post #37)
      P.S. I am not affiliated in any way with those who created and provide the excellent third party program - TeraCopy.
    • By _leo_
      Hey there!
      I am having a problem with clicking a button in IE. As you can see in the code, there would be a class name to click it, but since this button exists several times with exactly the same code, I can only use the "Noch nicht veröffentlicht" to separate it from the others. But this part is just pure text without any classification. 
      "<div class="checkmark" data-v-1e5b1f70=""><svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 16 16" data-v-1e5b1f70=""><circle cx="8" cy="8" r="8" data-v-1e5b1f70="" /></svg></div>Noch nicht veröffentlicht"
      I was trying to click it with:
      #include <IE.au3> local $oNNVs = _IEFormGetCollection($oIE2) For $oNNV in $oNNVs If String($oNNV.innertext) = "Noch nicht veröffentlicht" Then _IEAction($oNNV, "click") EndIf Next and:
      $inputs = $oIE2.document.getElementsByTagName("div") For $input In $inputs If $input.innertext == "Noch nicht veröffentlicht" then $input.click() EndIf Next But nothing seems to work. Thanks for the help!
    • By GillesMaisonneuve
      Good morning,
      I am trying to align a two-line text in a button so that it would be centered in the button but can't succeed to do it for the vertical part.
      Can someone tell me what I do wrong ?
      Here is my code:
      $btn3 = GUICtrlCreateButton("Execute " & @LF & " file 1", 100, 65, 60, 50, _ BitOr($BS_CENTER,$BS_BOTTOM,$BS_MULTILINE)) and here is the resulting layout I get in my window.
      I tried to follow the help, made a lot of different parameter combinations (here it's `$BS_BOTTOM`, but I tried `$BS_VCENTER` and `$BS_TOP`), still get the same result : vertical alignement is a no go for me.
      Can someone help ?

    • By dascondor
      So basically need to pull text from a .txt file or .sql and use that text to replace something within a different file.
      First File:
      Text1 = jumps
      Text2 = fence
      Second File:
      My Dog XXXXX over the YYYYY.
      After Script:
      My Dog jumps over the fence
      I have found a good start, I'm basic programmer at best, I have some experience but this gets out of my wheel house.
      Any help is appreciated.
  • Create New...