0

该脚本基本上会转到一个文件夹并列出该文件夹中的所有文件并将其输出为 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
4

1 回答 1

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
于 2012-08-18T12:00:06.370 回答