Jump to content
vin1

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
        ExitLoop
    EndIf
Next
if $success = True Then
    MsgBox(0, "Success", "Line has been deleted")
Else
    MsgBox(0, "Failure", "Line wasn't found")
EndIf

 

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()
_Example2()

;~ 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)
                EndIf
            Next
        Next
    Next
EndFunc

;~ 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")
            ContinueLoop
        EndIf
        ;~ 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
            Next
        Next
        ;~ If replacements were made overwrite the file with the updated array data.
        If $bFindText Then
            $hFileOpen = FileOpen($aFileList[$i], 2)
            _FileWriteFromArray($hFileOpen, $aFileRead, 1)
            FileClose($hFileOpen)
            $bFindText = False
        EndIf
    Next
EndFunc

 

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 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.5.zip  (MAJOR UPDATE - see detail at Post #39)
      Enjoy!
      OLDER DOWNLOADS
      P.S. I am not affiliated in any way with those who created and provide the excellent third party program - TeraCopy.
    • By Kiko745
      Hello!
      I am new to autoit so please if someone could help me. I am trying to combine multiple .txt files but I can't get it right. In every file are the exact amout of lines ( for example here are 2 ) ,            I don't want to make it for just 2 lines for each txt files but for all the lines the txt files contains. The thing I want to do is :
      Text File 1
      Hello my name
      I am a big
      ...
      Text File 2
      is Fred,
      Potato head for
      ...
      Text File 3
      and I like to eat pizza.
      Not getting this right.
      ...
      ------------------- Combine them all into one txt file like here ------------------------------
      Final Text file
      Hello my name is Fred, and I like to eat pizza.
      I am a big Potato head for Not getting this right.
      ...
       
      Thanks for any advice!
      P.S. Sorry for my English, not my first language.
       
    • By Kruxe
      Hello Everyone,
      I am very green when it comes to AutoIT, I have recently started using it to automate a job that i perform using MS Excel and that is working great. Recently I have been faced with a challenge I which i need to copy a specific set of lines from one .txt file and past them, overwriting a specific set of lines in a different .txt file. I honestly have no clue how I can copy the specified lines and transfer them. What functions / commands could I use and how could I lay them out I AutoIT? Any and all help will be appreciated. 
      Thanks!
      Kruxe
    • By Luigi
      Greetings,
      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 _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!
×
×
  • Create New...