VBScript中读取TXT文件和不存在的目录列表中的多个文件名 [英] VBScript read multiple filenames from txt file and list which do not exist in directory

查看:199
本文介绍了VBScript中读取TXT文件和不存在的目录列表中的多个文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的VB脚本,我试图找到一种方法:

I am new to VB scripting, and I was trying to find a way to:


  1. 读取写为一个特定文本文件中的文本行的文件名的列表。

  2. 检查是否在同一目录中的脚本存在的那些文件。

  3. 写的文件不存在输出文件(missing.txt)
  4. 的名称

从我的研究,似乎一个好方法是使用FSO和读取整个文本文件,创建一个数组,然后通过循环来检查文件是否存在于目录中,然后登录到missing.txt 如果它没有。

From my research, it seems that a good approach would be to use FSO and read the entire text file, creating an array, then looping through to check if the file exists in the directory, and then logging to "missing.txt" if it doesn't.

不过,做这种方式,就不会出现是一个类型不匹配,因为在.TXT文件名是字符串并在该目录本身是对象的文件?

However, doing it this way, wouldn't there be a type mismatch because the file name in the .txt is a string and the files in the directory themselves are objects?

我如何比较呢?我在概念上理解这一点,但我不知道从哪里开始的语法。

How would I compare that? I can understand this conceptually, but I have no idea where to start with the syntax.

感谢您的帮助。

推荐答案

正如你说,你是新来的VBScript,你似乎是新的堆栈溢出,我想我会尽量为您提供一些帮助。

As you have stated that you are new to vbscript, and you appear to be new to Stack Overflow, I thought I would try to offer you some assistance.

这是没有必要为你存储整个文件作为数组。所有你需要做的是处理由线作为文本输入文件行:检查每行文本的存在是为文件名

It is not necessary for you to store the entire file as an array. All you need to do is process your input file line by line as text: check if each line of text exists as a filename.

让我们假设我们的示例的以下细节:

Let's assume the following details for our example:

file1.txt
file2.txt
file3.txt
file7.txt


目录列表   {文件夹,你将运行从脚本}

 Directory of C:\lazy-code-handed-over-on-silver-plate\files

 06/06/6666  06:66 PM    <DIR>          .
 06/06/6666  06:66 PM    <DIR>          ..
 06/06/6666  06:66 PM               666 file1.txt
 06/06/6666  06:66 PM               666 file2.txt
 06/06/6666  06:66 PM               666 file4.txt
 06/06/6666  06:66 PM               666 file5.txt
               4 File(s)           2664 bytes
               2 Dir(s)   6,666,666,666,666 bytes free


我们的例子

Filelist.txt中文件包含 FILE1.TXT FILE2.TXT file3.txt file7.txt

该目录的包含 file3.txt file7.txt

我们的距离根据这些参数这个脚本预期成果是如下:

Our expected output from this script based on these parameters is as follows:

file3.txt
file7.txt


脚本

下面是一个脚本来帮助你实现这个(我已经添加了注释到每个相关行右一):


The Script

Here's a script to help you achieve this (I have added comments off to the far right of each relevant line):

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                                                                 ' ..

我希望这可以帮助你。祝你今天愉快。 :)

I hope this helps you. Have a nice day. :)

这篇关于VBScript中读取TXT文件和不存在的目录列表中的多个文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