Vbscript 如何监视包含所有子文件夹以及子文件夹内的文件夹

Vbscript 如何监视包含所有子文件夹以及子文件夹内的文件夹,vbscript,Vbscript,我目前正在使用监视子文件夹的vbscript。但是现在,我希望脚本监视子文件夹中的文件夹 例如:映射为“导入”。在导入文件夹中有几个文件夹子文件夹。在子文件夹中还有更多的文件夹,这就是我想要监视的 我希望这是清楚明白我的意思 例如: 例2: 这就是我现在拥有的: Option Explicit Dim objExcel, strExcelPath, objSheet, fso, folder, colFolders, objFSO, objFile, objbook, objDoc,

我目前正在使用监视子文件夹的vbscript。但是现在,我希望脚本监视子文件夹中的文件夹

例如:映射为“导入”。在导入文件夹中有几个文件夹子文件夹。在子文件夹中还有更多的文件夹,这就是我想要监视的

我希望这是清楚明白我的意思

例如:

例2:

这就是我现在拥有的:

Option Explicit
Dim objExcel, strExcelPath, objSheet, fso, folder, colFolders, objFSO, objFile, objbook, objDoc,                             objShell, rs, f, s, EmailBody, EmailAttachments, objWorkbook, strFile, IntRow, objRange, x,       objWorksheet,wbc, SaveChanges, objSubFolder, objFrigolanda, foldername, moddate,folders, Frigolanda
Const adVarChar = 200 
Const adDate = 7 
Const adBigInt = 20
'===== 
'Set objecten
set fso = createobject("scripting.filesystemobject") 
set folder = fso.getfolder("\\netko-sbs\data\imports\") 
Set colFolders = folder.SubFolders
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("\\netko-sbs\data\Imports\output.txt", True)
'===== 
' Check if file exists.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists(strFile) = True) Then
   objFSO.DeleteFile(strFile)
End If
'===== 
'create a custom disconnected recordset 
'with fields for filename, last modified date, and size. 
'===== 
 set rs = createobject("ador.recordset") 
 rs.fields.append "foldername",adVarChar,255 
 rs.fields.append "moddate",adDate 
'rs.fields.append "filesize",adBigInt 
'====
'Excel
'Set objWorksheet = objWorkbook.Worksheets(1)
 Set objExcel = CreateObject("Excel.Application")
 Set objWorkbook = objExcel.Workbooks.Open _
   ("\\netko-sbs\Data\Imports\output.xlsx") 'Opslaan als..
 objExcel.Visible = True  'toon excel
 objExcel.DisplayAlerts = FALSE 'Foutmeldingen uitschakelen
 objExcel.Cells(1, 1).Value = "foldernaam" 'cellen naam geven
 objExcel.Cells(1, 2).Value = "Laatste import" 'cellen naam geven

 x = 2 'set de juiste rij in excel
 '===== 
 'opening without any connection info makes 
 'it "disconnected". 
 '=====  
 rs.open 
'===== 
'load it with file name, date, etc. 
'===== 
for each frigolanda in folder.SubFolders
if frigolanda = "frigolanda" then
set folderfrigo = fso.getfolder ("\\netko-sbs\data\imports\Frigolanda\")
set colFolders = folder.SubFolders
    end if
next
For each 



for each f in folder.SubFolders     
 rs.addnew array("foldername","moddate"), _ 
           array(f.name,f.datelastmodified) 
 rs.update 
 next 
s = "Sortering van Oud naar Nieuw:" & vbcrlf  _ 
  & "=============================" & vbcrlf 
if not (rs.bof and rs.eof) then 
   rs.sort = "moddate asc" 
   rs.movefirst 
   do until rs.eof 
  s = s & rs("foldername") & ";" _ 
      & rs("moddate") & vbcrlf 
    objExcel.Cells(x, 1).Value = _
        rs.Fields("foldername").Value 
    objExcel.Cells(x, 2).Value = _
        rs.Fields("moddate").Value 
        x = x + 1
    rs.movenext 
 loop 
end if 

objFile.WriteLine s 'Schrijf waarden naar Excel
Set rs = nothing 'Gooi RS leeg 
Set folder = nothing 'Object leegmaken
set fso = nothing 'Object leegmaken

Set objRange = objExcel.Range("A1") 'Selecteer actieve cell
objRange.Activate 'Activeer cell

Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit() 'Set grootte van kolom

Set objRange = objExcel.Range("B1") 'Selecteer actieve cell
objRange.Activate 'Activeer cell

Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit() 'Set grootte van kolom


ObjWorkbook.SaveAs "\\netko-sbs\Data\Imports\output.xlsx" 'Excel bestand opslaan
'objExcel.Quit 'Excel afsluiten als nodig is.
您可以使用WMI和InstanceCreationEvent类在创建文件或文件夹时收到通知。关于这个话题。它讨论了如何通知正在创建的子文件夹,而不是文件,但想法是一样的。不幸的是,您只能监视单个文件夹,而不能监视文件夹层次结构

因此,除了使用第三方工具外,我能想到的唯一解决方案是迭代文件夹结构并将文件列表存储在字典中。然后,每隔一段时间轮询文件夹,查看是否发生了任何更改

例如:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set d = CreateObject("Scripting.Dictionary")

' Initialize the file list...
DoFolder "\\netko-sbs\data\imports", True

' Now poll the folder every 10 seconds, looking for new files...
Do
    WScript.Sleep 10000
    DoFolder "\\netko-sbs\data\imports", False
Loop

' Recursive function...
Sub DoFolder(strFolder, bFirstTime)

    ' Add each file to our dictionary...
    For Each objFile In objFSO.GetFolder(strFolder).Files

        If bFirstTime Then

            ' Initializing. Just add every file we find...
            d.Add objFile.Path, ""

        Else

            ' Polling. Report any new files...
            If Not d.Exists(objFile.Path) Then

                ' New file found! Do something with it.

            End If

        End If

    Next

    ' Recursively check each subfolder...
    For Each objFolder In objFSO.GetFolder(strFolder).SubFolders
        DoFolder objFolder.Path
    Next

End Sub

此操作运行一次,以在文件夹层次结构中生成文件列表。然后,您可以在一段时间后再次运行它以检查是否有任何新文件。

您希望得到什么通知?在子文件夹中创建/修改/删除文件时?或创建/删除子文件夹时?上次修改文件夹时。这就是我的目标。如何修改文件夹?如果文件夹名称发生更改?是否添加、删除或更改了文件?是否添加或删除了子文件夹?只是想了解当添加文件时,您希望得到关于什么类型的修改的通知。基本上就是这样,这很有帮助。那么具体的子文件夹呢?。你怎么排除他们呢?我刚刚和老板谈过,他告诉我这看起来不错,但并不完全是我们想要实现的。我会写一篇新的文章,里面有全部的细节。