Jump to content
Sign in to follow this  
chewy

open and edit text file

Recommended Posts

chewy

I am looking to open a specific text file and need to delete the first 2 characters in the file and then save the file. I am very new to this any help would be appreciated.

Thanks

Share this post


Link to post
Share on other sites
DirtyBanditos

I am looking to open a specific text file and need to delete the first 2 characters in the file and then save the file.  I am very new to this any help would be appreciated.

Thanks

<{POST_SNAPBACK}>

Hi look this tread for exampel and edit it for you) i hope that help you :idiot:

http://www.autoitscript.com/forum/index.php?showtopic=7022

Share this post


Link to post
Share on other sites
chewy

i tried this

Run('C:\Windows\Notepad.exe "C:\canastotahigh.txt" ')

WinActivate("CanastotaHigh.txt - Notepad")

Send("{DELETE}")

the problem is the window will not keep the focus

Share this post


Link to post
Share on other sites
DirtyBanditos

i tried this

Run('C:\Windows\Notepad.exe "C:\canastotahigh.txt" ')

WinActivate("CanastotaHigh.txt - Notepad")

Send("{DELETE}")

the problem is the window will not keep the focus

<{POST_SNAPBACK}>

Hi :D I use this code exampel edit it for you !Thats wörks great for me :idiot: i hope that help you!

;AutoItSetOption("MustDeclareVars", 1)

;AutoItSetOption("MouseCoordMode", 0)

;AutoItSetOption("PixelCoordMode", 0)

;AutoItSetOption("RunErrorsFatal", 1)

;AutoItSetOption("TrayIconDebug", 1)

;AutoItSetOption("WinTitleMatchMode", 4)

; ----------------------------------------------------------------------------

; Script Start

; ----------------------------------------------------------------------------

Run("C:\Programme\DirtyBanditos\Notepad.exe ", "", @SW_MAXIMIZE)

WinWait("Notepad.exe ")

$ln = StatusbarGetText("Notepad.exe ", "")

MsgBox(0, "Title", $ln & @CR & @error)

WinWait("Notepad.exe ")

Sleep(1000)

Send('{Enter}')

Edited by DirtyBanditos

Share this post


Link to post
Share on other sites
chewy

got it

Run('C:\Windows\Notepad.exe "C:\Documents and Settings\Administrator\Desktop\canastotaMiddle.txt" ')

WinWaitActive("CanastotaMiddle.txt - Notepad")

Send("{DELETE}")

Send("{DELETE}")

Send("!f")

Send("s")

WinClose("CanastotaMiddle.txt - Notepad", "")

i was trying too hard :idiot:

Share this post


Link to post
Share on other sites
DirtyBanditos

got it

Run('C:\Windows\Notepad.exe "C:\Documents and Settings\Administrator\Desktop\canastotaMiddle.txt" ')

WinWaitActive("CanastotaMiddle.txt - Notepad")

Send("{DELETE}")

Send("{DELETE}")

Send("!f")

Send("s")

WinClose("CanastotaMiddle.txt - Notepad", "")

i was trying too hard :idiot:

<{POST_SNAPBACK}>

:D good luky for happy coding! for good exampel use the helpchm.exe from your autoit Install.

Share this post


Link to post
Share on other sites
Coffee

There has to be or needs to be a better way to do this with StringTrimLeft on the fly without having to write a whole seperate text file.

Share this post


Link to post
Share on other sites
killaz219

There has to be or needs to be a better way to do this with StringTrimLeft on the fly without having to write a whole seperate text file.

<{POST_SNAPBACK}>

Maybe something like this?

$text = FileRead("file.txt", Filegetsize("file.txt"))
$text = StringTrimRight($text, 2)
FileDelete("file.txt")
FileWrite("file.txt", $text)

Share this post


Link to post
Share on other sites
Coffee

no no as this does not work.

Basicly a way to read in a line of text from file, edit it and overwrite the the full or partial line of that line in the text file.

as a hypothetical this would work:

$string = "this is a line of text"
$replace = "that is a cool on dood"
$text = StringReplace($string, 1, $replace, 1)
MsgBox(0, "Title", $text)

if the $replace string is longer than the $string however you get undesired results.

