0

全部,

下面是我用 AutoIT 编写的代码。

    $fileToWrite = FileOpen("C:\output.txt", 1)

If FileExists("C:\test.csv") Then
    $fileHandle= FileOpen("test.csv", 0)
    If ($fileHandle = -1) Then
        MsgBox (0, "Error", "Error occured while reading the file")
        Exit
    Else
        While 1
            $currentLine = FileReadLine($fileHandle)
            If @error = -1 Then ExitLoop
            $days = StringSplit($currentLine, ",")
            FileWrite($fileToWrite,$days[2] & ", " & $days[9] & @CRLF)
            EndIf
        Wend
    EndIf
Else
    MsgBox (0, "Error", "Input file does not exist")
EndIf

FileClose($fileToWrite)
FileClose($fileHandle)

和一组错误:

C:\ReadCSV.au3(14,4) : ERROR: missing Wend.
            EndIf
            ^
C:\ReadCSV.au3(9,3) : REF: missing Wend.
        While
        ^
C:\ReadCSV.au3(15,3) : ERROR: missing EndIf.
        Wend
        ^
C:\ReadCSV.au3(3,34) : REF: missing EndIf.
If FileExists("C:\test.csv") Then
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\ReadCSV.au3(15,3) : ERROR: syntax error
        Wend
        ^
C:\ReadCSV.au3 - 3 error(s), 0 warning(s)
>Exit code: 0    Time: 3.601

我无法理解这里的问题,因为每个 While 循环和 If 条件都有一个 Wend 和 EndIf。我在这里错过了什么吗?

4

2 回答 2

2

问题是你Endif之前有一个Wend并且If已经关闭了,因为你ExitLoop在那里。

所以要么删除,Endif要么把它ExitLoop放在其他地方。

于 2010-10-29T04:49:13.010 回答
2

由于您在ExitLoop之后有命令then,因此不需要EndIf.

(在这一行If @error = -1 Then ExitLoop:)

您可以:

  1. 去除那个EndIf

    While 1
        $currentLine = FileReadLine($fileHandle)
        If @error = -1 Then ExitLoop
        $days = StringSplit($currentLine, ",")
        FileWrite($fileToWrite,$days[2] & ", " & $days[9] & @CRLF)
    Wend
    
  2. 移动ExitLoop到下一行。(这没有多大意义,但脚本仍然会运行。)

    While 1
        $currentLine = FileReadLine($fileHandle)
        If @error = -1 Then
        ExitLoop
        $days = StringSplit($currentLine, ",")
        FileWrite($fileToWrite,$days[2] & ", " & $days[9] & @CRLF)
        EndIf
    Wend
    

我会选择#1。

于 2010-10-29T05:46:29.810 回答