Solved Compare two exact files and delete one

December 8, 2012 at 18:29:34
Specs: Windows 7
Hello,

I am l looking for a vbscript that can compare two files and if identical, delete one of them.
I have a directory that gets files constantly and each file gets a duplicate copy as well. I was looking for a vbscript that can scan that folder and delete all duplicate copies. The scan would have to start looking at the data after the 5th line, lines 1-5 will be different but after 5 they are all the same.

These are text files.
The match will be exact (after the fifth line) so it can be case sensitive.
There could be 2 files or 20 files in the folder. Never the same amount each time.
The files are small (3KB to 9KB)
It will be very very rare but there could be three files
After the fifth line everything will be identical so either file can be deleted.

Thank you.


See More: Compare two exact files and delete one

Report •

#1
December 8, 2012 at 20:21:24
fc file1 file2 > nul && del file2

Report •

#2
December 8, 2012 at 20:46:11
The problem is that it is not just two files. There is a folder that can have anywhere between 1-20 files. All files need to be compared to each other but after line 5.

Report •

#3
December 8, 2012 at 21:32:37
✔ Best Answer
I'm assuming the after-line-5 problem was taken care of by SED?
this might work, but it is (obviously) not optimized for speed:
for %%a in (*) do (
for %%b in (*) do (
if "%%a" neq "%%b" fc "%%a" "%%b" && del "%%b"
)
)

Report •

Related Solutions

#4
December 9, 2012 at 00:59:21
nbrane,

pretty neat

=====================
M2 Golden-Triangle


Report •

#5
December 9, 2012 at 03:55:45
No. I cannot use sed in this one. I need the first 5 lines in the file. Sed was for something different. I would need to compare lines 6 and everything after that. Delete the dup file and keep one file with line 1-5.

Report •

#6
December 9, 2012 at 13:46:00
(@M2: thanks)

Ah, I was afraid of that. Well, here goes with convoluted vbscript attempt:
'=======begin vbscript: safety is ON for testing/debugging.
set fso=createobject("scripting.filesystemobject")
if not(fso.folderexists("temp")) then fso.createfolder("temp")
set folder=fso.getfolder(".")
for each file in folder.files
f=file.name
strip5(f)
this=t
if this<>"" then
for each fi in folder.files
if fi.name <>f then
strip5 (fi.name)
'safety is ON:
if t = this then fi.move(".\temp\")
'safety is OFF:
'if t = this then fso.deletefile(fi.name)
end if
next
end if
next
wscript.quit

sub strip5 (n)
t=fso.opentextfile(n,1).readall
for i=1 to 5
p=instr(t,vbcrlf)
if p=0 then
t=""
exit for
end if
t=mid(t,p+2)
next
end sub
'======= end vbscript


Report •

#7
December 10, 2012 at 00:06:29
Maybe it's just me but I think we have two threads cross-linked.

One is delete dup.

The other is drop first five lines.

=====================
M2 Golden-Triangle


Report •

Ask Question