Not only this but if your going to edit a .txt file you have to rewrite your changes along with the entire old contents of the file.

It would be much easier to read in the line you want from a file be it line 1 or line 50, change it, and then overwrite the full OR partial line and only that/those lines in that file ONLY and then close it out rather than edit and rewrite the entire contents of the file.

Does this make sense?

Share this post


Link to post
Share on other sites
chewy

my answer is giving me problem because it is using wordpad instead of notepad on a 98 machine. any more suggestions on this, I cant seem to implement the giving suggestions

Share this post


Link to post
Share on other sites
Blue_Drache

(Parody of "Name that Tune")

I can write that code in 9 lines, Jack.

#include file.au3
Dim $file, $filearray, $x
$file = Fileopen(@scriptdir & "blah.txt",0)
_FileReadToArray($file,$filearray)
FileClose($file)
StringTrimLeft($filearray[1],2)
$file = FileOpen(@scriptdir & "blah.txt",2)
For $x = 1 to $filearray[0] step 1
    FileWriteLine($file,$filearray[$x])
Next

Of course, it takes a few assumptions, there's no error checking if the file never opens, and it may not be fast on large text files.

Basically, it's reading the text from the file to an array, performs the removal on the first 2 characters as specified, then writes it back to the same file, destroying the previous contents.

Edit: Changed String Trim operation from right to left. Oops. :lmao: Silly me.

Edit2: Corrected the FileOpen syntax. Forgot the "" and the @scriptdir tag. Basically, the @scriptdir tag means you have to run the exe from the same directory as the text file you want to modify.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites
phillip123adams

I am looking to open a specific text file and need to delete the first 2 characters in the file and then save the file.

The simplest and quickest way is to read the entire text file, remove the 1st two characters and re-write the entire text file (unless the file is huge).

; Adjust values accordingly
$sInFile = "c:\Test.txt"; Input file name
$sOutFile = "c:\Test.txt";Output file name )can be same or different than input file name)
$iRemoveChars = 2; number of characters to remove from 1st line.

_RemoveLeadingCharsFromFile($sInFile, $sOutFile, $iRemoveChars)

MsgBox(4096, "All Done!", "The first " &  $iRemoveChars & " characters have been removed from: " & @lf & $sInFile & @lf & @lf & "and written to:"  & @lf & $sOutFile)
Exit


Func _RemoveLeadingCharsFromFile($sInFile, $sOutFile, $iRemoveChars)
    Local $sAll, $hFile
    $sAll = FileRead($sInFile, FileGetSize($sInFile))
    $hFile = Fileopen($sOutFile, 2)
    FileWrite($hFile, stringmid($sAll, $iRemoveChars + 1))
    FileClose($hFile)
EndFunc

Phillip

Share this post


Link to post
Share on other sites
phillip123adams

the file is about 100K or so

<{POST_SNAPBACK}>

I just tried the script I suggested on a 104KB file, and it took 9.65 milliseconds to reconstruct the file without the first 2 characters on the first line.

Phillip

Share this post


Link to post
Share on other sites
chewy

i tried phillips response and get the following error

Posted Image

Share this post


Link to post
Share on other sites
chewy

you were missing the count parameter in the stringmid, works good now! thanks for the help!

Share this post


Link to post
Share on other sites
phillip123adams

you were missing the count parameter in the stringmid, works good now! thanks for the help!

<{POST_SNAPBACK}>

Glad you got it working. I think the syntax of the StringMid function was changed a month or so ago to make the count parameter optional. Are you using the latest version of AutoIt? Version 3.1.0.15 was released yesterday, and it is no longer beta. Get it while it's hot!

Phillip

Share this post


Link to post
Share on other sites
chewy

boy I spoke too soon, if I write it to a different filename than the original it is fine, but if the output file is the same as the input file the output file is blank

Share this post


Link to post
Share on other sites
phillip123adams

boy I spoke too soon, if I write it to a different filename than the original it is fine, but if the output file is the same as the input file the output file is blank

<{POST_SNAPBACK}>

I just tried it again with the input and output file being the same, and it works for me. What did you use for the "count" parameter?

Post your script and I'll check it out.


Phillip

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.