Jump to content

Weird file writing behavior


Recommended Posts

Basically, I'm trying to open several files as specified by the comman line and append them together (omitting hte first line of each file except the first). When I run it with no files, the new output file is created but when a file is specified, it is read correctly but the FileWrite never seems to do anything in that case. I've been specifying the files by arguments by dragging and dropped them on the exe but that shouldn't be relevant.

$num_files = $CmdLine[0];

$out_file_handle = FileOpen("out.txt", 2)

For $cur_file_num = 1 To $num_files Step 1
    $cur_file_name = $CmdLine[$cur_file_num]                            ;get the current filename as specified by the command line
    $in_file_handle = FileOpen($cur_file_name, 0)                        ;open the current file

;MsgBox(0, "Current File Number", $cur_file_num)                   ;DEBUG
    $cur_line = FileReadLine($in_file_handle)                               ;read the first line
    If $cur_file_num == 1 Then                                                 ;if this is the first file....

    ;MsgBox(0, "Writing the first line...", $cur_line)       ;DEBUG

        FileWriteLine($out_file_handle, $cur_line)
    EndIf
    While 1
        $cur_line = FileReadLine($in_file_handle)
        If @error = -1 Then ExitLoop
        FileWriteLine($out_file_handle, $cur_line)
    WEnd
    
Next

FileClose($out_file_handle)
Link to comment
Share on other sites

Basically, I'm trying to open several files as specified by the comman line and append them together (omitting hte first line of each file except the first). When I run it with no files, the new output file is created but when a file is specified, it is read correctly but the FileWrite never seems to do anything in that case. I've been specifying the files by arguments by dragging and dropped them on the exe but that shouldn't be relevant.

$num_files = $CmdLine[0];

$out_file_handle = FileOpen("out.txt", 2)

For $cur_file_num = 1 To $num_files Step 1
    $cur_file_name = $CmdLine[$cur_file_num]                        ;get the current filename as specified by the command line
    $in_file_handle = FileOpen($cur_file_name, 0)                       ;open the current file

;MsgBox(0, "Current File Number", $cur_file_num)                  ;DEBUG
    $cur_line = FileReadLine($in_file_handle)                           ;read the first line
    If $cur_file_num == 1 Then                                                ;if this is the first file....

;MsgBox(0, "Writing the first line...", $cur_line)      ;DEBUG

        FileWriteLine($out_file_handle, $cur_line)
    EndIf
    While 1
        $cur_line = FileReadLine($in_file_handle)
        If @error = -1 Then ExitLoop
        FileWriteLine($out_file_handle, $cur_line)
    WEnd
    
Next

FileClose($out_file_handle)
1. You may be getting a different @error from FileReadLine().

2. You need to close each input file when done with it.

3. You need to test things like $CmdLine[0] before using them.

4. This has much more debug reporting (via TrayTip).

Global $sOut = "out.txt"
Global $hOut, $hIn, $iLines, $sLine

If $CmdLine[0] Then
    __DebugTrayTip("File count = " & $CmdLine[0], 1)
    $hOut = FileOpen($sOut, 2)
    If $hOut <> -1 Then
        __DebugTrayTip("Opened output file: " & $sOut, 1)
        For $n = 1 To $CmdLine[0]
            $hIn = FileOpen($CmdLine[$n], 0)
            If $hIn = -1 Then
                MsgBox(16, "Error", "Failed to open input file: " & $CmdLine[$n])
                ContinueLoop
            EndIf
            __DebugTrayTip("Opened input file: " & $CmdLine[$n], 1)
            $iLines = 0
            $sLine = FileReadLine($hIn)
            If $n = 1 Then __FileWriteLine()
            While 1
                $sLine = FileReadLine($hIn)
                If @error = 0 Then
                    __FileWriteLine()
                Else
                    __DebugTrayTip("FileReadLine() @error = " & @error, 2)
                    FileClose($hIn)
                    __DebugTrayTip("Closed input file: " & $CmdLine[$n], 1)
                EndIf
            WEnd
        Next
        FileClose($hOut)
        __DebugTrayTip("Opened output file: " & $sOut, 1)
    Else
        MsgBox(16, "Error", "Failed to open output file.")
    EndIf
Else
    MsgBox(16, "Error", "No files to process... exiting.")
EndIf

; Write line with debug output of count
Func __FileWriteLine()
    FileWriteLine($hOut, $sLine)
    $iLines += 1
    __DebugTrayTip("Lines writen from this input file = " & $iLines)
EndFunc   ;==>__FileWriteLine

; Display Debug info as a TrayTip
Func __DebugTrayTip($sText, $iSleep = 0)
    TrayTip("Debug", $sText, 5)
    If $iSleep Then Sleep($iSleep * 1000)
EndFunc   ;==>__DebugTrayTip

Of course, all the debug message stuff (and the delays they cause) can be removed once you are satisfied with how it works.

:)

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
Link to comment
Share on other sites

Perhaps a bug exists?

Drop a file on this compiled script and look in your user profile folder.

MsgBox(0, 'WorkingDir', @WorkingDir)

If FileWrite('test.txt', 'testing') Then
    MsgBox(0, 'FileWrite', 'File written')
