D4rk^S0ul Posted March 12, 2007 Posted March 12, 2007 I was wondering if it was possible to use filewrite to delete something in a file i only need it to delete the last writen letter space or number atm but is that possible?
Traddles Posted March 12, 2007 Posted March 12, 2007 sure, there is probably a better way, but you could do this: Open the file in read mode and read in the last line. figure out which character you want to delete and remove it from the variable that holds this line now open the file in write mode (I'm not sure if you want append or overwrite, I think overwrite is what you want) then do FileWriteLine() to the last line. does this make sense? English is my second language..... there was no first language, but english is my second
D4rk^S0ul Posted March 12, 2007 Author Posted March 12, 2007 The logic is there, but the problem i am having is that i am not sure how to find the last letter writen in the file or the line is.
Traddles Posted March 12, 2007 Posted March 12, 2007 The logic is there, but the problem i am having is that i am not sure how to find the last letter writen in the file or the line is. #include <File.au3> $sFilePath = "test.txt" $lastLine = _FileCountLines( $sFilePath ) $line = FileReadLine($sFilePath, $lastLine) $newLine = StringTrimRight($line, 1) _FileWriteToLine($sFilePath, $lastLine, $newLine, 1) though when I run that code for some reason it seems to insert a newline at the end of the last line.... no characters but it does append a new empty line. hope this helps
D4rk^S0ul Posted March 12, 2007 Author Posted March 12, 2007 Thanks for the help, does anyone know how to fix the problem with the extra line? I looked through it but i cant see why this is happening, because i am using FileWrite command to alter the file, when i write after editing it, it types on the new line.
Moderators SmOke_N Posted March 12, 2007 Moderators Posted March 12, 2007 Is it just an empty line? If so, StringTrimRight($value, 2) would get rid of a @CRLF at the end. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
D4rk^S0ul Posted March 12, 2007 Author Posted March 12, 2007 (edited) I thought of something like that but what i did had no effect: #include <File.au3> $sFilePath = "test.txt" $lastLine = _FileCountLines( $sFilePath ) $line = FileReadLine($sFilePath, $lastLine) $newLine = StringTrimRight($line, 1) _FileWriteToLine($sFilePath, $lastLine, $newLine, 1) $lastLine = _FileCountLines( $sFilePath ) $line = FileReadLine($sFilePath, $lastLine) $newLine = StringTrimRight($line, 1) Im not sure if this was what you were on about. Edited March 12, 2007 by D4rk^S0ul
Moderators SmOke_N Posted March 12, 2007 Moderators Posted March 12, 2007 (edited) $newLine = StringTrimRight($line, 2) << if you are trimming right 1 of a Carriage Return + Line Feed ie... @CRLF, you are only getting rid of the LineFeed, by using "2" you get rid of both. As you may not know what I'm "on" about, I as well haven't payed as much attention to your post as I should to know your exact issue, I took a stab in the dark that it was writing a blank line, and if it is, then taking that stab would fix your issue . Edited March 12, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
D4rk^S0ul Posted March 12, 2007 Author Posted March 12, 2007 I have also tried deleting 2 from the line but it made no difference the extra line still exists even if i try it out on my own made file with a new blank line in it.
Moderators SmOke_N Posted March 12, 2007 Moderators Posted March 12, 2007 I think I understand what you are saying... #include <File.au3> $sFilePath = @DesktopDir & "\txt.txt" $lastLine = UBound(StringSplit(StringStripCR(FileRead($sFilePath)), @LF)) - 1 $line = FileReadLine($sFilePath, $lastLine) _FileWriteToLineDE($sFilePath, $lastLine, $line, 1) ConsoleWrite(@LF & 'Number of Lines Before: ' & $lastLine & @LF & _ 'Number of Lines After: ' & UBound(StringSplit(StringStripCR(FileRead($sFilePath)), @LF)) - 1 & @LF) Func _FileWriteToLineDE($sFile, $iLine, $sText, $fOverWrite = 0) _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite) Local $sReadFile = FileRead($sFile) Local $nCount = UBound(StringSplit(StringStripCR($sReadFile), @LF)) - 1 If FileReadLine($sFile, $nCount) = '' Then FileClose(FileOpen($sFile, 2)) FileWrite($sFile, StringTrimRight($sReadFile, 2)) EndIf Return EndFuncAnd it's what I thought was happening. FileWriteLine does insert a Carriage Return + LineFeed to the end of the file. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
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