how to extract the properties of all files and folders of a particular directory using vbscript?

StackOverflow https://stackoverflow.com/questions/17945886

  •  04-06-2022
  •  | 
  •  

Question

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.

The script is as follows:

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?

Error message is as follows:

line:15
character:12
error:permission denied
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top