dkwokgs Posted November 25, 2004 Share Posted November 25, 2004 Hi there, Two question. 1. How can I read only lines that I want into an array? eg. only wants to read starting with 10.151.10.10 IN FILES:- 10.151.10.10 www.corp.com 10.151.200.1 www.corp10.com 10.151.10.10 www.abcd.com 10.151.100.1 www.corp5.com 10.151.105.1 www.corp10.com 10.151.10.10 www.a123.com 2. How do I delete/replace lines (random) with another entries Best Regards Daniel Link to comment Share on other sites More sharing options...
SlimShady Posted November 25, 2004 Share Posted November 25, 2004 Dim $FoundLines $OpenedTextFile = FileOpen("yourfile.txt", 0) If $OpenedTextFile = -1 Then MsgBox(64, "Error!", "File could not be opened!") Exit EndIf While 1 $Line = FileReadLine($OpenedTextFile) If @error Then ExitLoop Else If StringInStr($Line, "10.151.10.10") Then $FoundLines = $FoundLines & $Line & @LF EndIf EndIf WEnd $FoundLines = StringTrimRight($FoundLines, 1) $FoundLinesArray = StringSplit($FoundLines, @LF) Link to comment Share on other sites More sharing options...
dkwokgs Posted November 25, 2004 Author Share Posted November 25, 2004 Thanks. But how to put back the alter string to the file replacing the previous Link to comment Share on other sites More sharing options...
SlimShady Posted November 25, 2004 Share Posted November 25, 2004 FileCopy("yourfile.txt", @TempDir & "\yourfile.txt") $OpenedTextFile = FileOpen("yourfile.txt", 2) $OpenedTempFile = FileOpen(@TempDir & "\yourfile.txt", 0) If $OpenedTempFile = -1 Then MsgBox(64, "Error!", "File could not be opened!") Exit EndIf While 1 $Line = FileReadLine($OpenedTempFile) If @error Then ExitLoop Else If StringInStr($Line, "10.151.10.10") Then FileWriteLine($OpenedTextFile, $Line) EndIf EndIf WEnd FileClose($OpenedTextFile) FileClose($OpenedTempFile) Link to comment Share on other sites More sharing options...
dkwokgs Posted November 26, 2004 Author Share Posted November 26, 2004 Sorry, I think u got me wrong. I want to replace the strings that I want with another strings but retain the rest. IN FILES:- 10.151.10.10 www.corp.com replace with 10.151.10.124 www.corp.com 10.151.200.1 www.corp10.com 10.151.10.10 www.abcd.com replace with 10.151.10.124 www.abcd.com 10.151.100.1 www.corp5.com 10.151.105.1 www.corp10.com 10.151.10.10 www.a123.com replace with 10.151.10.124 www.a123.com Link to comment Share on other sites More sharing options...
SlimShady Posted November 26, 2004 Share Posted November 26, 2004 Can't you be grateful for my time and try to finish the script yourself FileCopy("yourfile.txt", @TempDir & "\yourfile.txt") $OpenedTextFile = FileOpen("yourfile.txt", 2) $OpenedTempFile = FileOpen(@TempDir & "\yourfile.txt", 0) If $OpenedTempFile = -1 Then MsgBox(64, "Error!", "File could not be opened!") Exit EndIf While 1 $Line = FileReadLine($OpenedTempFile) If @error Then ExitLoop Else If StringInStr($Line, "10.151.10.10") Then $NewLine = StringReplace($Line, "10.151.10.10", "10.10.10.10") FileWriteLine($OpenedTextFile, $NewLine) EndIf EndIf WEnd FileClose($OpenedTextFile) FileClose($OpenedTempFile) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now