Vbscript: Constantly check if a file exists and displays a lifetime

I had a very specific question regarding my code.

So I have a folder. This folder may have> = 0 files. My first script runs in the background, checking if the number of files is> 0. As soon as it is> 0, it activates the second script using the path + file name, and as soon as the file is deleted / deleted from the folder, it displays the file lifetime .

Everything works fine, but there is one problem: If there are several files in a folder in one folder, these are only the "watchers" of the top one (file name, ascending). Therefore, if the first of them is deleted, he probably notices the second, but the lifetime is incorrect, because it did not start until the first was deleted.

Here are my two codes:

Script1.vbs:

Set fso =CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count = 1 Then
    pfadTiff0 = CStr(WScript.Arguments(0))
Else
    msgbox "Bitte Argumente (Pfade) angeben"
    WScript.Quit
End If

While True
    Set ordnerTiff0 = fso.GetFolder(pfadTiff0)
    Set filesTiff0 = ordnerTiff0.Files
    anzFilesTiff0 = ordnerTiff0.Files.Count
    If anzFilesTiff0 > 0 Then
        For Each objFile in filesTiff0  
            CreateObject("WScript.Shell").Run "QueueTimeUP.vbs " & objFile.Name & " " & pfadTiff0, 0, True                                      
        Next
    End If
    WScript.Sleep 2000
WEnd

Script2.vbs:

filename = CStr(WScript.Arguments(0))
pfad = CStr(WScript.Arguments(1))
Set fso = CreateObject("Scripting.FileSystemObject")

startZeit = Timer()
komplett = pfad&"\"&filename

While fso.FileExists(komplett) = True
    WScript.Sleep 100
WEnd

endZeit = Timer()
differenz = endZeit-startZeit
msgbox "Existenz von Job " & filename & " in Sekunden: " & differenz
+4
3

.

@Bond: while, true loop , . , , . "" Run-Statement !

@Rob1991: , , .

. , :

Set fso =CreateObject("Scripting.FileSystemObject") 
If WScript.Arguments.Count = 1 Then
    pfadTiff0 = CStr(WScript.Arguments(0))
Else
    msgbox "Bitte Argumente (Pfade) angeben"
    WScript.Quit
End If

Set ordnerTiff0 = fso.GetFolder(pfadTiff0
Set filesTiff0 = ordnerTiff0.Files

letztesFile = "000a"    // PART OF SOLUTION, IT´S "LAST FILE"

While True
    For Each objFile in filesTiff0
        If objFile.Name > letztesFile Then   // PART OF SOLUTION
            CreateObject("WScript.Shell").Run "QueueTimeUP.vbs " & objFile.Name & " " & pfadTiff0, 0, False  // thanks Bond for "false"
            letztesFile = objFile.Name      // PART OF SOLUTION
        End If      
    Next                                        
    WScript.Sleep 500
WEnd
+2

:

CreateObject("WScript.Shell").Run "QueueTimeUP.vbs " & objFile.Name & " " & pfadTiff0, 0, True

True. , script () . , script , script (QueueTimeUp.vbs), , .

False , script. While True script .

' Main script. Remove "While True" loop.
Set ordnerTiff0 = fso.GetFolder(pfadTiff0)
Set filesTiff0 = ordnerTiff0.Files
anzFilesTiff0 = ordnerTiff0.Files.Count
If anzFilesTiff0 > 0 Then
    For Each objFile in filesTiff0  

        ' Use False for last param to specify asynchronous call...
        CreateObject("WScript.Shell").Run "QueueTimeUP.vbs " & objFile.Name & " " & pfadTiff0, 0, False

    Next
End If

' Allow script to complete.

QueueTimeUp.vbs script .

FYI: WMI VBScript, __InstanceCreationEvent __InstanceDeletionEvent, script, . , .

+1

I believe that for each file it downloads vbs2, but does not start the next execution until vbs2 finishes. If I used the following code below, it will not iterate until the cmd instance ends.

For Each objFile in colFiles
     oShell.run "cmd /k CD C:\Program File" ,1 , true
Next

I think you would like to start the start timer in your first vbs, and then pass it to the second vbs2. This would mean that the start time is called only once at the start of the program.

startZeit = Timer() 

For Each objFile in filesTiff0 
     CreateObject("WScript.Shell").Run "QueueTimeUP.vbs " & objFile.Name & " " & pfadTiff0 & " " & startZeit , 0, True                                      
Next

vbs 2

startZeit = CStr(WScript.Arguments(2))

Hope this helps

0
source

Source: https://habr.com/ru/post/1535923/


All Articles