MattsBits
MattsBits

MattsBits

List All Files In A Folder Using VBScript  

by Matt Hawkins, 19/01/2011
Categories : VBScript

If you need to create a customised file listing from a folder you can use the following VBScript. It outputs file information to a text file. This can be modified to create XML or CSV outputs.

You can choose which file attributes to include in the output.

The script follows these steps :

- Dimension FileSystemObject variables
- Dimension other variables
- Create FileSystemObject
- Define the path
- Get file collection
- Loop through collection of files
- Output attributes to text file

Dim fso, folder, files, OutputFile
Dim strPath

' Create a FileSystemObject  
Set fso = CreateObject("Scripting.FileSystemObject")

' Define folder we want to list files from
strPath = "mySubDirectory"

Set folder = fso.GetFolder(strPath)
Set files = folder.Files

' Create text file to output test data
Set OutputFile = fso.CreateTextFile("ScriptOutput.txt", True)

' Loop through each file  
For each item In files

  ' Output file properties to a text file
  OutputFile.WriteLine(item.Name)
  OutputFile.WriteLine(item.Attributes)
  OutputFile.WriteLine(item.DateCreated)
  OutputFile.WriteLine(item.DateLastAccessed)
  OutputFile.WriteLine(item.DateLastModified)
  OutputFile.WriteLine(item.Drive)
  OutputFile.WriteLine(item.Name)
  OutputFile.WriteLine(item.ParentFolder)
  OutputFile.WriteLine(item.Path)
  OutputFile.WriteLine(item.ShortName)
  OutputFile.WriteLine(item.ShortPath)
  OutputFile.WriteLine(item.Size)
  OutputFile.WriteLine(item.Type)   
  OutputFile.WriteLine("")
  
Next

' Close text file
OutputFile.Close




The following is a list of properties available for File objects :

Attributes Property
This property allows us to get or change the various attributes of a file.

Syntax: object.Atributes [ = newattributes]

DateCreated Property
This property gets the date and time that the file was created.

Syntax: object.DateCreated

DateLastAccessed Property
Gets the date and time that the file was last accessed.

Syntax: object.DateLastAccessed

DateLastModified Property
This property returns the date and time that the file was last modified.

Syntax: object.DateLastModified

Drive Property
Returns the drive letter of the drive where the file is located.

Syntax: object.Drive

Name Property
Lets us get or change the name of the specified file.

Syntax: object.Name [ = newname]

ParentFolder Property
This property gets the Folder object for the parent relating to the specified file.

Syntax: object.ParentFolder

Path Property
This property returns a file's path.

Syntax: object.Path

ShortName Property
Returns the short version of a filename (using the 8.3 convention).
e.g. Employees.html is truncated to Employ~1.htm

Syntax: object.ShortName

ShortPath Property
Returns the short version of the file path (this is the path with any folder and file names truncated as above).

Syntax: object.ShortPath

Size Property
Returns the size of a file in bytes.

Syntax: object.Size

TypeProperty
Returns a string containing the file type description.
e.g. For files ending in .TXT, "Text Document" is returned.

Syntax: object.Type

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2017