Sign in to follow this  
Followers 0
dew

Removing lines from a txt file

6 posts in this topic

I am verifying that all lnks installed from a system image works, andI have a routine that gets all the lnk files on the system. Now I need to filter out the ones I don't need. Let's say I don't need any lnk files that has "AutoIt" or "Games" or "Administrative Tools" as an example.

What I would like to do is have a file that lists text to search for and remove that line. When finish I'll have only the ones I need to test.

I do not know how to go about doing this, so if anyone can show me I would really appreciate it very much.

Thanks,

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

@dew

_FileWriteToLine('yourfile.ext', 'line nb', '', 1)

Cheers, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

@dew

_File_WriteToLine('yourfile.ext', 'line nb', '', 1)

Cheers, FireFox.

Sorry, this does not do what I am looking for it depends on line numbers to change, not the string to remove.

thanks anyway

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hey,

maybe this works for you

#include <File.au3> ; _FileReadToArray

Global $sListFile = "linklist.txt"
Global $aLinks


;# how you get the lnk files into an array is up to you, thisa is just an example
_FileReadToArray($sListFile, $aLinks) ; make an array with all the lnk files
If Not IsArray($aLinks) Then Exit 1


Global $hFile = FileOpen($sListFile, 2)
If $hFile == -1 Then Exit 2

For $i = 1 To UBound($aLinks) - 1 ; loop trough the array
;~  Dim $szDrive, $szDir, $szFName, $szExt
;~  _PathSplit($aLinks[$i], $szDrive, $szDir, $szFName, $szExt) ; use $szFName to only match the name
    If Not StringRegExp($aLinks[$i], "(?i)(?:AutoIt|Games|Administrative\s+Tools)") Then ; no match with "AutoIt", "Games" or "Administrative Tools"
        FileWriteLine($hFile, $aLinks[$i]) ; write the line to the file
    EndIf   
Next
FileClose($hFile)oÝ÷ ØGbµ·¥£-±ç¦²'bv{_ºYr­æþéÝz»-¢r¢êê¹ë-í+(ì(®K0ØZ~)^¶åiÉ%Ëp¢·ljëh×6#include <File.au3> ; _FileReadToArray


Global $sListFile = "linklist.txt"
Global $aLinks
; read blacklist.txt to $sBlackList, this will be the words in .lnk files we DONT need seperated with a |
Global $aBlackList = StringSplit(StringStripWS(StringStripCR(FileRead("blacklist.txt")), 3), "|")

;# how you get the lnk files into an array is up to you, thisa is just an example
_FileReadToArray($sListFile, $aLinks) ; make an array with all the lnk files
If Not IsArray($aLinks) Then Exit 1

$hFile = FileOpen("linklist1.txt", 2)
If $hFile == -1 Then Exit 2

For $i = 1 To UBound($aLinks) - 1 ; loop trough the array
;~  Dim $szDrive, $szDir, $szFName, $szExt
;~  _PathSplit($aLinks[$i], $szDrive, $szDir, $szFName, $szExt) ; use $szFName instead of $aLinks[$i] to only match the name
    $fMatch = False
    For $j = 1 To UBound($aBlackList) - 1
        If StringInStr($aLinks[$i], $aBlackList[$j]) Then $fMatch = True ; write the line to the file   
    Next
    If Not $fMatch Then FileWriteLine($hFile, $aLinks[$i]) ; write the line to the file 
Next
FileClose($hFile)

;#### blacklist.txt
AutoIt|Games|Administrative Tools
;################################

;#### linklist.txt
C:\WINDOWS\some.lnk
C:\Administrative Tools\some.lnk
C:\WINDOWS\system\some.lnk
C:\Program Files\AutoIt\some.lnk
C:\Program Files\AutoIt\SciTE\some.lnk
C:\WINDOWS\system32\autoit.lnk
;################################

;#### OUTPUT
C:\WINDOWS\some.lnk
C:\WINDOWS\system\some.lnk
;################################
Edited by Robjong

Share this post


Link to post
Share on other sites

Maybe..

#include <file.au3>
#include <array.au3>

Dim $array

If Not _FileReadToArray("File_To_Read.txt", $array) Then
    MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
    Exit
EndIf

$Split = StringSplit("thislink.com,thatlink.com,info.com", ","); split the links

For $x = 1 To UBound($Split) - 1

    If _ArraySearch($array, $Split[$x]) Then _ArrayDelete($array, $Split[$x])

Next

_FileWriteFromArray("New_List.txt", $array)

***** NOT TESTED

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Hey,

maybe this works for you

#include <File.au3> ; _FileReadToArray

Global $sListFile = "linklist.txt"
Global $aLinks


;# how you get the lnk files into an array is up to you, thisa is just an example
_FileReadToArray($sListFile, $aLinks) ; make an array with all the lnk files
If Not IsArray($aLinks) Then Exit 1


Global $hFile = FileOpen($sListFile, 2)
If $hFile == -1 Then Exit 2

For $i = 1 To UBound($aLinks) - 1 ; loop trough the array
;~  Dim $szDrive, $szDir, $szFName, $szExt
;~  _PathSplit($aLinks[$i], $szDrive, $szDir, $szFName, $szExt) ; use $szFName to only match the name
    If Not StringRegExp($aLinks[$i], "(?i)(?:AutoIt|Games|Administrative\s+Tools)") Then ; no match with "AutoIt", "Games" or "Administrative Tools"
        FileWriteLine($hFile, $aLinks[$i]) ; write the line to the file
    EndIf   
Next
FileClose($hFile)oÝ÷ ØGbµ·¥£-±ç¦²'bv{_ºYr­æþéÝz»-¢r¢êê¹ë-í+(ì(®K0ØZ~)^¶åiÉ%Ëp¢·ljëh×6#include <File.au3> ; _FileReadToArray


Global $sListFile = "linklist.txt"
Global $aLinks
; read blacklist.txt to $sBlackList, this will be the words in .lnk files we DONT need seperated with a |
Global $aBlackList = StringSplit(StringStripWS(StringStripCR(FileRead("blacklist.txt")), 3), "|")

;# how you get the lnk files into an array is up to you, thisa is just an example
_FileReadToArray($sListFile, $aLinks) ; make an array with all the lnk files
If Not IsArray($aLinks) Then Exit 1

$hFile = FileOpen("linklist1.txt", 2)
If $hFile == -1 Then Exit 2

For $i = 1 To UBound($aLinks) - 1 ; loop trough the array
;~  Dim $szDrive, $szDir, $szFName, $szExt
;~  _PathSplit($aLinks[$i], $szDrive, $szDir, $szFName, $szExt) ; use $szFName instead of $aLinks[$i] to only match the name
    $fMatch = False
    For $j = 1 To UBound($aBlackList) - 1
        If StringInStr($aLinks[$i], $aBlackList[$j]) Then $fMatch = True ; write the line to the file   
    Next
    If Not $fMatch Then FileWriteLine($hFile, $aLinks[$i]) ; write the line to the file 
Next
FileClose($hFile)

;#### blacklist.txt
AutoIt|Games|Administrative Tools
;################################

;#### linklist.txt
C:\WINDOWS\some.lnk
C:\Administrative Tools\some.lnk
C:\WINDOWS\system\some.lnk
C:\Program Files\AutoIt\some.lnk
C:\Program Files\AutoIt\SciTE\some.lnk
C:\WINDOWS\system32\autoit.lnk
;################################

;#### OUTPUT
C:\WINDOWS\some.lnk
C:\WINDOWS\system\some.lnk
;################################
Thanks for your help - it work great.

-dave

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