Jump to content

Delete all text in a txt file


damon
 Share

Go to solution Solved by damon,

Recommended Posts

I have been digging and can not find a way to delete the text in a file. 

i tried this

This one is creating a blank line and causing script to crash

FileClose($binFile)

                FileOpen($binFile,2)

I also tried

this one i get the error that the file does not get created, i think it never gets deleted as i can see it still in the directory with 2KB of data instead of 0.

FileClose($binFile)

                $binFileDel = FileDelete($binFile)
                If $binFileDel = 0 Then  ;;0 = Error
                    MsgBox (0, "ERROR", "Error Deleting" & $binFile & @CRLF & "Exiting Program" )
                    Exit
                EndIf

                $binFileCreate = _FileCreate($binFile)
                If $binFileCreate <> 1 Then  ;;0 = Error
                    MsgBox (0, "ERROR", "Error Creating" & $binFile & @CRLF & "Exiting Program" )
                    Exit
                EndIf
                
                FileOpen($binFile,2)

and a few other veriations, any ideas would be awesome.

 

my main objective is to clear the $binFile so that i can write more data to it.  if there is another way i am up for trying it. 

This is being processed from a log file of approx. 56000 lines and every 10 to 30 lines it will need to erase this file back to a blank slate.

It always amazes me how one little thing can cause so much havoc

Link to comment
Share on other sites

  • Moderators

damon,

Works every time for me. Are you mixing up handles and filenames in your code? :huh:

#include <String.au3>
#include <Constants.au3>

$sFile_Name = @ScriptDir & "\Test.txt"

$hFile = FileOpen(@ScriptDir & "\Test.txt", 2) ; Get handle <<<<<<<<<<<<<<<<<<
FileWrite($hFile, _StringRepeat("*", 2000))    ; Use handle <<<<<<<<<<<<<<<<<<
FileClose($hFile)                              ; Use handle <<<<<<<<<<<<<<<<<<

MsgBox($MB_SYSTEMMODAL, "File Size", FileGetSize($sFile_Name))

FileDelete($sFile_Name)                        ; Use filename <<<<<<<<<<<<<<<<
MsgBox($MB_SYSTEMMODAL, "Is File Deleted?", FileExists($sFile_Name))

$hFile = FileOpen(@ScriptDir & "\Test.txt", 2)
FileWrite($hFile, _StringRepeat("*", 2000))
FileClose($hFile)

MsgBox($MB_SYSTEMMODAL, "File Size", FileGetSize($sFile_Name))

FileDelete($sFile_Name)
MsgBox($MB_SYSTEMMODAL, "Is File Deleted?", FileExists($sFile_Name))
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

below is the entire code, i am getting the same, i know its something in the code that i am missing

#include <file.au3>
#include <date.au3>
#include <Constants.au3>

;;common variables
Global $file = @ScriptDir & "\dingservice.log"
Global $binFile = @ScriptDir & "\bin.txt"
Global $CycleFile = @ScriptDir & "\CycleFile.txt"
Global $line
Global $outboxID = "000000000"
Global $count = 1 ;;sets line count
Global $aTemp, $sHour, $sMinute, $sSecond

;;Counts how many lines in the file
$lineCount = _FileCountLines($file)
$lineCount = $lineCount + 1

;;Opens file
$fileOpen = FileOpen($file, 0)
If $fileOpen = -1 Then  ;; -1 = Error
    MsgBox (0, "ERROR", "Unable to open file." & @CRLF & "Exiting Program")
    Exit
EndIf

;;Open Bin file
$openBinFile = FileOpen($binFile,2)
If $openBinFile = -1 Then  ;;-1 = Error
    MsgBox (0, "ERROR", "Error opening working file" & @CRLF & "Exiting Program" )
    Exit
EndIf

;;Open Cycle file
$openCycleFile = FileOpen($CycleFile,2)
If $openCycleFile = -1 Then  ;;-1 = Error
    MsgBox (0, "ERROR", "Error opening working file" & @CRLF & "Exiting Program" )
    Exit
EndIf

ProgressOn("Creating Report","Please Wait.","" ,-1,-1,16)
$progressSet = 100 / $lineCount
$currentProgress = $progressSet
sleep(1000)
;;processes the report
Do
    ;;parse for ERR or INF
$lineRead = FileReadLine($file, $count)
$stringsplit1 = StringLeft($lineRead,91)
$stringsplit2 = StringRight($stringsplit1, 5)

    If $stringsplit2 = "Sendi" Then
        FileWriteLine($openBinFile, $lineRead)
        ;;parse for current 9 digits
        $outboxID = StringRight($lineRead,9)
        $countAhead = $count + 1
        $lineReadAhead = FileReadLine ($file, $countAhead)
        ;;Parse for 9 Digit number and compare it to the previous for <>
        $outboxID2 = StringRight($lineReadAhead, 9)
        $stringCompare = StringCompare($outboxID2, $outboxID,2)
        ;;  -1 = greater than number before      1 = less than   0 = equal t
        If $stringCompare = -1 Then
            CycleTime()
            ;$binFileRead = FileRead($binFile)


                FileClose($binFile)

                FileDelete($binFile)
                ;$binFileDel =
