Jump to content

Closing a Text File


Ace08
 Share

Recommended Posts

uhm guys, in my script i opened 2 text files but the problem is i can't get my 1st file to close...

here is a part of my script

;========================================================================== 
;For .Rep file
;==========================================================================
$RepFile = $ShortCode & stringright($YYYY,2) & Chr(64 + $MM) & $DD & $Fseq & ".REP"
    $RepPath = "C:\Script\Test\" & $TDate & "\" & $RepFile
    FileOpen($RepPath, 10)
    FileWriteLine($RepPath, "DBO")
    FileWriteLine($RepPath, "Date: " & _datetomonth($MM) & " " & $DD & "," & $YYYY)
    FileWriteLine($RepPath, "Summary List")
    FileWriteLine($RepPath, " ")
    FileWriteLine($RepPath, "Filename:" & $FileName)
    FileWriteLine($RepPath, " ")
    FileClose($RepPath)
    
    $ReadFile = FileOpen($UnFilePath, 0)
    If $ReadFile = -1 Then
        MsgBox(0, "Error", $FileName & "Unable to open file.")
        Exit
    EndIf
    While 1
        $BRSline = FileReadLine($ReadFile)
        If @error = -1 Then ExitLoop
        $SFile = stringSplit($BRSline ,chr(34) & "," & chr(34),1)
        $SRefNo = $SFile[1]
        $SRefNo = StringReplace($SRefNo, chr(34), "")
        $SRemName = $SFile[3]
        $SBeneName = $SFile[7]
        $SAmt = $SFile[19]
        $SCur = $SFile[18]
        $SAccNo = $SFile[17]
        $SBnkCode = $SFile[15]
        if $strcountPHP = "" then 
            $strcountPHP = "0" 
        endif
        if $strcountUSD = "" then 
            $strcountUSD = "0" 
        endif
        FileOpen($RepPath)
        FileWriteLine($RepPath, "RNo.       :" & $SRefNo)
        FileWriteLine($RepPath, "ReName     :" & $SReName)
        FileWriteLine($RepPath, "BeName :" & $SBeName)
        FileWriteLine($RepPath, "At         :" & $SAt)
        FileWriteLine($RepPath, "No.        :" & $SNo)
        FileWriteLine($RepPath, "BaName     :" & $SBCode)
        FileWriteLine($RepPath, " ")
        FileClose($RepPath)
    WEnd
    FileClose($ReadFile)
    msgbox(0,"", FileClose($ReadFile))
    if $TotAmtPHP = "" then 
        $TotAmtPHP = "0" 
    endif
    if $TotAmtUSD = "" then 
        $TotAmtUSD = "0" 
    endif
    FileOpen($RepPath)
    FileWriteLine($RepPath, " ")
    FileWriteLine($RepPath, "Summary :")
    FileWriteLine($RepPath, "")
    FileWriteLine($RepPath, "Total Number of Records (1) : " & $strcount1)
    FileWriteLine($RepPath, "Total Number of Records (2) : " & $strcount2)
    FileWriteLine($RepPath, "Total Amount 1       : " & $TotAmt1)
    FileWriteLine($RepPath, "Total Amount 2      : " & $TotAmt2)
    FileClose($RepPath)
;========================================================================== 
;For .sum file
;==========================================================================
    $sumName = $ZipFile & ".SUM"
    $sumPath = "C:\Script\Test\" & $TDate & "\" & $sumName
    FileOpen($sumPath, 10)
    FileWriteLine($sumPath, "TCode=" & $LocCode)
    FileWriteLine($sumPath, "TID=" & $ShortCode & stringright($YYYY,2) & Chr(64 + $MM) & $DD & $Fseq)
    FileWriteLine($sumPath, "Total1=" & $TotAmt1)
    FileWriteLine($sumPath, "Total2=" & $TotAmt2)
    FileWriteLine($sumPath, "1Count=" & $strcount1)
    FileWriteLine($sumPath, "2Count=" & $strcount2)
    FileClose($sumPath)

as you can see i added a message box after WEnd but the value i am getting is 0 so basicaly the file is still open...

