Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Comparare files .txt

    Salve,ho uno script che compara valori in due file di testo e nel caso il primo file dovesse contenere meno stringhe del secondo mi notifica.
    Ora il problema è che in pratica i due files contengono circa 100 righe ciascuno.
    Ora succede che se ad essere mancate è l'ultima riga del file 1 allora ottengo la notifica correttamente.
    Se invece manca una stringa iniziale o intermedia inizia a dirmi che tutte le stringhe successive a quella riga sono differenti.
    Il consiglio che mi è stato dato è che anzichè scrivere tutte le stringhe solo se sono diverse,leggere entrambi i files e popolare due array di stringhe.
    Poi con due for next controllare se ognuna delle stringhe
    dell'array A è presente o meno nell'array B
    Purtroppo sono un newbie...qualcuno può darmi una mano?

    Questo è il codice:

    codice:
    dim filename1
    dim filename2
    
    filename1="c:\sitelist.txt"
    filename2="c:\textstream.txt"
    
    
    Set oFSO1 = CreateObject("Scripting.FileSystemObject")
    Set oFSO2 = CreateObject("Scripting.FileSystemObject")
    
    Set oTextFile1 = oFSO1.OpenTextFile(filename1, 1 , True)
    Set oTextFile2 = oFSO2.OpenTextFile(filename2, 1 , True)
    
    
    While oTextFile1.AtEndOfStream <> True
    line1 = oTextFile1.ReadLine
    line2 = oTextFile2.ReadLine
    
    if (line1<>line2) then
    Mail = Mail & "Different files" & vbcrlf & line1 & vbcrlf & line2 & 
    vbcrlf
    
    end if
    wend
    
    'if the second file has more strings i get the notify
    if (oTextFile2.AtEndOfStream <> True) then
    Mail = Mail & "Different Files"
    
    
    oTextFile1.close
    oTextFile2.close
    Dade2 • Premium Windows Hosting • Microsoft Partner
    Server Dedicati • R1Soft daily backups • Dedicated Account Managers

  2. #2
    UP
    Dade2 • Premium Windows Hosting • Microsoft Partner
    Server Dedicati • R1Soft daily backups • Dedicated Account Managers

  3. #3
    enjoy!
    codice:
    Option Explicit
    
    Const ForReading = 1
    
    Dim arrOne(), arrTwo()
    Dim file1, file2
    Dim fso, sOne, sTwo
    Dim i, j
    Dim trovato
    Dim msg
    
    file1 = "C:\Documents and Settings\Alethesnake\Desktop\file1.txt"
    file2 = "C:\Documents and Settings\Alethesnake\Desktop\file2.txt"
    
    Set fso  = CreateObject("Scripting.FileSystemObject")
    Set sOne = fso.OpenTextFile(file1, ForReading)
    Set sTwo = fso.OpenTextFile(file2, ForReading)
    
    i = 0
    
    Do While Not (sOne.AtEndOfStream And sTwo.AtEndOfStream)
    	
    	If Not sOne.AtEndOfStream Then
    		ReDim preserve arrOne(i)
    		arrOne(i) = Trim(sOne.readLine() & "")
    	End If
    	
    	If Not sTwo.AtEndOfStream Then
    		ReDim preserve arrTwo(i)
    		arrTwo(i) = Trim(sTwo.readLine() & "")
    	End If
    	
    	i = i + 1
    loop
    
    sOne.Close: Set sOne = Nothing
    sTwo.Close: Set sTwo = Nothing
    Set fso = Nothing
    
    msg = ""
    
    For i = 0 To UBound(arrOne)
    	
    	trovato = False
    	
    	For j = 0 To UBound(arrTwo)
    	
    		If arrOne(i) = arrTwo(j) Then
    			trovato = True
    			Exit For
    		End If
    	
    	Next 'j
    	
    	If Not trovato Then
    		msg = msg & (i + 1) & ", "
    	End If
    	
    Next 'i
    
    If msg = "" Then
    	MsgBox "Tutte le righe del file: " & vbcrlf & file1 & vbcrlf & _
    			"si trovano anche nel file: " & vbcrlf & file2 & vbcrlf & _
    			"(ATTENZIONE: non vale il viceversa!)", _
    			vbInformation
    Else
    	msg = Left(msg, Len(msg) - 2)
    	
    	MsgBox "Le righe: " & vbCrlf & msg & vbCrlf & _
    			"del file: " & vbCrlf & file1 & vbCrlf & _
    			"non sono presenti nel file: " & vbCrlf & file2 & vbCrlf,  _
    			vbInformation
    End If
    xxx

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.