#cs             If $binFileDel = 0 Then  ;;0 = Error
                    MsgBox (0, "ERROR", "Error Deleting" & $binFile & @CRLF & "Exiting Program" )
                    Exit
                EndIf


                $binFileCreate = _FileCreate($binFile)
                If $binFileCreate <> 1 Then  ;;0 = Error
                    MsgBox (0, "ERROR", "Error Creating" & $binFile & @CRLF & "Exiting Program" )
                    Exit
                EndIf
#ce
                FileOpen($binFile,2)
                ;FileSetPos($binFile,-1,0)


        EndIf




    EndIf

$currentProgress = $currentProgress + $progressSet
ProgressSet($currentProgress, "Lime " & $count & " of " & $lineCount)
$count = $count + 1

Until $count = $lineCount

FileClose($fileOpen)
FileClose($binFile)
FileClose($CycleFile)
ProgressOff()

MsgBox (0, "Complete", "INF or ERR = " & $stringsplit2 & @CRLF & "Line Read " & $lineRead & @CRLF & "split1 = " & $stringsplit1 & @CRLF & "split2 = " & $stringsplit2)
Exit


Func CycleTime()
    $cycleCount = _FileCountLines($binFile)
    $cycleLineRead = FileReadLine ($binFile, 1)
    $sTime1 = StringLeft($cycleLineRead,19)
    $sTime1 = StringRight($sTime1, 8)

    $cycleLineRead2 = FileReadLine ($binFile, $cycleCount)
    $sTime2 = StringLeft($cycleLineRead2,19)
    $sTime2 = StringRight($sTime2, 8)

    $aTemp = StringSplit($sTime1, ":")
    $iTime1 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
    $aTemp = StringSplit($sTime2, ":")
    $iTime2 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
    _TicksToTime($iTime2-$iTime1, $sHour, $sMinute, $sSecond)
    FileWriteLine($openCycleFile,"Cycle " & $count & " " & $sHour & ":" & $sMinute & ":" & $sSecond)


EndFunc

file it scans:

dingService.log

2013-08-15 13:20:34,894 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 502500104
2013-08-15 13:20:36,534 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 511100055
2013-08-15 13:20:38,300 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 511200054
2013-08-15 13:20:40,519 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 820300082
2013-08-15 13:20:42,237 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 851100014
2013-08-15 13:20:59,129 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 340200130
2013-08-15 13:20:59,847 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 501200068
2013-08-15 13:21:01,363 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 701500132
2013-08-15 13:21:02,754 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 706100016
2013-08-15 13:21:06,988 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 720200101
2013-08-15 13:21:24,020 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 122200030
2013-08-15 13:21:25,223 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 303200062
2013-08-15 13:21:26,176 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 310100101
2013-08-15 13:21:27,801 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 720100102
2013-08-15 13:21:29,911 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 740300093
2013-08-15 13:21:31,504 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 801400093
2013-08-15 13:21:33,083 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 820200084
2013-08-15 13:21:49,677 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 104100070
2013-08-15 13:21:50,724 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 310100093
2013-08-15 13:21:52,145 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 310200100
2013-08-15 13:21:52,849 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 330100096
2013-08-15 13:21:54,161 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 520200049
2013-08-15 13:21:55,364 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 530100026
2013-08-15 13:21:56,521 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 570100013
2013-08-15 13:21:57,614 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 620100053
2013-08-15 13:22:00,005 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 720200104
2013-08-15 13:22:01,646 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 820300083
2013-08-15 13:22:02,927 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 820400085
2013-08-15 13:22:05,255 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 840200070
2013-08-15 13:22:22,131 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 103100035
2013-08-15 13:22:24,115 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 111100038
2013-08-15 13:22:25,443 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 123200076
2013-08-15 13:22:27,100 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 302100096
2013-08-15 13:22:29,381 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 302400095
2013-08-15 13:22:30,662 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 340300131
2013-08-15 13:22:31,412 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 360200101
2013-08-15 13:22:32,412 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 410200091
2013-08-15 13:22:35,022 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 410300092
2013-08-15 13:22:36,084 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 501100064
2013-08-15 13:22:37,772 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 601100068
2013-08-15 13:22:39,365 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 601200067
2013-08-15 13:22:40,444 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 620200047
2013-08-15 13:22:41,381 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 701100133
2013-08-15 13:22:42,756 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 701100136
2013-08-15 13:22:43,397 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 701300134
2013-08-15 13:22:44,537 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 704100016
2013-08-15 13:22:45,647 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 720400105
2013-08-15 13:22:48,194 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 741200078
2013-08-15 13:23:05,491 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 103100034
2013-08-15 13:23:06,960 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 150100016
2013-08-15 13:23:08,085 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 221200023
2013-08-15 13:23:09,210 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 301300129
2013-08-15 13:23:10,585 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 301300130
2013-08-15 13:23:11,632 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 302200097
2013-08-15 13:23:12,648 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 340200132
2013-08-15 13:23:13,866 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 360200095
2013-08-15 13:23:14,257 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 451100010
2013-08-15 13:23:15,601 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 610100027
2013-08-15 13:23:16,476 [6] INFO  L1ID.TN_DL_2011.FacadeServer.DingService [(null)] - Sending ding request for OutboxID: 720400103

 

