Right so the problem is as follows if anybody could give me a few pointers:
I've what seems like a simple task to do. I'm coding in VBA and want to run a macro such that you identify two folders (each contain the same number of files, with the same name).
I then want to compare these files(.txt, .xls, .exp, .tab - Issues with file types?) whether that be by parsing the files or by loading them into excel in a temporary sheet, pair by pair and doing some sort of vlookup.
The point of this is to find any discrepancies and highlight them in a seperate sheet for further analysis. (can the FC function in DOS help at all?)
If anyone could point me in the direction of a VBA method or a starting point I would greatly appreciate it. Thanks in advance.
Last edited by VBABen; 06-17-2011 at 09:47 AM.
to compare 2 textfiles in 2 different folders:
sub snb() open "E:\OF\example1.txt" for input as 1 c01=Input(LOF(1),1) close open "E:\OF2\example1.txt" for input as 1 c02=Input(LOF(1),1) close msgbox iif(strcomp(c01,c02)=0,"","Not ") & "equal" End Sub
Thanks snb for the input there, it does compare the files but I need to extend this functionality a lot further.
I need to be able to check various types of files e.g. .xls, .tab, .exp. I can convert these files to .txt files before the whole process begins which isn't a big issue. The issue is when I want to then highlight the area in the file that doesn't match. I want to create a big report to detail all the files, there status and the unmatched area if there is one.
Thanks in advanced.
I did what you asked in your first post.
Please be more specific, post some example files, and what you want the comparison result to look like.
Sorry, I should have explained how I wanted the errors highlighted. I've attached an example of an output report I''m looking for. And pairs of files (1a and 2a are pairs and 1b and 2b are pairs) - These are basic examples.
As well as checking if the files match I'd like to be able to say where exactly they don't to give the user a better chance of finding the error in the file after. I.e. line number and string of mis match from both files. Could the files themselves be altered when a mis match is found e.g. Set the text that doesn't match to bold or a different colour?
Thanks in advanced.
If you want the files in both folders to be exactly the same, you don't need to compare, but only to copy.....Could the files themselves be altered when a mis match is found
I don't need the files to be the same, I just need to see where there is a difference. What I meant when i said altered is to have an added highlight to the file so the user could spot it straight away. But if I can populate the report description field with a line number and string then I wouldn't need to alter the .txt file.
In a perfect run all files should report as matched. But I'd like details of when they don't i.e. line numbers and strings.
Apologies if I'm not being all that clear.
sub snb() c00="E:\OF2\" ' the folder to compare with for each fl in createobject("scripting.filesystemobject").getfolder("E:\OF").files if dir(c00 & fl.name)="" then c03= c03 & vbcr & fl.name & "|missing" else open fl for input as 1 c01=Input(LOF(1),1) close open c00 & fl.name for input as 1 c02=Input(LOF(1),1) close if strcomp(c01,c02)=0 then c03=c03 & vbcr & fl.name & "|match" else c04="" sn=split(c01,vbcrlf) sq=split(c02,vbcrlf) for j=1 to ubound(sn) if strcomp(sn(j),sq(j))<>0 then c04=c04 & vbtab "line " & j &": " & sn(j) & " / " & sq(j) next c03=c03 & vbcr & fl.name & "|mismatch|" & c04 end if end if next st=split(mid(c03,2),vbcr) sheets(1).cells(1).resize(ubound(st)+1)=application.transpose(st) sheets(1).columns(1).texttocolumns ,,,,False,False,False,False,True,"|" End Sub
Last edited by snb; 06-17-2011 at 06:33 AM.
I'm having problems with the ubound function, how have you declared your variables? namely sn and sq?
Thanks in advance
There's no need to declare any variable.I let the VBA-compiler determine what vartype to use.
sn, sq and st are variant arrays.The split method produces arrays.
If you encounter a problem please be as specific as possible: what (kind of) file, which folder, the file content, etc. 'a problem with' doesn't provide an information overload.![]()
Thanks snb. I'm been stepping through it so I follow your logic. I'll need to clean up the output so if a lot of errors are there it won't generate one long line.
I was hoping for something along the lines of the output in the attached. Would this just be a case of inserting the correct variable into the desired cell? I'd like to break on a new line with each set of files.
Thanks in advance, this has really helped me so far![]()
Last edited by VBABen; 06-17-2011 at 08:50 AM.
That's what my macro does.I'd like to break on a new line with each set of files
What is the use of repeating the identical filenames in column A & column B ?
In my previous attachment, I've put a line break when the second mismatch is found so you can read it clearly. Your macro didn't seem to do this and only put one long string of detail on the same line. I need it to break per detail then break again per file as it originally does.
In that case you'll have to adapt the suggestion I made to your 'needs'.
Ok, I'll keep cracking on with this myself. Thanks for the suggestions, they have been very much appreciated. I'll mark as solved based on my initial description![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks