2012-10-24 42 views
2

早上好, 我一直在試圖將一個需要文件路徑和文件名的VBscript當腳本被摘錄時,來自用戶的內容可能具有通配符。然後,腳本將檢查指定目錄中與所提供的文件名匹配的文件,然後查看最後修改日期,以查看它是否在特定時間範圍內(即上午6點加上或減去5分鐘)被創建/修改。然後它會將所述文件複製到一個zip文件中。VBscript在一定的時間範圍內檢查文件是否存在(具有使用通配符的能力)

到目前爲止,我已經能夠獲得工作的論點,我有它設置抓取當前時間,看看文件夾中的文件和文件夾中匹配一個硬編碼文件名之一。這是我迄今爲止所做的。

currentTime = Now() 

filePath = Wscript.Arguments.Item(0) 
fileName = Wscript.Arguments.Item(1) 

Set fileSystem = CreateObject("Scripting.FileSystemObject") 
Set directory = fileSystem.GetFolder(filePath) 

For each file in directory.Files 
    If file.Name = fileName Then 
     Wscript.echo file.Name & " " & file.DateLastModified 
    end if 
Next 

我是一個VBscript noob,我期待着學習的方式!

CAP3

+0

[如何列出使用通配符在VBScript文件(http://www.source-code.biz/snippets/vbscript/1.htm)可能會有一些使用。 –

+0

在這裏有很多東西我不完全瞭解,但是這給了我一些想法和一些很好的閱讀:-)感謝您的信息,它應該幫助我更接近我的目標。 – Th3Cap3

回答

6

如果使用WMI,它支持通配符。

Dim strPath 

strFile = "*.*" 
If WScript.Arguments.Count > 1 Then 
    strPath = WScript.Arguments.Item(0) 
    strFile = WScript.Arguments.Item(1) 
Elseif WScript.Arguments.Count = 1 Then 
    strPath = WScript.Arguments.Item(0) 
Else 

End If 

Set objFso = CreateObject("Scripting.FileSystemObject") 
If Not objFso.FolderExists(strPath) Then 
    WScript.Echo "Folder path does not exist." 
    WScript.Quit 
Else 
    'Remove any trailing slash 
    If Right(strPath, 1) = "\" Then 
     strPath = Left(strPath, Len(strPath) - 1) 
    End If 
End If 
Set objFso = Nothing 

If Not IsNull(strPath) And strPath <> "" Then 
    strQuery = strPath & "\" & strFile 
Else 
    strQuery = strFile 
End If 

strQuery = Replace(strQuery, "*", "%") 
strQuery = Replace(strQuery, "?", "_") 

strQuery = Replace(strQuery, "\", "\\") 

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2") 

Set colFiles = objWMIService.ExecQuery _ 
    ("Select * From CIM_DataFile Where FileName Like '" & strQuery & "'") 

For Each objFile in colFiles 
    WScript.Echo "Access mask: " & objFile.AccessMask 
    WScript.Echo "Archive: " & objFile.Archive 
    WScript.Echo "Compressed: " & objFile.Compressed 
    WScript.Echo "Compression method: " & objFile.CompressionMethod 
    WScript.Echo "Creation date: " & objFile.CreationDate 
    WScript.Echo "Computer system name: " & objFile.CSName 
    WScript.Echo "Drive: " & objFile.Drive 
    WScript.Echo "8.3 file name: " & objFile.EightDotThreeFileName 
    WScript.Echo "Encrypted: " & objFile.Encrypted 
    WScript.Echo "Encryption method: " & objFile.EncryptionMethod 
    WScript.Echo "Extension: " & objFile.Extension 
    WScript.Echo "File name: " & objFile.FileName 
    WScript.Echo "File size: " & objFile.FileSize 
    WScript.Echo "File type: " & objFile.FileType 
    WScript.Echo "File system name: " & objFile.FSName 
    WScript.Echo "Hidden: " & objFile.Hidden 
    WScript.Echo "Last accessed: " & objFile.LastAccessed 
    WScript.Echo "Last modified: " & objFile.LastModified 
    WScript.Echo "Manufacturer: " & objFile.Manufacturer 
    WScript.Echo "Name: " & objFile.Name 
    WScript.Echo "Path: " & objFile.Path 
    WScript.Echo "Readable: " & objFile.Readable 
    WScript.Echo "System: " & objFile.System 
    WScript.Echo "Version: " & objFile.Version 
    WScript.Echo "Writeable: " & objFile.Writeable 
Next 

編輯..........

您可以使用WMI事件腳本與__InstanceCreationEvent在一個特定的文件夾來監視創建新文件。它看起來像這樣:

strSource = "C:\\somefilepath\\withdoubleshlashes" 
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2") 

Set colEvents = objWMIService.ExecNotificationQuery _ 
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _ 
     & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _ 
     & "TargetInstance.GroupComponent= " _ 
     & "'Win32_Directory.Name=""" & strSource & """'") 

Do While True 
    Set objEvent = colEvents.NextEvent() 
    copyFile(objEvent.TargetInstance.PartComponent) 
Loop 

更全面的解釋,你可以閱讀我的博客Monitoring and Archiving Newly Created Files

+0

當您使用WMI服務時,它使您可以將其連接到[事件接收器](http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa393013.aspx)。現在你可以處理真正基於事件的事件。 – AutomatedChaos

+0

在這裏使用事件接收器將是首選方法(因爲它使用的開銷少得多),但是,OP沒有提供足夠的信息來利用事件接收器。儘管聽起來好像在一天的特定時間段內使用__InstanceCreationEvent監視特定文件夾可能會完成預期的任務。 – Nilpo

+0

這_InstanceCreationEvent會監視在特定位置創建文件嗎?如果這是它的作用,那可能是非常有用的。我希望我的帖子能夠更加精確,但是我對這個請求沒有給出太多的細節,我仍然在等待更多關於這些文件的命名方式,進入時間,是否刪除等信息。 ..你能展開InstanceCreationEvent嗎? – Th3Cap3

0

對於你的榜樣,要做到這一點最簡單的方法是使用INSTR(在字符串)功能。我發現它在99%的通配符任務中有效。所以,在你的榜樣的,而不是使用:

If file.Name = fileName Then 

使用:

If inStr(file.Name, filename) Then 

這實際上並沒有允許使用通配符(*),因爲它不會找到一個匹配(帶星號參數),所以你需要從字符串中去除通配符,並沒有取代它(或者只訓練用戶不能使用通配符):

Replace(filename,"*", "") 

然而,InStr函數確實允許部分或全部匹配它使得它適用於大多數通配符任務。因此,如果你的文件名是pic.jpg,無論是用戶搜索:

PIC或JPG或P或C或PI等

它會返回一個匹配。請記住,instr函數返回一個數字,其中匹配顯示在字符串中。因此,如果它不創建匹配,結果將爲0.我遇到的例子NOT不起作用,或者我需要使用在這種情況下的完整語法:

If inStr(file.Name, filename)<>0 Then 
1

本答案使用正則表達式。爲了使它工作,它將你的模式格式改寫爲正則表達式格式。例如*.txt將變成^.*[.]txt$

strPath = "C:\Temp" 
strFile = "*.txt" 
startTime = 555 
endTime = 605 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set folder = fso.GetFolder(strPath) 
Set files = folder.Files 

Set re = New RegExp 
re.IgnoreCase = true 
re.Pattern = "^" + Replace(Replace(strFile, ".", "[.]"), "*", ".*") + "$" 

For Each f in Files 
    Set matches = re.Execute(f.Name) 
    If matches.Count > 0 Then 
    HM = Hour(f.DateLastAccessed) * 100 + Minute(f.DateLastAccessed) 
    If HM >= startTime And HM <= endTime Then 
     WScript.Echo f.Name, f.DateLastAccessed 
    End If 
    End If 
Next 

參考:

相關問題