2016-05-23 52 views
1

我將此與HP Operations Manager一起使用,它使用PARAMETER部分,因此您可以從HPOM策略中定義變量。這目前適用於硬編碼的一個文件,但我希望能夠使用PARAMETER來設置文件名,所以腳本是通用的。通過使用帶有.Files的變量設置文件對象

這是做什麼檢查是否存在一個特定的文件,並設置一個變量(Rule.Status = True),如果它比在FileAge變量中指定的某個數量的分鐘更早。

現在我使用:

Set MonitorFile = MonitorFolder.Files("EDI.001") 

這工作正常。但是,當我嘗試做:

Set MonitorFile = MonitorFolder.Files(FileName) 

它失敗,以下錯誤:

Invalid procedure call or argument.

難道我做錯了什麼?在這種情況下使用變量是否有更好的方法?

這裏是整個腳本:

'PARAMETERS START 

'PARAMETER FolderName STRING DEFAULT "D:\RFInput\InBoxPO" VALUE "D:\RFInput\InBoxPO\" SESSION 
Dim FolderName 
FolderName = "D:\RFInput\InBoxPO\" 
Session("FolderName") = FolderName 

'PARAMETER FileAge INT DEFAULT "60" VALUE "1" SESSION 
Dim FileAge 
FileAge = 1 
Session("FileAge") = FileAge 

'PARAMETER FolderDisplayName STRING DEFAULT "InBoxPO" VALUE "InBoxPO" SESSION 
Dim FolderDisplayName 
FolderDisplayName = "InBoxPO" 
Session("FolderDisplayName") = FolderDisplayName 

'PARAMETER FileName STRING DEFAULT "EDI.001" VALUE "EDI.001" SESSION 
Dim FileName 
FileName = "EDI.001" 
Session("FileName") = FileName 

'PARAMETERS END 

Dim fs, MonitorFolder, MonitorFile, objShell, MinutesOld 
Dim objFile, listNames 
' Set constants for working with files 
Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Set objShell = CreateObject("Shell.Application") 
Set fs = CreateObject("Scripting.FileSystemObject") 
Set MonitorFolder = fs.GetFolder(FolderName) 

Rule.Status = False 

For Each objFile In MonitorFolder.Files 
    listNames = objFile.Name 
    If InStr(listNames, FileName) = 1 Then 
     Set MonitorFile = MonitorFolder.Files("EDI.001") 
     MinutesOld = DateDiff("n", MonitorFile.DateLastModified, Now) 
     If MinutesOld > FileAge Then 
      'Turn on for debugging - Wscript.Echo FileName & " is older than " & FileAge & " minutes in folder " & FolderName & "." 
      Rule.Status = True 
     End If 
    End If 
Next 
Set objShell = Nothing 
Set fs = Nothing 
Set MonitorFolder = Nothing 
'END OF SCRIPT 

回答

1

事實上訪問特定項目Files集合中,似乎只與字符串文字工作,不知道這是爲什麼。

可以簡化For Each循環,雖然:

For Each objFile In MonitorFolder.Files 
    If LCase(objFile.name) = LCase(FileName) Then 
     Set MonitorFile = objFile 
     ... 
    End If 
Next 

如果通過文件名需要查詢,你可以建立一個dictionary這樣的:

Set filenames = CreateObject("Scripting.Dictionary") 
For Each objFile In MonitorFolder.Files 
    filenames.Add objFile.Name, objFile 
Next 

這將允許您訪問文件這樣的名字:

Set MonitorFile = filenames(FileName) 
+0

感謝您的提示!我很高興我不會發瘋 –