File It Under Files

上一篇 / 下一篇  2010-03-31 00:04:32 / 个人分类:VBScript

In your first file system script, ListFiles.vbs, connect to FileSystemObject, attach it to a folder defined by the variable FolderPath, and then use the Files command to enable the For Each loop to echo out each file in the folder. This is just the beginning of what can be done with this script.

 

Table 6-1. Variables used in ListFiles.vbs

Variable name

Use

FolderPath

Defines the folder to be enumerated in the script

objFSO

Creates FileSystemObject

objFolder

Holds the connection to the folder whose path is stored in the FolderPath variable. The connection is returned by the GetFolder method of FileSystemObject

colFiles

Holds the collection of files returned by using the Files method

objFile

Holds individual files as the script. iterates through the collection of files by using the For Each construction

 

 

ListFiles.vbs

Option Explicit
On Error Resume Next
Dim FolderPath        'path to the folder to be searched for files
Dim objFSO            'the FileSystemObject
Dim objFolder         'the folder object
Dim colFiles          'collection of files from files method
Dim objFile           'individual file object

FolderPath = "c:\fso"
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFolder = objFSO.GetFolder(FolderPath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
  WScript.Echo objFile.Name, objFile.Size & " bytes"
  WScript.Echo VbTab & "created: " & objFile.DateCreated
  WScript.Echo VbTab & "modified: " & objFile.DateLastModified
Next
 
 
 
one function to offer the objpath
Sub subGetFolder
Dim objShell, objFolder, objFolderItem, objPath
Const windowHandle = 0
Const folderOnly = 0
const folderAndFiles = &H4000&

Set bjShell = CreateObject("Shell.Application")
Set bjFOlder = objShell.BrowseForFolder(windowHandle, _
   "Select a folder:", folderOnly)
Set bjFolderItem = objFolder.Self
bjPath = objFolderItem.Path
End Sub

TAG:

 

评分:0

我来说两句

Open Toolbar