i need to close that file to be able to encrypt it.... any help would be apreciated, thanks

Edited by Ace08

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

$RepPath = "C:\Script\Test\" & $TDate & "\" & $RepFile
    FileOpen($RepPath, 10)
    FileWriteLine($RepPath, "DBO")
    FileWriteLine($RepPath, "Date: " & _datetomonth($MM) & " " & $DD & "," & $YYYY)
    FileWriteLine($RepPath, "Summary List")
    FileWriteLine($RepPath, " ")
    FileWriteLine($RepPath, "Filename:" & $FileName)
    FileWriteLine($RepPath, " ")
    FileClose($RepPath)

Replace FileOpen($RepPath, 10) by $_file = FileOpen($RepPath, 10)

And FileClose($RepPath) by FileClose($_file) Posted Image

See the helpfile for this Functions.

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

thanks a lot wakillon i was able to encrypt the file, i thought that i could do a fileopen without declaring it to a variable since i would just do a fileclose on the same file, thanks again ;)

just one more question, after doing your change even if i was able to sucessfuly close the file the msgbox still showed a value of 0 and from what i've read 0 means that fileclose failed.

sorry if i'm asking too much its just that i want to understand how autoit works

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

;========================================================================== 
;For .Rep file
;==========================================================================
$RepFile = $ShortCode & stringright($YYYY,2) & Chr(64 + $MM) & $DD & $Fseq & ".REP"
    $RepPath = "C:\Script\Test\" & $TDate & "\" & $RepFile
    $_RepPath = FileOpen($RepPath, 10)
    FileWriteLine($RepPath, "DBO")
    FileWriteLine($RepPath, "Date: " & _datetomonth($MM) & " " & $DD & "," & $YYYY)
    FileWriteLine($RepPath, "Summary List")
    FileWriteLine($RepPath, " ")
    FileWriteLine($RepPath, "Filename:" & $FileName)
    FileWriteLine($RepPath, " ")
    FileClose($_RepPath )
    
    $ReadFile = FileOpen($UnFilePath, 0)
    If $ReadFile = -1 Then
        MsgBox(0, "Error", $FileName & "Unable to open file.")
        Exit
    EndIf
    While 1
        $BRSline = FileReadLine($ReadFile)
        If @error = -1 Then ExitLoop
        $SFile = stringSplit($BRSline ,chr(34) & "," & chr(34),1)
        $SRefNo = $SFile[1]
        $SRefNo = StringReplace($SRefNo, chr(34), "")
        $SRemName = $SFile[3]
        $SBeneName = $SFile[7]
        $SAmt = $SFile[19]
        $SCur = $SFile[18]
        $SAccNo = $SFile[17]
        $SBnkCode = $SFile[15]
        if $strcountPHP = "" then 
            $strcountPHP = "0" 
        endif
        if $strcountUSD = "" then 
            $strcountUSD = "0" 
        endif
        $_RepPath2 = FileOpen($RepPath)
        FileWriteLine($RepPath, "RNo.       :" & $SRefNo)
        FileWriteLine($RepPath, "ReName     :" & $SReName)
        FileWriteLine($RepPath, "BeName :" & $SBeName)
        FileWriteLine($RepPath, "At         :" & $SAt)
        FileWriteLine($RepPath, "No.        :" & $SNo)
        FileWriteLine($RepPath, "BaName     :" & $SBCode)
        FileWriteLine($RepPath, " ")
        FileClose($_RepPath2)
    WEnd
    FileClose($ReadFile)
    msgbox(0,"", FileClose($ReadFile))
    if $TotAmtPHP = "" then 
        $TotAmtPHP = "0" 
    endif
    if $TotAmtUSD = "" then 
        $TotAmtUSD = "0" 
    endif
    $_RepPath3 = FileOpen($RepPath)
    FileWriteLine($RepPath, " ")
    FileWriteLine($RepPath, "Summary :")
    FileWriteLine($RepPath, "")
    FileWriteLine($RepPath, "Total Number of Records (1) : " & $strcount1)
    FileWriteLine($RepPath, "Total Number of Records (2) : " & $strcount2)
    FileWriteLine($RepPath, "Total Amount 1       : " & $TotAmt1)
    FileWriteLine($RepPath, "Total Amount 2      : " & $TotAmt2)
    FileClose($_RepPath3)
