Jump to content

Edit A Text File?


Recommended Posts

so, I got the bug in my head that it'd be cool to be able to do a netsh dump (like this: netsh dump interface ip > dumpfile.nsh with comspec) and then make some changes to the file and dump it out to another file to be reintegrated with another netsh command... sounded great... I read the help file, tried to find how I might do it.. fileopen worked, I apparently had the file open, I couldn't figure out what I should do next... I stumbled about the helpfile some more, found _filereadtoarray (#include <file.au3>) but I can't seem to figure out how to define an array variable properly to recieve the information...

but I digress...

my real question isn't how to make filereadtoarray work, it's more "what's the best way to open a file, find specific text (in multiple instances), edit what follows that text, and save it?"

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

I like to put them all in one line, straight to an array.

I usually work with it line by line, but you can use it in an all in one variable as well.

$arrayvar = StringSplit(Stringreplace(FileRead("SomeFile.txt", FileGetSize("SomeFile.txt)),@lf,""),@cr)

Basically you read the entire file into a variable, remove the @lf of @crlf so you are left with only @cr, then you split the file by every @cr, and thus you have an array of each line. (stringSplit needs one and only one character, @CRLF is 2)

Look into StringTrimLeft, StringReplace, and StrinInStr() as suggested above. These can do wonders.

edit..

For large files, I would sugest a regular expression edit however.

edit2... Dang trids beat me to my own edit. But I prefer a good search and replace Regular Expessions, last time I downloaded GREP it was only search. Maybe I need to look into it again..

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

regular expressions? :D what's that?

as to the size of the file, it's 10k and 372 lines...

trying to read the file, and test that it's been read, nothing comes up in the msgbox:

$dumpfile = '"' & @ScriptDir & "\dump.nsh" & '"'
; get a dump

RunWait(@ComSpec & " /c " & 'netsh dump interface ip > ' & $dumpfile)
$dumpread = FileRead($dumpfile, FileGetSize($dumpfile))

MsgBox(0,"is there anything in there?",$dumpread)

next I'd try to figure out the string searches and replaces... god help me there...

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

ok, figured that part out, it's the way I'd defined $dumpfile with the quotes built in, I changed it as follows:

$dumpfile = @ScriptDir & "\dump.nsh"
RunWait(@ComSpec & " /c " & 'netsh dump interface ip > "' & $dumpfile & '"')
and the msgox now hold the contents of dump.nsh... now on to the fun part...

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

I think you're right Kitty, that link to grep doesn't seem to offer any replacement features :huh2:

But. This one does :lol:

@Emmanuel: Regular Expressions are only second to autoit when it comes to acquiring new superhuman powers. :D OK seriously. I guess you could say they are pattern recognition expressions. Google a bit and see how you appetite gets whetted :)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...