Jump to content

from select 1 file, to select all files


vin1
 Share

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

 

Link to comment
Share on other sites

  • Moderators

@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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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

 

Link to comment
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
 Share

×
×
  • Create New...