gdnmarc
Goto Top

VBScript um Dateien zu vergleichen (fast) fertig

Hallo Forum,

ich bin leider ein Anfänger und mir fehlt eine Kleinigkeit in meinem Script. Ich möchte zwei Dateien vergleichen und veränderte Zeilen in eine neue Datei ausgeben. Das Script funktioniert aber nur wenn ich die beiden oberen Zeilen zur Variablendeklaration deaktivere, aber warum?

VIELEN DANK!!!

Marc


'Option Explicit
'Dim objFSO, strlastapps, strqueryapps, strFilePath, objF1, objF2, objL1, objL2, WshShell

Const ForWriting = 2
Const OpenAsASCII = 0
Const CreateIfNotExist = True

strlastapps = "lastapps.txt"
strqueryapps = "queryapps.txt"
strFilePath = "newapps.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objF1 = objFSO.OpenTextFile(strlastapps)
Set objF2 = objFSO.OpenTextFile(strqueryapps)
Set objL1 = CreateObject("Scripting.Dictionary")
objL1.CompareMode = vbTextCompare
Set objL2 = CreateObject("Scripting.Dictionary")
objL2.CompareMode = vbTextCompare

' Open the file for write access.
On Error Resume Next
Set objFile = objFSO.OpenTextFile(strFilePath, _
ForWriting, CreateIfNotExist, OpenAsASCII)

' Read first file adding unique value to dictionary object.
Do Until objF1.AtEndOfStream
strV = objF1.ReadLine
If (objL1.Exists(strV) = False) Then
objL1.Add strV, True
End If
Loop
objF1.Close

' Read the second file.
Do Until objF2.AtEndOfStream
strV = objF2.ReadLine
If (objL1.Exists(strV) = False) And (objL2.Exists(strV) = False) Then
objFile.WriteLine strV
End If
' Remove duplicates.
If (objL1.Exists(strV) = True) Then
objL1.Remove strV
End If
' Add unique values to 2nd dictionary object.
If (objL2.Exists(strV) = False) Then
objL2.Add strV, True
End If
Loop
objF2.Close
objFile.Close

Content-Key: 91389

Url: https://administrator.de/contentid/91389

Printed on: April 26, 2024 at 05:04 o'clock

Member: bastla
bastla Jul 06, 2008 at 12:53:21 (UTC)
Goto Top
Hallo gdnmarc und willkommen im Forum!

Vielleicht wird's besser, wenn Du auch "objFile" und "strV" deklarierst.

Grüße
bastla

P.S.: Für das Posten von Scripts gibt es hier -Tags ...
Member: gdnmarc
gdnmarc Jul 06, 2008 at 13:32:52 (UTC)
Goto Top
Prima!!! BESTEN DANK das funktioniert jetzt, ich habe jetzt inzwischen noch ein anderes Script erstellt welches auch funktioniert, füge ich es aber zu einem anderen Script hinzu, bleibt die auszugebende Datei immer leer. Führe ich die beiden Scripte separat aus funktioniert alles. Vielleicht habt ihr noch eine Idee? Was genau mache ich falsch, alleine funktionieren odch beide Scripte??? Es wird auch kein Fehler ausgegen, sondern das zu erstellende File newapss.log wird zwar erstellt, bleibt aber einfach leer....

Script1:
Option Explicit
Dim objFSO, strlastapps, strqueryapps, strnewapps, WshShell

strlastapps = "LastApps.log"  
strqueryapps = "QueryApps.log"  
strnewapps = "NewApps.log"  

'LastApps  

Const OverwriteExisting = True

Set objFSO = CreateObject("Scripting.FileSystemObject")  

If not objFSO.FileExists(strlastapps) Then
   objFSO.CreateTextFile strlastapps 
End If

If not objFSO.FileExists(strqueryapps) Then
   objFSO.CreateTextFile strqueryapps 
Else
   objFSO.CopyFile strqueryapps, strlastapps, OverwriteExisting
   objFSO.DeleteFile strqueryapps
   objFSO.CreateTextFile strqueryapps
End If

'QueryApps  

Set WshShell = WScript.CreateObject("WScript.Shell")  
WshShell.Run "sftmime query obj:app /global /short /LOG:QueryApps.log"  