;========================================================================== 
;For .sum file
;==========================================================================
    $sumName = $ZipFile & ".SUM"
    $sumPath = "C:\Script\Test\" & $TDate & "\" & $sumName
    $_SumPath = FileOpen($sumPath, 10)
    FileWriteLine($sumPath, "TCode=" & $LocCode)
    FileWriteLine($sumPath, "TID=" & $ShortCode & stringright($YYYY,2) & Chr(64 + $MM) & $DD & $Fseq)
    FileWriteLine($sumPath, "Total1=" & $TotAmt1)
    FileWriteLine($sumPath, "Total2=" & $TotAmt2)
    FileWriteLine($sumPath, "1Count=" & $strcount1)
    FileWriteLine($sumPath, "2Count=" & $strcount2)
    FileClose($_SumPath)

here it is i've just added a variable with $_ in the beggining.

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

i see thanks again wakillon... so in order to get the functions return value i must 1st assign it to a variable ;)

No you don't, the error was that you tried to close something that already was closed. You should already know that as you have:

$RepFile = $ShortCode & stringright($YYYY,2) & Chr(64 + $MM) & $DD & $Fseq & ".REP"

And lots of other places where you use the return value directly.

Link to comment
Share on other sites

Change this:

$_RepPath3 = FileOpen($RepPath)

FileWriteLine($RepPath, " ")
FileWriteLine($RepPath, "Summary :")
FileWriteLine($RepPath, "")
FileWriteLine($RepPath, "Total Number of Records (1) : " & $strcount1)
FileWriteLine($RepPath, "Total Number of Records (2) : " & $strcount2)
FileWriteLine($RepPath, "Total Amount 1     : " & $TotAmt1)
FileWriteLine($RepPath, "Total Amount 2     : " & $TotAmt2)
FileClose($_RepPath3)

To this:

$_RepPath3 = FileOpen($RepPath)

FileWriteLine($_RepPath3, " ")
FileWriteLine($_RepPath3, "Summary :")
FileWriteLine($_RepPath3, "")
FileWriteLine($_RepPath3, "Total Number of Records (1) : " & $strcount1)
FileWriteLine($_RepPath3, "Total Number of Records (2) : " & $strcount2)
FileWriteLine($_RepPath3, "Total Amount 1   : " & $TotAmt1)
FileWriteLine($_RepPath3, "Total Amount 2   : " & $TotAmt2)
FileClose($_RepPath3)

And this:

$_SumPath = FileOpen($sumPath, 10)
FileWriteLine($sumPath, "TCode=" & $LocCode)
FileWriteLine($sumPath, "TID=" & $ShortCode & StringRight($YYYY, 2) & Chr(64 + $MM) & $DD & $Fseq)
FileWriteLine($sumPath, "Total1=" & $TotAmt1)
FileWriteLine($sumPath, "Total2=" & $TotAmt2)
FileWriteLine($sumPath, "1Count=" & $strcount1)
FileWriteLine($sumPath, "2Count=" & $strcount2)
FileClose($_SumPath)

To this:

$_SumPath = FileOpen($sumPath, 10)
FileWriteLine($_SumPath, "TCode=" & $LocCode)
FileWriteLine($_SumPath, "TID=" & $ShortCode & StringRight($YYYY, 2) & Chr(64 + $MM) & $DD & $Fseq)
FileWriteLine($_SumPath, "Total1=" & $TotAmt1)
FileWriteLine($_SumPath, "Total2=" & $TotAmt2)
FileWriteLine($_SumPath, "1Count=" & $strcount1)
FileWriteLine($_SumPath, "2Count=" & $strcount2)
FileClose($_SumPath)

In fact, just replace the entire bloody lot:

