问题
I have a folder containing few files. I would like to get, for each file, a list of all the properties assigned to this file and their values.
I have written these few lines but I am not able to identify the right methods to use in the placeholders MethodIamLookingFor1 (to get list of properties assigned), MethodIamLookingFor2 (to get property name) and MethodIamLookingFor3 (to get property value):
Dim sFolder
sFolder = "C:\Batch_DEV\to"
Dim objFSO, objDir, listObjFiles, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDir = objFSO.GetFolder(sFolder)
Set listObjFiles = objDir.Files
For Each objFile In listObjFiles
Wscript.Echo objFile.name & ": "
listProperties = objFile.MethodIamLookingFor1
For Each objFileProperty In listProperties
Wscript.Echo "property name is: " & objFileProperty.MethodIamLookingFor2
Wscript.Echo "property value is: " & objFileProperty.MethodIamLookingFor3
Next
Next
Could you please help? thanks!
回答1:
Code from Using VBA to get extended file attributes
ignore the VBA keyword as it can be converted to VBScript with little effort. See below.
Dim fso
Dim txtStream
Dim sFile
Dim oShell
Set oShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oDir
Set oDir = oShell.Namespace(pathToFolder)
Set txtStream = fso.OpenTextFile(pathToTextFile, 2, True, -2)
For Each sFile In oDir.Items
For i = 0 To 40
txtStream.WriteLine i & " : " & oDir.GetDetailsOf(oDir.Items, i) & " : " & oDir.GetDetailsOf(sFile, i)
Next
Next
回答2:
I don't recall VBScript as having a property that can be used to enumerate file properties. VB6 contained the FileInfo object that would have allowed for that.
Using VBScript you will need to specify each property you want. The files properties are listed on MSDN.
回答3:
thanks, I have used the GetDetailsOf instruction you indicated and adapted my code to use https://technet.microsoft.com/en-us/library/ee176615.aspx inputs
Dim sFolder
sFolder = "C:\Batch_DEV\to"
Set objShell = CreateObject("Shell.Application")
Set objDir = objShell.Namespace(sFolder)
For Each strFileName in objDir.Items
Wscript.Echo objDir.GetDetailsOf(strFileName, 0) & ":"
For i = 0 To 10
Wscript.Echo vbtab & "property name is: " & objDir.GetDetailsOf(objDir.Items, i)
Wscript.Echo vbtab & "property value is: " & objDir.GetDetailsOf(strFileName, i)
Next
Next
so at the end the methods are:
- MethodIamLookingFor1 ==> GetDetailsOf(objDir.Items, i)
- MethodIamLookingFor2 ==> GetDetailsOf(objDir.Items, i)
- MethodIamLookingFor3 ==> GetDetailsOf(strFileName, i)
I have added a vbtab to enhane the output and here is the result:
test.txt: property name is: Nome property value is: test.txt property name is: Dimensione property value is: 351 byte property name is: Tipo elemento property value is: File TXT property name is: Ultima modifica property value is: 23/12/2015 14:34 property name is: Data creazione property value is: 29/12/2015 09:30 property name is: Data ultimo accesso property value is: 29/12/2015 09:30 word sample.docx: property name is: Nome property value is: word sample.docx property name is: Dimensione property value is: 11,1 KB property name is: Tipo elemento property value is: Documento di Microsoft Word property name is: Ultima modifica property value is: 10/12/2015 16:24 property name is: Data creazione property value is: 29/12/2015 09:31 property name is: Data ultimo accesso property value is: 29/12/2015 09:31
Many thanks for the support!
来源:https://stackoverflow.com/questions/34514580/get-document-properties-using-vbs