Script2:
Option Explicit
Dim objFSO, objFile1, objFile2, objFile3, strqueryappsDevices, strAddress, strNotqueryapps

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFile1 = objFSO.OpenTextFile("LastApps.log", ForReading)  

strqueryappsDevices = objFile1.ReadAll
objFile1.Close

Set objFile2 = objFSO.OpenTextFile("QueryApps.log", ForReading)  

Do Until objFile2.AtEndOfStream
    strAddress = objFile2.ReadLine
    If InStr(strqueryappsDevices, strAddress) = 0 Then
        strNotqueryapps = strNotqueryapps & strAddress & vbCrLf
    End If
Loop

objFile2.Close

'Wscript.Echo "New Apps: " & vbCrLf & strNotqueryapps  

Set objFile3 = objFSO.CreateTextFile("NewApps.log")  

objFile3.WriteLine strNotqueryapps
objFile3.Close

Script3 (Script 1+2 zusammen):
Option Explicit
Dim objFSO, strlastapps, strqueryapps, WshShell
Dim objFile1, objFile2, objFile3, strqueryappsDevices, strAddress, strNotqueryapps

strlastapps = "LastApps.log"  
strqueryapps = "QueryApps.log"  


'LastApps  

Const OverwriteExisting = True

Set objFSO = CreateObject("Scripting.FileSystemObject")  

If not objFSO.FileExists(strlastapps) Then
   objFSO.CreateTextFile strlastapps 
End If

If not objFSO.FileExists(strqueryapps) Then
   objFSO.CreateTextFile strqueryapps 
Else
   objFSO.CopyFile strqueryapps, strlastapps, OverwriteExisting
   objFSO.DeleteFile strqueryapps
   objFSO.CreateTextFile strqueryapps
End If

'QueryApps  

Set WshShell = WScript.CreateObject("WScript.Shell")  
WshShell.Run "sftmime query obj:app /global /short /LOG:QueryApps.log"  

'NewApps  

Const ForReading = 1

'Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFile1 = objFSO.OpenTextFile("LastApps.log", ForReading)  

strqueryappsDevices = objFile1.ReadAll
objFile1.Close

Set objFile2 = objFSO.OpenTextFile("QueryApps.log", ForReading)  

Do Until objFile2.AtEndOfStream
    strAddress = objFile2.ReadLine
    If InStr(strqueryappsDevices, strAddress) = 0 Then
        strNotqueryapps = strNotqueryapps & strAddress & vbCrLf
    End If
Loop

objFile2.Close

'Wscript.Echo "New Apps: " & vbCrLf & strNotqueryapps  

Set objFile3 = objFSO.CreateTextFile("NewApps.log")  

objFile3.WriteLine strNotqueryapps
objFile3.Close
Member: bastla
bastla Jul 06, 2008 at 15:46:39 (UTC)
Goto Top
Hallo gdnmark!

Vorweg: Bei einem Test mit 2 geringfügig unterschiedlichen Textdateien "LastApps.log" und "QueryApps.log" und deaktivierten Zeilen 22-24 sowie 30 erhalte ich das korrekte Ergebnis: jene Zeilen, welche in "QueryApps.log", nicht aber in "LastApps.log" aufscheinen - demzufolge müsste der Fehler eigentlich beim Erzeugen der neuen "QueryApps.log" per Shell-Aufruf (den ich hier nicht nachbauen kann) liegen (oder, es gibt eben keine Unterschiede zwischen den beiden Dateien face-wink).
Was mir noch so an Kleinigkeiten aufgefallen ist:

Du erstellst zwar Variable für die Dateinamen, verwendest diese aber dann nicht (eine Folge des Zusammenkopierens). Außerdem würde ich, soferne nichts Wesentliches dagegen spricht, nicht nur Namen, sondern auch Pfade für die Dateien festlegen.

Die Wahl des Variablennamens "strAddress" ergibt für einen, der die Inhalte der .log-Dateien nicht kennt, weniger Sinn, als wenn Du etwa "strLine" verwendet hättest - für Dich wird das aber vermutlich anders sein.

Die entstehende "NewApps.log" schließt mit zwei Leerzeilen (CrLf) - zumindest den zweiten Umbruch kannst Du leicht vermeiden, indem Du mit "Write" anstatt mit "WriteLine" in die Datei schreibst.

Grüße
bastla