题
全部,
下面是我在 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 循环都有一个 Wend 和 EndIf 以及一个 If 条件。我在这里错过了什么吗?
解决方案
既然你有命令 ExitLoop
之后 then
, ,不需要 EndIf
.
(在这一行中: If @error = -1 Then ExitLoop
)
您可以:
去除
EndIf
While 1 $currentLine = FileReadLine($fileHandle) If @error = -1 Then ExitLoop $days = StringSplit($currentLine, ",") FileWrite($fileToWrite,$days[2] & ", " & $days[9] & @CRLF) Wend
移动
ExitLoop
到下一行。(这没有多大意义,但脚本仍然会运行。)While 1 $currentLine = FileReadLine($fileHandle) If @error = -1 Then ExitLoop $days = StringSplit($currentLine, ",") FileWrite($fileToWrite,$days[2] & ", " & $days[9] & @CRLF) EndIf Wend
我会选择#1。
其他提示
问题是,你的Endif
之前有Wend
和If
已经关闭,因为你必须在那里ExitLoop
。
因此,无论除去Endif
或把ExitLoop
别的地方。
不隶属于 StackOverflow