开发者

Looking for a way to execute a batch file once a folder hits 10 files

开发者 https://www.devze.com 2023-01-27 04:26 出处:网络
I am looking for a way to monitor a folder so that 开发者_如何学编程it executes a batch file once it hits 10 files. It would be cool if it used vbscript or any other type of solution like that.

I am looking for a way to monitor a folder so that 开发者_如何学编程it executes a batch file once it hits 10 files. It would be cool if it used vbscript or any other type of solution like that.

any help would be appreciated

thanks


Refer to this question: batch file to monitor additions to download folder

Note Nick's final solution where he counts files.

I would recommend that any test like this is executed via Task Scheduler.

Simple Example

rem Counting files...
set /a count = 0
for /f "tokens=*" %%P IN ('dir "C:\examplefolder" /A /b') do (set /a count += 1)

rem 10 or more files?
if %count% GEQ 10 call AnotherBatchFileHere.bat


The equivalent VBScript for this would involve obtaining a folder object and checking the count of its files collection. The Last Modified Date for the folder could also be examined to determine if something has changed or when.

Looping through the folder's .Files collection will let you examine the dates, size etc. of each file individually. Since this is a collection of file objects, any file object method can be executed directly or the file object can be passed off to a subroutine for processing. A similar .Subfolders collection enumerates folders created within this folder as folder objects in case you wish to monitor that situation as well.

File methods include .Copy .Move .Delete .OpenAsTextStream and the file properties .DateLastModified .DateLastAccessed .Attributes and .Name are updateable.

Note that the .Name property includes the file extension and if you change the name you may need to call FSO.GetExtensionName() to get that extension and append it to the new name before assigning it back to the property.

The Subfolders collection also has a .Add() method which can create a new child folder

.SubFolders.Add("NewFolderName")

and instead of the file object's .OpenAsTextStream method, folder objects have a .CreateTextFile() method which returns an open text stream object to a new text file created in that folder. A clever use could be to create a text stream used by your subroutines to log your file processing activities to a log file. Or read a text file directly and process its contents.

A basic example script to watch for 10 files in folder

Set FSO = WScript.CreateObject("Scripting.FileSystemObject")

WatchFolder FSO.GetFolder("c:\watched")

WScript.Quit

Sub WatchFolder(oFldr)
    While True
        If oFldr.Files.Count >= 10 Then
            WScript.Echo oFldr.Files.Count , "files in" , ofldr.Path , _
                         "Last Modified" , oFldr.DateLastModified

            For Each oFile In oFldr.Files
                WScript.Echo "File" , oFile.Name , _
                             "Last Modified" , oFile.DateLastModified , _
                             "Created" , oFile.DateCreated , _
                             "Size" , oFile.Size
                ' call subroutine to optionally process file
                KillJunkFile oFile
            Next
            Exit Function 
        End If 
        WScript.Sleep 2000  ' wait 2 seconds before checking again.
    Wend
End Sub 
Sub KillJunkFile(oTestFile)
    ' delete any file named junk.txt
    If LCase(oTestFile.Name) = "junk.txt" Then 
        oTestFile.Delete True  ' true forces the delete
    End If
End Sub

Note that the WatchFolder() function will loop until at least 10 files are in the watched folder. You have to kill the task to stop it otherwise or add some termination logic that checks something on your system that can tell it to quit looping. Something like a specially named file, a registry entry, an environment variable, etc. You could also comment out the While Wend loop keywords and have Windows Task Scheduler run the script every hour if it takes that long for enough files to appear.

0

精彩评论

暂无评论...
验证码 换一张
取 消