Sign in to follow this  
Followers 0
LurchMan

need help with writing to a file....

8 posts in this topic

#1 ·  Posted (edited)

All of the variables that are not defined in this func are defined else where in the script. For the first file, it appends "1" to it, for the second one it appends "002" to it, and then it wont even create and write to the file from then on.

All help will be greatly appreciated

Func LogBug ()
    $Prob = GUICtrlRead ($ProbIn)
    $Name = GUICtrlRead ($NameIn)
    $Script = GUICtrlRead ($ScriptIn)
    Global $BugCnt = 1
    $File = (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log")
    $FileTest = FileExists (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log") 
    If $FileTest = True Then
        Do 
            $BugCnt = $BugCnt + 1
            $NewFile = FileExists (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log")
        Until $NewFile = False  
        If $BugCnt < "010" Then
            $BugCnt = "00" & $BugCnt
        ElseIf $BugCnt < "100" Then
            $BugCnt = "0" & $BugCnt
        EndIf   
        $NewFile = (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log")
        _FileCreate (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log")
        FileOpen ($NewFile , 2)
        FileWrite($NewFile, "***************************************************" _ 
              & @CRLF & "Name: " & $Name _
              & @CRLF & "Script: " & $Script _
              & @CRLF & "Problems: " & $Prob _
              & @CRLF & "***************************************************")
        FileClose ($NewFile)
    Else
;~      FileOpen ($File , 2)
        If $BugCnt < "010" Then
            $BugCnt = "00" & $BugCnt
        ElseIf $BugCnt < "100" Then
            $BugCnt = "0" & $BugCnt
        EndIf   
        _FileCreate ($File)
        FileOpen ($File , 2)
        FileWrite($File,    "***************************************************" _ 
                & @CRLF & "Name: " & $Name _
                & @CRLF & "Script: " & $Script _
                & @CRLF & "Problems: " & $Prob _
                & @CRLF & "***************************************************")
        FileClose ($File)
    EndIf   
Email ()
EndFunc
Edited by LurchMan

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites



All of the variables that are not defined in this func are defined else where in the script. For the first file, it appends "1" to it, for the second one it appends "002" to it, and then it wont even create and write to the file from then on.

All help will be greatly appreciated

Func LogBug ()
    $Prob = GUICtrlRead ($ProbIn)
    $Name = GUICtrlRead ($NameIn)
    $Script = GUICtrlRead ($ScriptIn)
    Global $BugCnt = 1
    $File = (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log")
    $FileTest = FileExists (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log") 
    If $FileTest = True Then
        Do 
            $BugCnt = $BugCnt + 1
            $NewFile = FileExists (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log")
        Until $NewFile = False  
        If $BugCnt < "010" Then
            $BugCnt = "00" & $BugCnt
        ElseIf $BugCnt < "100" Then
            $BugCnt = "0" & $BugCnt
        EndIf   
        $NewFile = (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log")
        _FileCreate (@ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $BugCnt & ".log")
        FileOpen ($NewFile , 2)
        FileWrite($NewFile, "***************************************************" _ 
              & @CRLF & "Name: " & $Name _
              & @CRLF & "Script: " & $Script _
              & @CRLF & "Problems: " & $Prob _
              & @CRLF & "***************************************************")
        FileClose ($NewFile)
    Else
;~      FileOpen ($File , 2)
        If $BugCnt < "010" Then
            $BugCnt = "00" & $BugCnt
        ElseIf $BugCnt < "100" Then
            $BugCnt = "0" & $BugCnt
        EndIf   
        _FileCreate ($File)
        FileOpen ($File , 2)
        FileWrite($File,    "***************************************************" _ 
                & @CRLF & "Name: " & $Name _
                & @CRLF & "Script: " & $Script _
                & @CRLF & "Problems: " & $Prob _
                & @CRLF & "***************************************************")
        FileClose ($File)
    EndIf   
Email ()
EndFunc
I think you need to stop treating strings and integers as equivalent. Keep $BugCnt as a normal integer and use StringFormat() to produce the minimum three digit with leading zeroes that you want. At some point that is probably screwing up your process:
For $iBugCnt = 1 To 16
    $sBugCnt = StringFormat("%03d", $iBugCnt)
    ConsoleWrite("$iBugCnt = " & $iBugCnt & "  $sBugCnt = " & $sBugCnt & @LF)
Next

For $iBugCnt = 91 To 106
    $sBugCnt = StringFormat("%03d", $iBugCnt)
    ConsoleWrite("$iBugCnt = " & $iBugCnt & "  $sBugCnt = " & $sBugCnt & @LF)
Next

For $iBugCnt = 991 To 1006
    $sBugCnt = StringFormat("%03d", $iBugCnt)
    ConsoleWrite("$iBugCnt = " & $iBugCnt & "  $sBugCnt = " & $sBugCnt & @LF)
Next

Note the convention for the variable naming, where $iBugCnt is an integer, and $sBugCnt is a string (never to be used in arithmetic compares).

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks for the help....I kinda understand how this works and what its doing, but I don't quite understand how to integrate it into my original code....(sorry for the newb question here)


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites

Never mind! I got it to work! Thank you so much for your help! :) I've been working on this function for about 2 hours trying to get it to work right.....


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for the help....I kinda understand how this works and what its doing, but I don't quite understand how to integrate it into my original code....(sorry for the newb question here)

Something like this:
Func LogBug ()
    $Prob = GUICtrlRead ($ProbIn)
    $Name = GUICtrlRead ($NameIn)
    $Script = GUICtrlRead ($ScriptIn)
    Global $iBugCnt = 1
    While 1
        $sBugCnt = StringFormat("%03d", $iBugCnt)
        $sFile = @ScriptDir & "\Temp\Bugs\" & @MON & @MDAY & @YEAR & "_BugReport_" & $sBugCnt & ".log"
        If FileExists($sFile) Then 
            $iBugCnt += 1
        Else
            ExitLoop
        EndIf
    WEnd
    $hFile = FileOpen ($sFile , 1); Get file handle
    FileWrite($hFile, "***************************************************" & @CRLF & _
            "Name: " & $Name & @CRLF & _
            "Script: " & $Script & @CRLF & _
            "Problems: " & $Prob & @CRLF & _
            "***************************************************")
    FileClose ($hFile)
    Email ()
EndFunc

Note $iBugCnt is an integer count. $sBugCnt is the formatted string of $iBugCnt. $sFile is the string file path, and $hFile is the handle to the opened file.

Never use FileOpen() and then reference the file by path, use the handle returned by FileOpen().

:)

Edit: Typo: "Never use FileOpen() and then reference..."

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Never use FileOpen() and the reference the file by path, use the handle returned by FileOpen().

:)

Don't quite understand what your saying there? from what i understand your saying something like:

FileOpen ($File)

Correct?

Edit: Sorry Didn't look very closely at your sample code...I had my original code set up in places like that because I was trying absolutly everything i knew to try and make it run correctly...I've already changed everything back :o

Edited by LurchMan

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites

@PsaltyDS

Ive never used FileOpen for after use FileWrite I only use FileWrite and it works great all the time :)

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Don't quite understand what your saying there? from what i understand your saying something like:

FileOpen ($File)

Correct?

No. Because you didn't save the handle that was returned from FileOpen(). That should be:
$hFile = FileOpen ($File)

Then you should use the handle ($hFile) for your FileWrite() and FileClose().

This is a confusing point for noobs with AutoIt (including me, a while back). You can avoid working with handles and use the string file path. Or, you can use handles. What you must not do is mix the two.

If you want to stick with the string path, then never FileOpen()/FileClose(). You can just write to the file:

$sFile = "C:\Temp\File.txt"
FileWrite($sFile, "This is my file.")

This has limitations, like not specifying the modes you can add to FileOpen().

If you choose to use a file handle, then use the handle and do not refer to the file again by the string path until after you have done FileClose().

It's just either/or. Use the path or use a handle, but don't mix them.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0