如何在VBScript中为GetFolder提供变量路径?

时间:2012-08-18 03:16:33

标签: vbscript

此脚本基本上转到文件夹并列出该文件夹中的所有文件,并将其输出到txt文件中。现在,而不是定义文件夹路径,我希望它使用包含一堆文件夹路径的txt文件,我想循环该txt文件。我怎么能这样做?

Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile

'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")

'Getting the Folder Object
Set ObjFolder = fso.GetFolder("C:\Users\Susan\Desktop\Anime\ova")

'Creating an Output File to write the File Names
Set ObjOutFile = fso.CreateTextFile("C:\Users\Susan\Documents\iMacros\Macros\WindowsFiles.txt")

'Getting the list of Files
Set ObjFiles = ObjFolder.Files

'Writing Name and Path of each File to Output File
For Each ObjFile In ObjFiles
    ObjOutFile.WriteLine(ObjFile.Path)
Next

ObjOutFile.Close

1 个答案:

答案 0 :(得分:1)

Ruriko,这应该是一个工作版本,我会添加一个检查,看看输入文件是否存在,我相信你可以自己做。

Dim fso, ObjFolder, ObjOutFile, ObjFiles, ObjFile, outputFile, inputFileList
Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = true

inputFileList = "list.txt"
outputFile = "C:\Users\Susan\Documents\iMacros\Macros\WindowsFiles.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objTextFile = fso.OpenTextFile(inputFileList, ForReading)

Do Until objTextFile.AtEndOfStream
 sFolderName = objTextFile.Readline
 wscript.Echo "writing contents of " & sFolderName
 writefilenames(sFolderName)
Loop

function writefilenames(sFolderName)
  Set ObjFolder = fso.GetFolder(sFolderName)

  If fso.FileExists(outputFile) Then
    Set ObjOutFile = fso.OpenTextFile(outputFile, ForAppending)
  Else
    Set ObjOutFile = fso.OpenTextFile(outputFile, ForWriting, CreateIfNeeded)
  End If

  Set ObjFiles = ObjFolder.Files

  For Each ObjFile In ObjFiles
    ObjOutFile.WriteLine(ObjFile.Path)
  Next

  ObjOutFile.Close
end function