Vbscript读取带有文件名的文本文件,然后搜索该文件并移动另一个文件

时间:2015-04-14 16:14:46

标签: vbscript

我找到了这段代码:

Option Explicit                                                               ' .. Just coz.
Const forReading = 1                                                          ' Set our constants for later.
Const forWriting = 2                                                          ' ....

Dim inputFile, outputFile, fso, fileList, logFile, fileSpec                   ' Dimension our variables

inputFile = "filelist.txt"                                                    ' Our input file
outputFile = "missing.txt"                                                    ' Our output file

Set fso = CreateObject("Scripting.FileSystemObject")                          ' Set up fso
Set fileList = fso.OpenTextFile(inputFile, forReading)                        ' Open our input file for reading
If Not (fso.FileExists(outputFile)) Then fso.CreateTextFile(outputfile)       ' Create output file if it doesn't exist
Set logFile = fso.OpenTextFile(outputFile, forWriting)                        ' Open up our output file for writing later

Do while not fileList.AtEndOfStream                                           ' While we have lines to process do this loop
    fileSpec = fileList.ReadLine()                                            ' Read in line of text as variable fileSpec
    If Not (fso.FileExists(fileSpec)) Then                                    ' If it doesnt exist ....
        logFile.writeline (fileSpec)                                          ' ....Write it out to the output file
    End If 
Loop

fileList.close                                                                ' Clean up
logFile.close   

Here是对该代码的解释。

我还需要一件事。我需要将默认目录中的额外文件(它们不是在filelist.txt中写入)移动到新目录中。我在默认目录中只需要写入filelist.txt的文件。我不完全理解这些代码。我试着重新编写代码,但每次都失败了。

1 个答案:

答案 0 :(得分:0)

设置问题(联合,差异,......)可以通过VBScript中的Dictionary来解决。

您有两组文件名,一组来自源文件(SetF),另一组来自默认目录(SetD)中的文件。在现有的SetF循环中,将名称存储在字典(DicF)中。然后在SetD(DefFolder.Files)上添加一个循环并移动DicF中不存在的文件 - 如下所示:

>> SetF = Split("A D F G")
>> SetD = Split("B D G H")
>> Set DicF = CreateObject("Scripting.Dictionary")
>> For Each f in SetF
>>     DicF(f) = 0
>> Next
>> For Each f In SetD
>>     If Not DicF.Exists(f) Then
>>        WScript.Echo f
>>     End If
>> Next
>>
B
H

(参见Compare arrays