;==========================================================================
;For .Rep file
;==========================================================================
$RepFile = $ShortCode & StringRight($YYYY, 2) & Chr(64 + $MM) & $DD & $Fseq & ".REP"
$RepPath = "C:\Script\Test\" & $TDate & "\" & $RepFile
$_RepPath = FileOpen($RepPath, 10)
FileWriteLine($_RepPath, "DBO")
FileWriteLine($_RepPath, "Date: " & _DateToMonth($MM) & " " & $DD & "," & $YYYY)
FileWriteLine($_RepPath, "Summary List")
FileWriteLine($_RepPath, " ")
FileWriteLine($_RepPath, "Filename:" & $FileName)
FileWriteLine($_RepPath, " ")
FileClose($_RepPath)

$ReadFile = FileOpen($UnFilePath, 0)

If $ReadFile = -1 Then
    MsgBox(0, "Error", $FileName & "Unable to open file.")
    Exit
EndIf

While 1
    $BRSline = FileReadLine($ReadFile)

    If @error = -1 Then ExitLoop

    $SFile = StringSplit($BRSline, Chr(34) & "," & Chr(34), 1)
    $SRefNo = $SFile[1]
    $SRefNo = StringReplace($SRefNo, Chr(34), "")
    $SRemName = $SFile[3]
    $SBeneName = $SFile[7]
    $SAmt = $SFile[19]
    $SCur = $SFile[18]
    $SAccNo = $SFile[17]
    $SBnkCode = $SFile[15]

    If $strcountPHP = "" Then
        $strcountPHP = "0"
    EndIf

    If $strcountUSD = "" Then
        $strcountUSD = "0"
    EndIf
    
    $_RepPath2 = FileOpen($RepPath)
    FileWriteLine($_RepPath2, "RNo.     :" & $SRefNo)
    FileWriteLine($_RepPath2, "ReName   :" & $SReName)
    FileWriteLine($_RepPath2, "BeName   :" & $SBeName)
    FileWriteLine($_RepPath2, "At   :" & $SAt)
    FileWriteLine($_RepPath2, "No.  :" & $SNo)
    FileWriteLine($_RepPath2, "BaName   :" & $SBCode)
    FileWriteLine($_RepPath2, " ")
    FileClose($_RepPath2)
WEnd

FileClose($ReadFile)
MsgBox(0, "", FileClose($ReadFile))

If $TotAmtPHP = "" Then
    $TotAmtPHP = "0"
EndIf

If $TotAmtUSD = "" Then
    $TotAmtUSD = "0"
EndIf

$_RepPath3 = FileOpen($RepPath)
FileWriteLine($_RepPath3, " ")
FileWriteLine($_RepPath3, "Summary :")
FileWriteLine($_RepPath3, "")
FileWriteLine($_RepPath3, "Total Number of Records (1) : " & $strcount1)
FileWriteLine($_RepPath3, "Total Number of Records (2) : " & $strcount2)
FileWriteLine($_RepPath3, "Total Amount 1   : " & $TotAmt1)
FileWriteLine($_RepPath3, "Total Amount 2   : " & $TotAmt2)
FileClose($_RepPath3)

;==========================================================================
;For .sum file
;==========================================================================
$sumName = $ZipFile & ".SUM"
$sumPath = "C:\Script\Test\" & $TDate & "\" & $sumName
$_SumPath = FileOpen($sumPath, 10)
FileWriteLine($_SumPath, "TCode=" & $LocCode)
FileWriteLine($_SumPath, "TID=" & $ShortCode & StringRight($YYYY, 2) & Chr(64 + $MM) & $DD & $Fseq)
FileWriteLine($_SumPath, "Total1=" & $TotAmt1)
FileWriteLine($_SumPath, "Total2=" & $TotAmt2)
FileWriteLine($_SumPath, "1Count=" & $strcount1)
FileWriteLine($_SumPath, "2Count=" & $strcount2)
FileClose($_SumPath)
Edited by jaberwocky6669
Link to comment
Share on other sites

@AdmiralAlkex

oh yeah now i understand, i have closed the file before and placing it inside a msgbox will also do a close and since th file is closed i would realy be getting a 0 for a value :) thanks a lot for explaining ;)

@jaberwocky6669

thanks for the code jaberwocky6669 the code i have is working but after doing what you told me all i am getting is a bank file? any explanation or did i messed up again ;)

