等待创建文件

时间:2013-12-17 15:34:30

标签: file vbscript

我已经在vbs中编写了一个程序,它应该执行以下操作。首先,它执行我用C ++编写的程序。在该程序结束时,将创建一个文件。所以我希望我的vbs脚本等到该程序完成后我尝试使用它来等待创建文件。

Set fso = CreateObject("Scripting.FileSystemObject")
While (Not(fso.FileExists("D:\dev\FF\temp.asm")))
Wend

一旦发生这种情况,它应该执行以下程序。

现在我注意到它并没有真正起作用,所以我不确定循环是否设置正确。 谁能告诉我如何正确创建文件?

编辑:现在我正在使用此代码:

folder="D:/dev/FF"
x=inputbox("name of your file (no extension)")

Set a = WScript.CreateObject("WScript.Shell") 
Return = a.Run("frankifier2 "&folder&" "&x&".ff", 1, true)

我收到了以下错误:

  

行:5
  Char:1
  错误:无法等待进程。
  代码:80020009
  资料来源:WshShell.Run

3 个答案:

答案 0 :(得分:1)

我使用Run方法并将“wait on return”布尔值设置为true。这在过去对我有用。以下是MSDN的链接:http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx

答案 1 :(得分:1)

我想有一天干掉这段代码。它包含两个几乎相同的功能。

runMyCppProgram filename  
If Not waitAbit(filename) Then Exit Sub 'or whatever else you wanna do here. The file is still not available after the timeout period.  
doSomethingElseOnFile filename  

Function waitAbit(filepath)  
    waitAbit = true  
    If Not waitTilExists (filepath, true) Then  
        MsgBox "Timeout. File not found: " & filepath, vbExclamation, "waitTilExists: error creating file"  
        waitAbit = false  
    End If  
    If Not waitTilUnlocked (filepath, true) Then  
        MsgBox "Timeout. File locked: " & filepath, vbExclamation, "waitTilUnlocked: error creating file"  
        waitAbit = false  
    End If  
End Function  

Function waitTilUnlocked (ByVal file, withRepeat)  
    ' dependency: the function fileIsLocked (file)  
    ' Sleeps until the file is unlocked  
    ' The polling interval will increase gradually, but never rises above MAX_WAITTIME  
    ' Times out after TIMEOUT msec. Will return false if caused by timeout.  
    Dim waittime, totalwaittime, rep, doAgain  
    Const INIT_WAITTIME = 20  
    Const MAX_WAITTIME = 1000  
    Const TIMEOUT = 5000  
    Const SLOPE = 1.1  
    file = replace (file, Chr(34), "") 'remove double quotes from the input  
    doAgain  = true  
    Do While doAgain  
        waittime = INIT_WAITTIME  
        totalwaittime = 0  
        Do While totalwaittime < TIMEOUT  
            waittime = Int (waittime * SLOPE)  
            If waittime>MAX_WAITTIME Then waittime=MAX_WAITTIME  
            totalwaittime = totalwaittime + waittime  
            WScript.sleep waittime  
            If Not fileIsLocked (file) Then  
                waitTilUnlocked = true  
                Exit Function  
            End If  
        Loop  
        If withRepeat Then  
            rep = MsgBox ("This file is locked:" & vbcr & file & vbcr & vbcr & "Keep trying?", vbRetryCancel+vbExclamation, "Locked file")  
            doAgain = (rep = vbRetry)  
        Else  
            doAgain = false  
        End If  
    Loop  

    waitTilUnlocked = false  
End Function  

Function fileIsLocked (file)  
    ' Returns true if the file is locked, and false if it is not.  
    ' If the files does not exist, false is returned  
    Dim ts  
    On Error Resume Next  
    If Not fso.fileExists (file) Then  
        fileIsLocked = False  
        Exit Function  
    End If  
    Set ts = fso.openTextFile (file, 8, False)  'ForAppending = 8  
     If Err.Number = 70 Then   'Permission denied  
         fileIsLocked = True  
     ElseIf Err.Number <> 0 then  
         WScript.Echo  "Unexpected Error #: " & Err.Number  
         fileIsLocked = False  
     Else  
         fileIsLocked = False  
     End If  
     Err.Clear  
    On Error GoTo 0  
End Function  

Function waitTilExists (ByVal file, withRepeat)  
    ' Sleeps until the file exists  
    ' The polling interval will increase gradually, but never rises above MAX_WAITTIME  
    ' Times out after TIMEOUT msec. Will return false if caused by timeout.  
    Dim waittime, totalwaittime, rep, doAgain  
    Const INIT_WAITTIME = 20  
    Const MAX_WAITTIME = 1000  
    Const TIMEOUT = 5000  
    Const SLOPE = 1.1  
    file = replace (file, Chr(34), "") 'remove double quotes from the input  
    doAgain  = true  
    Do While doAgain  
        waittime = INIT_WAITTIME  
        totalwaittime = 0  
        Do While totalwaittime < TIMEOUT  
            waittime = Int (waittime * SLOPE)  
            If waittime>MAX_WAITTIME Then waittime=MAX_WAITTIME  
            totalwaittime = totalwaittime + waittime  
            WScript.sleep waittime  
            If fso.fileExists (file) Then  
                waitTilExists = true  
                Exit Function  
            End If  
        Loop  
        If withRepeat Then  
            rep = MsgBox ("This file does not exist:" & vbcr & file & vbcr & vbcr & "Keep trying?", vbRetryCancel+vbExclamation, "File not found")  
            doAgain = (rep = vbRetry)  
        Else  
            doAgain = false  
        End If  
    Loop  

    waitTilExists = false  
End Function  

答案 2 :(得分:0)

试试这个,但你的代码也应该可以运行。

Set fso = CreateObject("Scripting.FileSystemObject")
While Not fso.FileExists("D:\dev\FF\temp.asm")
  Wscript.echo "File Not Found"
Wend