the dingService.log is normally 50,000 plus lines

it will write the first cycle time but not the script then bombs out due to the blank line at the begining of the $binFile

the $binfile is only temp to determine the time passing between each cycle

It always amazes me how one little thing can cause so much havoc

Link to comment
Share on other sites

  • Solution

damon,

Works every time for me. Are you mixing up handles and filenames in your code? :huh:

 

you were right, i just found it. 

Line 74

FileClose($openBinFile)

 

I was using

FileClose($binFile)

this fixed it. 

 

thanks.

It always amazes me how one little thing can cause so much havoc

Link to comment
Share on other sites

  • Moderators

damon,

This modified version seems to work fine for me - does it for you? :)

#include <file.au3>
#include <date.au3>
#include <Constants.au3>

;;common variables
Global $file = @ScriptDir & "\dingservice.log"
Global $binFile = @ScriptDir & "\bin.txt"
Global $CycleFile = @ScriptDir & "\CycleFile.txt"
;Global $line
;Global $outboxID = "000000000"
;Global $count = 1 ;;sets line count

;;Read file into array
Global $aLines
_FileReadToArray($file, $aLines)

;;Counts how many lines in the file
$lineCount = $aLines[0] + 1

;;Open Bin file
$openBinFile = FileOpen($binFile, 2)
If $openBinFile = -1 Then ;;-1 = Error
    MsgBox(0, "ERROR", "Error opening working file" & @CRLF & "Exiting Program")
    Exit
EndIf

;;Open Cycle file
$openCycleFile = FileOpen($CycleFile, 2)
If $openCycleFile = -1 Then ;;-1 = Error
    MsgBox(0, "ERROR", "Error opening working file" & @CRLF & "Exiting Program")
    Exit
EndIf

ProgressOn("Creating Report", "Please Wait.", "", -1, -1, 16)
$progressSet = 100 / $lineCount
$currentProgress = $progressSet
Sleep(1000)

;;processes the report
For $iCount = 1 To $aLines[0] - 1
    ;;parse for ERR or INF
    $lineRead = $aLines[$iCount]
    $sCheck_Text = StringMid($lineRead, 87, 5)
    ; If
    If StringInStr($lineRead, $sCheck_Text) Then
        FileWriteLine($openBinFile, $lineRead)
        ;;parse for current 9 digits
        $outboxID = StringRight($lineRead, 9)
        $lineReadAhead = $aLines[$iCount + 1]
        ;;Parse for 9 Digit number and compare it to the previous for <>
        $outboxID2 = StringRight($lineReadAhead, 9)
        $stringCompare = StringCompare($outboxID2, $outboxID, 2)
        ;;  -1 = greater than number before      1 = less than   0 = equal t
        If $stringCompare = -1 Then
            CycleTime($iCount)
            ; Close and reopen the file
            FileClose($openBinFile)
            $openBinFile = FileOpen($binFile, 2)
        EndIf
    EndIf

    $currentProgress = $currentProgress + $progressSet
    ProgressSet($currentProgress, "Lime " & $iCount & " of " & $lineCount)

Next

FileClose($openBinFile)
FileClose($openCycleFile)
ProgressOff()

;MsgBox(0, "Complete", "INF or ERR = " & $stringsplit2 & @CRLF & "Line Read " & $lineRead & @CRLF & "split1 = " & $stringsplit1 & @CRLF & "split2 = " & $stringsplit2)
Exit


Func CycleTime($count)

    Local $aTemp, $sHour, $sMinute, $sSecond

    $cycleCount = _FileCountLines($binFile)
    $cycleLineRead = FileReadLine($binFile, 1)
    $sTime1 = StringLeft($cycleLineRead, 19)
    $sTime1 = StringRight($sTime1, 8)

    $cycleLineRead2 = FileReadLine($binFile, $cycleCount)
    $sTime2 = StringLeft($cycleLineRead2, 19)
    $sTime2 = StringRight($sTime2, 8)

    $aTemp = StringSplit($sTime1, ":")
    $iTime1 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
    $aTemp = StringSplit($sTime2, ":")
    $iTime2 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
    _TicksToTime($iTime2 - $iTime1, $sHour, $sMinute, $sSecond)
    FileWriteLine($openCycleFile, "Cycle " & $count & " " & $sHour & ":" & $sMinute & ":" & $sSecond)

EndFunc   ;==>CycleTime
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...