here is what i did

;========================================================================== 
;For .Rep file
;==========================================================================
$RepFile = $ShortCode & stringright($YYYY,2) & Chr(64 + $MM) & $DD & $Fseq & ".REP"
    $RepPath = "C:\Script\Test\" & $TDate & "\" & $RepFile
    $_RepPath = FileOpen($RepPath, 10)
    FileWriteLine($_RepPath , "DBO")
    FileWriteLine($_RepPath , "Date: " & _datetomonth($MM) & " " & $DD & "," & $YYYY)
    FileWriteLine($_RepPath , "Summary List")
    FileWriteLine($_RepPath , " ")
    FileWriteLine($_RepPath , "Filename:" & $FileName)
    FileWriteLine($_RepPath , " ")
    FileClose($_RepPath )
    
    $ReadFile = FileOpen($UnFilePath, 0)
    If $ReadFile = -1 Then
        MsgBox(0, "Error", $FileName & "Unable to open file.")
        Exit
    EndIf
    While 1
        $BRSline = FileReadLine($ReadFile)
        If @error = -1 Then ExitLoop
        $SFile = stringSplit($BRSline ,chr(34) & "," & chr(34),1)
        $SRefNo = $SFile[1]
        $SRefNo = StringReplace($SRefNo, chr(34), "")
        $SRemName = $SFile[3]
        $SBeneName = $SFile[7]
        $SAmt = $SFile[19]
        $SCur = $SFile[18]
        $SAccNo = $SFile[17]
        $SBnkCode = $SFile[15]
        if $strcountPHP = "" then 
            $strcountPHP = "0" 
        endif
        if $strcountUSD = "" then 
            $strcountUSD = "0" 
        endif
        $_RepPath2 = FileOpen($RepPath)
        FileWriteLine($_RepPath2, "RNo.       :" & $SRefNo)
        FileWriteLine($_RepPath2, "ReName     :" & $SReName)
        FileWriteLine($_RepPath2, "BeName :" & $SBeName)
        FileWriteLine($_RepPath2, "At         :" & $SAt)
        FileWriteLine($_RepPath2, "No.        :" & $SNo)
        FileWriteLine($_RepPath2, "BaName     :" & $SBCode)
        FileWriteLine($_RepPath2, " ")
        FileClose($_RepPath2)
    WEnd
    FileClose($ReadFile)
    msgbox(0,"", FileClose($ReadFile))
    if $TotAmtPHP = "" then 
        $TotAmtPHP = "0" 
    endif
    if $TotAmtUSD = "" then 
        $TotAmtUSD = "0" 
    endif
    $_RepPath3 = FileOpen($RepPath)
    FileWriteLine($_RepPath3, " ")
    FileWriteLine($_RepPath3, "Summary :")
    FileWriteLine($_RepPath3, "")
    FileWriteLine($_RepPath3, "Total Number of Records (1) : " & $strcount1)
    FileWriteLine($_RepPath3, "Total Number of Records (2) : " & $strcount2)
    FileWriteLine($_RepPath3, "Total Amount 1       : " & $TotAmt1)
    FileWriteLine($_RepPath3, "Total Amount 2      : " & $TotAmt2)
    FileClose($_RepPath3)
;========================================================================== 
;For .sum file
;==========================================================================
    $sumName = $ZipFile & ".SUM"
    $sumPath = "C:\Script\Test\" & $TDate & "\" & $sumName
    $_SumPath = FileOpen($sumPath, 10)
    FileWriteLine($_SumPath, "TCode=" & $LocCode)
    FileWriteLine($_SumPath, "TID=" & $ShortCode & stringright($YYYY,2) & Chr(64 + $MM) & $DD & $Fseq)
    FileWriteLine($_SumPath, "Total1=" & $TotAmt1)
    FileWriteLine($_SumPath, "Total2=" & $TotAmt2)
    FileWriteLine($_SumPath, "1Count=" & $strcount1)
    FileWriteLine($_SumPath, "2Count=" & $strcount2)
    FileClose($_SumPath)
Edited by Ace08

Work smarter not harder.My First Posted Script: DataBase

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