Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compare files from 2 txt files with paths and filenames?

Status
Not open for further replies.

geofflsc

Technical User
Dec 19, 2012
9
CA
Hi all,

I have some differculty on writing a vbscript to do this. I have a script to create 2 txt files as follow,

For example:
In "server.txt", I have full paths and filenames,
\\server\ab\cd\ed\pp\pm\file1.xml
\\server\ab\cd\ed\pp\pm\file2.xml
....

In "client.txt", I have full paths and filenames,
\\client-01\c$\ab\pm\file1.xml
\\client-01\c$\ab\pm\file2.xml
....

The question is I need a vbscript to compare the files within the txt files if any change has been made. The following chart is my logic to read every line from client.txt and look for it from server.txt and compare the size and send email if any mismatch. Any help would be greatly appreciated.

Client.txt Server.txt
\\..\..\file1.xml >>> \\..\..\file1.xml
\\..\..\file2.xml
....so on....

\\..\..\file2.xml >>> \\..\..\file1.xml
\\..\..\file2.xml
....so on....

\\..\..\file3.xml >>> \\..\..\file1.xml
....so on....
 
So server.txt and client.txt have the same number of lines? And the 1st line of server.txt is a file that corresponds to the file in the 1st line of clients.txt? And the 2nd line of server.txt corresponds to the 2nd line of clients.txt, etc etc? If so, something like this should work (not tested)

Code:
Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim f, arrServerFiles, arrClientFiles, x

Set f = fso.OpenTextFile("server.txt", ForReading)
arrServerFiles = Split(f.ReadAll, vbCrLf)
Set f = fso.OpenTextFile("client.txt", ForReading)
arrClientFiles = Split(f.ReadAll, vbCrLf)

For x = LBound(arrClientFiles) To UBound(arrClientFiles)
   If fso.GetFile(arrClientFiles(x)).Size <> fso.GetFile(arrServerFiles(x)).Size Then 
      'sizes do not match
   End If
Next
 
Thanks guitarzan for your quick response, your work did it's job.

Unfortunately, the server.txt and client.txt are not corresponding to each other. I need to search every line in client.txt and find a match on every line in server.txt. There are 3 conditions I need to be known.

1. If file not exist in server.txt, something is wrong and email immediately.
2. If file exist in server.txt and it's newer, email immdeiately.
3. If file exist in server.txt and it's older, overwrite

I know this is complicated, that's why I'm kind of stuck. That will be great if you can figure it out. Thanks in advance.
 
Something like this should do it... not tested at all.
Code:
Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim f, arrServerFiles, arrClientFiles, x

Set f = fso.OpenTextFile("server.txt", ForReading)
arrServerFiles = Split(f.ReadAll, vbCrLf)
Set f = fso.OpenTextFile("client.txt", ForReading)
arrClientFiles = Split(f.ReadAll, vbCrLf)

Dim bFound, sClientFileName, sServerFileName
For x = LBound(arrClientFiles) To UBound(arrClientFiles)
   bFound = False
   sClientFileName = Mid(arrClientFiles(x), InStrRev("\", arrClientFiles(x)) + 1)
   For y = LBound(arrServerFiles) To UBound(arrServerFiles)
      sServerFileName = Mid(arrServerFiles(x), InStrRev("\", arrServerFiles(x)) + 1)
      
      If LCase(sClientFileName) = LCase(sServerFileName) Then
         bFound = True
         If fso.GetFile(arrClientFiles(x)).DateLastModified <= fso.GetFile(arrServerFiles(x)).DateLastModified Then 
            'server file date is newer or the same as client... send an email
         Else
            'server file date is older than client... copy file
         End If
      End If
   Next
   If Not bFound Then
      'file not found in server, send email
   End If
Next
 
Hi guitarzan,

First of all, I wish you a happy new year. I tried your latest codes, however, it gives me an error on Ln 22, Char 10, Invalid procedure call or argument, Code: 800A0005. Would you be able to debug it, please? I have no idea why it's causing the error. Thank you.


Code:
If fso.GetFile(arrClientFiles(x)).DateLastModified <= fso.GetFile(arrServerFiles(x)).DateLastModified Then
 
I'd replace all occurences of:
arrServerFiles(x)
with:
arrServerFiles(y)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, I just noticed the error that PHV caught. Also, it would be a good idea to check that the client and server files actually exist, using the FileExists method.
Code:
Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim f, arrServerFiles, arrClientFiles, x[highlight #FCE94F], y[/highlight]

Set f = fso.OpenTextFile("server.txt", ForReading)
arrServerFiles = Split(f.ReadAll, vbCrLf)
Set f = fso.OpenTextFile("client.txt", ForReading)
arrClientFiles = Split(f.ReadAll, vbCrLf)

Dim bFound, sClientFileName, sServerFileName
For x = LBound(arrClientFiles) To UBound(arrClientFiles)
[COLOR=blue]   If Not fso.FileExists(arrClientFiles(x))
      'client file doesn't exist.. do something?
   Else[/color] 
      bFound = False
      sClientFileName = Mid(arrClientFiles(x), InStrRev("\", arrClientFiles(x)) + 1)
      For y = LBound(arrServerFiles) To UBound(arrServerFiles)
         sServerFileName = Mid([highlight #FCE94F]arrServerFiles(y)[/highlight], InStrRev("\",[highlight #FCE94F] arrServerFiles(y)[/highlight]) + 1)

         If[COLOR=blue] fso.FileExists(arrServerFiles(y))[/color] And (LCase(sClientFileName) = LCase(sServerFileName)) Then
            bFound = True
            If fso.GetFile(arrClientFiles(x)).DateLastModified <= fso.GetFile([highlight #FCE94F]arrServerFiles(y)[/highlight]).DateLastModified Then 
               'server file date is newer or the same as client... send an email
            Else
               'server file date is older than client... copy file
            End If
         End If
      Next
   End If
   
   If Not bFound Then
      'file not found in server, send email
   End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top