如何使用vbscript提取特定目录的所有文件和文件夹的属性? [英] how to extract the properties of all files and folders of a particular directory using vbscript?
问题描述
我已经能够提取文件夹中所有文件的信息以及该特定文件夹的所有子文件夹,但是当我想提取所有文件的信息(即文件类型,文件路径,文件大小和文件名)时,特定目录的文件和文件夹,我无法执行此操作.它说被拒绝".
I have been able to extract information of of all files in a folder and also all subfolders of that particular folder, but when I want to extract information (i.e. file type, file path, file size and file name) of all the files and folders of a particular DIRECTORY, I am not able to do it. It says PERMISSION DENIED.
脚本如下:
Option Explicit
Dim objFSO,objf,objfolder, objFile, strFileProperties, strFiles,OBJFLD,objfile2
dim objf1,objfile1,objtextfile,strfolderproperties,objsubfld,objfl,objfl1,strfileproperties2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objf= objFSO.Getfolder("C:\")
set OBJFLD=objf.subfolders
for each objfolder in OBJFLD
strFolderproperties =_
strFolderproperties & "Number of files: " & objFolder.Files.Count & VbCrLf
set objfile2=objfolder.files
for each objfile in objfile2
' Display generel file properties of every subfld
strFileProperties = strFileProperties & "File name: " & objFile.Name & VbCrLf
strFileProperties = strFileProperties & "File path: " & objFile.Path & VbCrLf
strFileProperties = strFileProperties & "File size: " & objFile.Size & " bytes" & VbCrLf
strFileProperties = strFileProperties & "File type: " & objFile.Type & VbCrLf & vbcrlf
next
next
Set objf= objFSO.Getfolder("C:\")
set OBJfl=objf.files
for each objfl1 in OBJfl
'display properties of the files of the main folder
strFileProperties2 = strFileProperties2 & "File name: " & objfl1.Name & VbCrLf
strFileProperties2 = strFileProperties2 & "File path: " & objFl1.Path & VbCrLf
strFileProperties2 = strFileProperties2 & "File size: " & objFl1.Size & " bytes" & VbCrLf
strFileProperties2 = strFileProperties2 & "File type: " & objFl1.Type & VbCrLf & vbcrlf
next
set objf1=objfso.getfolder("E:\logs3")
set objfile1=objfso.getfile( "E:\logs3\database.txt")
set objf1=nothing
set objfile1=nothing
set objtextfile=objfso.opentextfile("E:\logs3\database.txt",8,true)
objtextfile.writeline(strfileproperties)
objtextfile.writeline(strfileproperties2)
objtextfile.close
set objf1=objfso.getfolder("E:\logs3")
set objfile1=objfso.getfile( "E:\logs3\database.txt")
set objf1=nothing
set objfile1=nothing
set objsubfld=objfso.opentextfile("E:\logs3\database.txt",8,true)
objsubfld.writeline(strfolderproperties)
objsubfld.close
注意:当我用它来提取除整个目录之外的任何文件夹的文件信息时,此脚本才起作用!
Note: This script works when I use it to extract info of files of any folder but the whole directory!
有解决方案吗?是否可以获取目录中所有文件和文件夹的信息?
Any solutions? Is it possible to get info of all files and folders of the directory?
错误消息如下:
line:15
character:12
error:permission denied
推荐答案
您需要访问对象才能读取其属性. Windows系统上有几个文件夹,甚至管理员也没有访问权限.您无法枚举这些文件夹或其中的文件/子文件夹的属性.最好的办法是检测您是否具有访问权限,否则跳过(子)文件夹:
You need access to an object to read its properties. There are several folders on Windows Systems, that not even administrators have access to. You can't enumerate the properties of those folders or the files/subfolders inside them. The best you can do is detect whether you have access and skip the (sub)folder otherwise:
For Each objfolder In objFSO.GetFolder("C:\").SubFolders
skip = False
On Error Resume Next
count = objFolder.Files.Count
If Err Then skip = True
On Error Goto 0
If Not skip Then
For Each objfile In objfolder.Files
...
Next
End If
...
Next
这篇关于如何使用vbscript提取特定目录的所有文件和文件夹的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!