EndIfoÝ÷ ØÚ0¶¬v¬±«Þ¶W¢Ø^±Êâ¦×è׫jÌ«Þiû¥­ÊW®+^±ç¦²Ú0¢¹)«­¢+Ù5Í  ½à À°Ìäí]½É­¥¹¥ÈÌäì°]½É­¥¹¥È¤()%¥±]ɥѡMÉ¥ÁѥȵÀìÌäìÀäÈíÑÍйÑáÐÌäì°ÌäíÑÍÑ¥¹Ìäì¤Q¡¸(%5Í    ½à À°Ìäí¥±]É¥ÑÌäì°Ìäí¥±ÝÉ¥ÑѸÌäì¤)¹%oÝ÷ Ù8^¡×[z¯«Â+a¶µ²×±jjez+-«V¢¹"â®+÷Þ­éí¶,z³ë¡ø¥x8«jwrzÛayø¥z+,j÷¶,z³ë¡ø¥x8«J¥ìmz{"¢{'¢¹yö®Ûhk
+)àv*ÞrÚ+ÊÔ±êÏ®âàâ®)æÉìiz¸ÇºÜ!È­ lßÕ¨®H§8«­ë.جv'ßz·§¶ÇÆî:»^²ÛaÀÇ+pØC­¨7ö}ýº)jwr¢éÊÇjȦV«y÷ÞrÚ±«âayø¥zÚv'ßz·§µØ«yËh¯&¬¶ayìiyË^w
+)àv*ÞrÚ+ÊÊ"¶f²±ç¦¶Þk§v,nè¡×r(}÷yÈhµë-xÞ¶µë-·ijëýÊxʯyìiyË^vØ^~)^iº/zÚzƯyا¶
Ø«yËh¯&î¶+pj˽çbDDôNÄëm+,r¸©¶ÇËhÂä~ì¢g«y«('Òr¸©´8«Ãjëh×6If @WorkingDir <> @ScriptDir Then
    FileChangeDir(@ScriptDir)
EndIf

MsgBox(0, 'WorkingDir', @WorkingDir)

If FileWrite('test.txt', 'testing') Then
    MsgBox(0, 'FileWrite', 'File written')
EndIf

Tested on XP Pro using AutoIt 3.2.10.0

:)

Link to comment
Share on other sites

1. You may be getting a different @error from FileReadLine().

2. You need to close each input file when done with it.

3. You need to test things like $CmdLine[0] before using them.

4. This has much more debug reporting (via TrayTip).

1. The @error wasn't showing any sign of the problem. I had more debugging message boxes and I verified each string it attempted to write was correct.

2. I was closing each input file after openning them before I removed globs of comments and debugging code to clean it up before posting. I must have deleted that line.

3. $CmdLine[0] was correct for the example input that I was trying.

4. Thanks for the debugging tip. Obviously I'm new to AutoIt but I have experience with Java and C mainly and after dabling with various WINAPI calls, I decided AutoIt is probably easier to use for solving my problems (not this one specifically).

**stuff**

Ah thanks a bunch! There was problem with the script directory vs current working directory and the output file was being dumping into %USERPROFILE% folder. Perhaps the AutoIt help file should be updated specifying this behavior or is this already covered and I overloooked it? Edited by llamaoo7
Link to comment
Share on other sites

Ah thanks a bunch! There was problem with the script directory vs current working directory and the output file was being dumping into %USERPROFILE% folder. Perhaps the AutoIt help file should be updated specifying this behavior or is this already covered and I overloooked it?

Not mentioned AFAIK. It seems like another weird Microsoft bug and may affect all functions that can use relative paths, so it would need a page like a "Microsoft Undesirable Bugs" page or something, (bah, OS bugs/ill effects could be fixed in the future but we still need to live with them with current OSes). What page would you suggest?

Tested some more. Sometimes you may want to not force change with changing the working directory and using FileChangeDir(@WorkingDir) still comes up bad. So it does leave a undesirable side effect if Drag'n'Drop is used to start a process.

; Compile and then Drag'n'Drop a file on to the compiled script to test

If FileChangeDir(@WorkingDir) Then
    ; You will see the Msgbox but it is still a false @WorkingDir set.
    MsgBox(0, 'Changed WorkingDir', @WorkingDir)
EndIf

If $CMDLINE[0] And FileExists($CMDLINE[1]) Then
    ; Maybe a Drag'n'Drop so change to @ScriptDir as @WorkingDir is unreliable to trust.
    FileChangeDir(@ScriptDir)
EndIf

MsgBox(0, 'WorkingDir', @WorkingDir)

If FileWrite('test.txt', 'testing') Then
    ; If shown then file exists either in @ScriptDir (or @UserProfileDir if FileChangeDir(@ScriptDir) is not used)
    MsgBox(0, 'FileWrite', 'File written')
EndIf
Link to comment
Share on other sites

What page would you suggest?

I'm not really sure but from my past programming expereince, the working directory tends to be the default directory when you specify a file to read by just the filename. I'm not sure if I'm in the minority or the majority when it comes to this.

Tested some more. Sometimes you may want to not force change with changing the working directory and using FileChangeDir(@WorkingDir) still comes up bad. So it does leave a undesirable side effect if Drag'n'Drop is used to start a process.

I didn't bother using FileChangeDir() so I just went with @ScriptDir and it worked just fine. I know this probably isn't a common use of AutoIt but I needed something quick and I have very little limitations what I could alter on the computer and AutoIt was already installed.

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