Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在打开.txt文件之前检查文件锁定_Excel_Vba_File_Text_Locking - Fatal编程技术网

Excel 如何在打开.txt文件之前检查文件锁定

Excel 如何在打开.txt文件之前检查文件锁定,excel,vba,file,text,locking,Excel,Vba,File,Text,Locking,长话短说。。。我正在使用pc打开服务器上的.txt文件。。。但有时.txt文件未完成(仍有数据存储在源计算机的缓冲区中) 比我聪明的人已经发现.txt文件被操作系统“锁定”,直到它完成,但我仍然能够打开它。我想等待.txt文件“未锁定”,然后再打开它。我该怎么做 注:比我更聪明的人解释说。。。txt文件可以由“记事本”之类的“哑”程序打开,但如果您尝试使用“Microsoft Word”打开它。。。您会收到一条消息,告诉您它已锁定…我不知道下面的代码在您的场景中的效果如何 import time

长话短说。。。我正在使用pc打开服务器上的.txt文件。。。但有时.txt文件未完成(仍有数据存储在源计算机的缓冲区中)

比我聪明的人已经发现.txt文件被操作系统“锁定”,直到它完成,但我仍然能够打开它。我想等待.txt文件“未锁定”,然后再打开它。我该怎么做


注:比我更聪明的人解释说。。。txt文件可以由“记事本”之类的“哑”程序打开,但如果您尝试使用“Microsoft Word”打开它。。。您会收到一条消息,告诉您它已锁定…

我不知道下面的代码在您的场景中的效果如何

import time

is_open = False

while not(is_open):
    try:
        f = open('test.txt','w')
        is_open=True
    except:
        time.sleep(1)
  • 您可以将常量更改为参数(如果您认为其值需要动态更改/确定)
  • 您还可以更改
    GetFileOrWait
    的实现,使其允许无限循环(我选择避免这种情况,但您可能希望这样)
  • 总而言之,下面的函数基本上是一个尝试在120秒内返回工作簿(或超时出错)的函数,希望您可以在父子例程/过程中使用它
  • 您可以使用
    定时器或其他较低级别的API来指定更精细的频率(秒可能太粗糙)

选项显式
私有函数GetFileOrNothing()作为工作簿
Const FILE_PATH As String=“D:\test.txt”可以将其作为参数传入(如果需要)。
出错时继续下一步
设置GetFileOrNothing=Workbooks.Open(文件名:=FILE\u路径,只读:=True,忽略只读建议:=True)
错误转到0
端函数
私有函数GetFileOrWait()作为工作簿
'尝试打开一个文件。如果访问失败,请等待n秒,然后重试。
'此函数在N秒后引发错误(超时以防止无限循环)。
常量最大等待时间(以秒为单位)长=10
Const INTERVAL_WAIT_,以秒为单位,长度=1
Dim timeToStopAt As Date
timeToStopAt=DateAdd(“s”,以秒为单位的最大等待时间,现在)
现在执行
我认为这样做行不通,因为无论是否有人打开了文件,我都可以很好地打开它。向我解释的方式是,txt文件太“哑”,无法知道文件是否已打开,而记事本太“哑”,无法知道文件已打开。但是,操作系统(在我的例子中是windows)将文件锁定在txt文件上。因此,如果我尝试使用“更智能”的程序(例如MS Word)打开它,那么它将看到它已经打开了。无论如何。。。我仍然没有找到解决这个问题的办法。我想我能做的最好的就是试着用word打开,但我觉得可能有更好的方法。
import time

is_open = False

while not(is_open):
    try:
        f = open('test.txt','w')
        is_open=True
    except:
        time.sleep(1)
Option Explicit

Private Function GetFileOrNothing() As Workbook
    Const FILE_PATH As String = "D:\test.txt" ' Could pass this in as argument (if needed).
    On Error Resume Next
    Set GetFileOrNothing = Workbooks.Open(Filename:=FILE_PATH, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
    On Error GoTo 0
End Function

Private Function GetFileOrWait() As Workbook
    ' Attempts to open a file. If access fails, waits n seconds before trying again.
    ' This function raises an error (times out to prevent infinite loop) after N seconds.
    Const MAXIMUM_WAIT_IN_SECONDS As Long = 10
    Const INTERVAL_WAIT_IN_SECONDS As Long = 1

    Dim timeToStopAt As Date
    timeToStopAt = DateAdd("s", MAXIMUM_WAIT_IN_SECONDS, Now)

    Do While Now < timeToStopAt
        Dim outputWorkbook As Workbook
        Set outputWorkbook = GetFileOrNothing()

        If Not (outputWorkbook Is Nothing) Then Exit Do
        Application.Wait DateAdd("s", INTERVAL_WAIT_IN_SECONDS, Now)
        DoEvents
    Loop

    If outputWorkbook Is Nothing Then
        Err.Raise vbObjectError + 5, , "Failed to access file within the specified time frame."
    End If

    Set GetFileOrWait = outputWorkbook
End Function