我正在尝试从运行脚本的文件夹中的视频文件获取扩展文件属性,并将结果输出到文本文件。
这是我到目前为止的代码:
Dim ls, fsObj, fd, fs, fl, sfs, sf, tf
' specify the file extensions to list
dim fileTypes
fileTypes = Array("mp4","mkv","avi")
On Error Resume Next
ls = ""
Set fsObj = CreateObject("Scripting.FileSystemObject")
Set fd = fsObj.GetFolder(".")
Set objFolder = objShell.NameSpace(fsObj)
set fs = fd.Files
For Each fileName in objFolder.Items
set objFolderItem = objFolder.ParseName(fileName)
size = objFolder.GetDetailsOf(objFolderItem, 1)
length = objFolder.GetDetailsOf(objFolderItem, 27)
height = objFolder.GetDetailsOf(objFolderItem, 283)
width = objFolder.GetDetailsOf(objFolderItem, 285)
Next
' list subfolders
set sfs = fd.SubFolders
For Each sf in sfs
ls = ls & sf.name & vbCrLf & chr(10)
Next
For Each fl in fs
' check whether the extension matches
if arrayContains(fileTypes, fsObj.GetExtensionName(fl.name))then
ls = ls & fl.name & vbtab & length & vbCrLf & chr(10)
end if
Next
Set tf = fsObj.OpenTextFile("index.txt", 2, True, False)
tf.Writeline ls
tf.Close
Set fsObj = Nothing
function arrayContains (arr, val)
dim found
found = false
for i = 0 to ubound(arr)
if arr(i) = val then
found = true
exit for
end if
next
arrayContains = found
end function
Run Code Online (Sandbox Code Playgroud)
一旦我可以添加其他文件,我就会尝试获取文件名和长度。
据我猜测,我需要指定 objFolder.ParseName(fileName)。如何指定与文件类型数组匹配的所有文件?我尝试过 fl.name 和 fd.files。我不知道还能尝试什么。
我通过在 Windows 7 中的 Windows 资源管理器中双击 vbs 文件来运行该脚本。
该脚本运行时没有错误并返回文件名,但不返回扩展属性。任何帮助将不胜感激。
以下代码能够获取 mp4 文件的名称、大小、长度、帧高度和帧宽度等详细信息。对于flv、avi、mkv格式的文件,只能提取其名称和大小。对于这些文件,即使右键单击它们,选择属性并转到“详细信息”选项卡,您也不会看到有关其长度、高度和宽度的信息。但对于 mp4,您可以看到所有这些细节。
试试这个代码:
Set fso = CreateObject("scripting.filesystemobject")
Set obs = CreateObject("shell.application")
Set fol = fso.GetFolder(".")
Set spl = obs.NameSpace(fol.Path)
Set files = fol.Files
filePath = fol.path&"\Info.txt"
set objFile = fso.openTextFile(filePath,2,true,true)
arr = Array("mp4", "mkv", "avi", "flv")
For Each file In files
ext = fso.GetExtensionName(file.Name)
For Each ex In arr
If StrComp(ext,ex,1)=0 Then
objFile.writeline "NAME: "&spl.GetDetailsOf(spl.ParseName(file.Name),0)&vbcrlf&_
"SIZE: "&spl.GetDetailsOf(spl.ParseName(file.Name),1)&vbcrlf&_
"LENGTH: "&spl.GetDetailsOf(spl.ParseName(file.Name),27)&vbcrlf&_
"FRAME HEIGHT: "&spl.GetDetailsOf(spl.ParseName(file.Name),283)&vbcrlf&_
"FRAME WIDTH: "&spl.GetDetailsOf(spl.ParseName(file.Name),285)&vbcrlf&string(50,"==")
Exit For
End If
Next
Next
objFile.Close
set objFile = Nothing
set files = nothing
set spl = nothing
set fol = nothing
set obs = nothing
set fso = nothing
Run Code Online (Sandbox Code Playgroud)