I am new to VB scripting, and I was trying to find a way to:
- Read a list of file names written as lines of text inside a particular text file.
- Check if those files exist in the same directory as the script.
- Write the name of the files that do not exist to an output file (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.
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.
Thank you for your help.
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:
filelist.txt {The file that will contain the listing of files to find}
file1.txt
file2.txt
file3.txt
file7.txt
Directory Listing {The folder you will be running the script from}
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
Our example
The filelist.txt
file contains file1.txt
file2.txt
file3.txt
file7.txt
.
The directory does not contain file3.txt
or file7.txt
.
Our expected output from this script based on these parameters is as follows:
missing.txt {The resultant output file that lists missing files}
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. :)