Autohotkey FileAppend循环中的数据丢失

Autohotkey FileAppend循环中的数据丢失,autohotkey,Autohotkey,在循环中,我使用FileAppend向文件添加文本。在循环的每一次迭代中,数据都累积在一个变量中,并且在循环的每n次迭代中调用FileAppend来保存数据。在FileAppend调用之后,在下一次迭代中积累在变量中的数据偶尔会丢失。由于这是非常间歇性的,我无法重现这种行为。似乎在某些情况下,脚本在FileAppend之后需要延迟。这是一个已知的问题吗?我搜索过AHK论坛和这个网站,但没有关于这个问题的报道 下面是一段发生这种情况的代码: Loop, %intMax% ; for each re

在循环中,我使用FileAppend向文件添加文本。在循环的每一次迭代中,数据都累积在一个变量中,并且在循环的每n次迭代中调用FileAppend来保存数据。在FileAppend调用之后,在下一次迭代中积累在变量中的数据偶尔会丢失。由于这是非常间歇性的,我无法重现这种行为。似乎在某些情况下,脚本在FileAppend之后需要延迟。这是一个已知的问题吗?我搜索过AHK论坛和这个网站,但没有关于这个问题的报道

下面是一段发生这种情况的代码:

Loop, %intMax% ; for each record in the collection
{
    if !Mod(A_Index, intProgressIterations)
    ; update the progress bar and save the data every intProgressIterations
    ; (when intProgressIterations / A_Index = 0)
    {
        ProgressUpdate(A_index, intMax, strProgressText)
        ; update progress bar only every %intProgressIterations% iterations
        FileAppend, %strData%, %strFilePath%
        strData := ""
        ; save the data accumulated in strData and empty it
    }
    strData := strData . BuildData(objCollection[A_Index]) 
    ; build the data for this record and add it to strData
}

更准确地说,它是行
strData:=strData的一次(或多次)迭代的内容。BuildData(objCollection[A_Index])
丢失。

可以是任意数量的东西。文件可能被锁定,BuildData函数中可能存在导致其无法生成数据的错误

我建议在添加数据之前和之后检查上次修改的日期

如果相同,您可以重试和/或通知用户


至于您关于延迟的问题,它应该不需要延迟,脚本在完成对文件的写入之前不会继续执行下一行代码。

以下关于文件锁定的发现,此带有错误管理的代码将更安全:

Loop, %intMax% ; for each record in the collection
{
    if !Mod(A_Index, intProgressIterations)
    ; update the progress bar and save the data every intProgressIterations
    ; (when intProgressIterations / A_Index = 0)
    {
        ProgressUpdate(A_index, intMax, strProgressText)
        ; update progress bar only every %intProgressIterations% iterations
        Loop
        {
            FileAppend, %strData%, %strFilePath%
            ; save the data accumulated in strData and empty it after the loop
            if ErrorLevel
                Sleep, 20
        }
        until !ErrorLevel or (A_Index > 50) ; after 1 second (20ms x 50), we have a problem 
        if (ErrorLevel)
            strError := strError . "Error writing line " . A_Index . " Error: #" . A_LastError . "`n"
        strData := ""
    }
    strData := strData . BuildData(objCollection[A_Index]) 
    ; build the data for this record and add it to strData
}

作为此问题的补充(用于将来的搜索)。。。我发现,当输出文件位于Dropbox上时,尝试在循环中添加FileAppend也会导致数据丢失

Dropbox将在循环的早期周期检测文件的更新,然后在将更新(更新)文件发送到Dropbox服务器时锁定文件。我在一个短至150行的文本文件中跟踪了数据丢失

我使用的解决方案是在Dropbox监控的主文件夹外创建一个临时输出文件,然后改为写入。循环完成后,将临时文件复制回Dropbox,然后删除临时文件

下面是一个部分代码示例,演示了我在循环完成后如何使用完整的临时文件进行切换:

; Copy the Daily Scrub temp file to var holding the filepath to todo.txt, then delete the Daily Scrub temp file
FileCopy, C:\temp-dailyscrub.txt, %todoFilePathParse%, 1
FileDelete, C:\temp-dailyscrub.txt

我在我的代码中添加了一些错误检查,发现了一些“LastError”32(错误共享违反)。你的第一个猜测是正确的。感谢您确认脚本在继续之前等待FileAppend执行结束。有趣的发现。当我收到这个问题时就是这样。但是,只有当您知道用户正在保存到Dropbox时,您的解决方案才会起作用。如果你把你的应用程序分发给一大群用户,你就不知道了。然后,您可以添加一个延迟(使用Sleep命令),或者,一个更好的解决方案可能是检查Person93之前建议的最后修改日期。