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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare Contents of Folder 1

Status
Not open for further replies.

akki007

Programmer
May 22, 2003
55
GB
I want to compare the contents of a directory to a list of files that are expected to exist in the directory. This will tell me which files are missing etc.

Anybody got any ideas on how to do this?
 
I think the demand is to compare a disk folder with a list of files in a .txt file.

 
Yes, this is correct. I will give you the background...

Every week I should receive 50 files from 50 seperate locations. Files are named as such... SIMS_MON.2001.27.csv

"SIMS_MON" Is the same for all files
"2001" Is a 4 digit unique code and is different for all locations but is the same every week
"27" is the number of records in the file and will be different every week.

I want to have a list in a .txt file...

SIMS_MON.2001.*.csv
SIMS_MON.2002.*.csv
SIMS_MON.2003.*.csv
SIMS_MON.2004.*.csv

etc

Which I can then compare to what is actually in the directory every week. Then I can see whether or not any files are missing.

 
Hello akki007,

If the folder is a fixed folder and that the file list is a fixed list, then you can try the following script which would report which desired files in the list are not found in the folder. Copy and paste to a text file and name it with extension vbs. Double-click to go. (Edit the starting two lines to reflect the real names)
Code:
folderspec="d:\test\data"              [blue]'<<<edit this[/blue]
listfilespec="d:\test\filelist.txt"    [blue]'<<<edit this[/blue]

set fso=createobject("scripting.filesystemobject")
if not fso.fileexists(listfilespec) then
	wscript.echo "List file not found. Operation aborted."
	set fso=nothing
	wscript.quit (1)
end if
if not fso.folderexists(folderspec) then
	wscript.echo "Target folder not found. Operation aborted."
	set fso=nothing
	wscript.quit (2)
end if
set odic=createobject("scripting.dictionary")
set ots=fso.opentextfile(listfilespec,1)
do while not ots.atEndOfStream
	s=ots.readline
	if (trim(s)<>"") and (not odic.exists(s)) then
		odic.add trim(s),"m"	'm missing
	end if
loop
ots.close : set ots=nothing
for each ofile in fso.getfolder(folderspec).files
	if odic.exists(ofile.name) then odic.remove(ofile.name)
next
if odic.count=0 then
	wscript.echo "All listed files are found in the folder " & folderspec & "."
	set odic=nothing : wscript.quit
end if
msg="The following files are missing : " & vbcrlf & vbcrlf
for each skey in odic.keys
	msg=msg & skey & vbcrlf
next
set odic=nothing
wscript.echo msg
The list file (listfilespec in the script) is of the format of line-delimited plain-text with each line list one file name without path as you described.

regards - tsuji
 
Thats really cool! thanks for that. Although...

I need to be able to use a wildcard as the names of the files change. E.g. One week, a file could be called SIMS_MON.2001.125.csv and the week after it could be called SIMS_MON.2001.0.csv this is because the number of records varies in the file each week. Can this be done?
 
akki007,

If that piece of info "*" represents is very important, the reduction test may have you in trouble some time eventually. For instance, if the files in the folder is not purged before the new consoldating update coming in from the network, you might mislead to believe newly updated files are uploaded. But, you know better...

As to the coding, the using of dictionary is rendered less efficient. But, I want to salvage the best out of the script. This is the modification to take care of this.
Code:
folderspec="d:\test\data"              [blue]'<<<edit this[/blue]
listfilespec="d:\test\filelist.txt"    [blue]'<<<edit this[/blue]

set fso=createobject("scripting.filesystemobject")
if not fso.fileexists(listfilespec) then
    wscript.echo "List file not found. Operation aborted."
    set fso=nothing
    wscript.quit (1)
end if
if not fso.folderexists(folderspec) then
    wscript.echo "Target folder not found. Operation aborted."
    set fso=nothing
    wscript.quit (2)
end if
set odic=createobject("scripting.dictionary")
set ots=fso.opentextfile(listfilespec,1)
do while not ots.atEndOfStream
    s=[blue]lcase(ots.readline)[/blue]
    if (trim(s)<>"") and (not odic.exists(s)) then
        odic.add trim(s),"m"    'm missing
    end if
loop
ots.close : set ots=nothing
for each ofile in fso.getfolder(folderspec).files
    [blue]sext=lcase(fso.getextensionname(ofile))
    sbase=lcase(fso.getbasename(ofile))
    for i=1 to len(sbase)-1
        spseudokey=left(sbase,i) & "*." & sext
        if odic.exists(spseudokey) then odic.remove(spseudokey) : exit for
    next[/blue]
next
if odic.count=0 then
    wscript.echo "All listed files are found in the folder " & folderspec & "."
    set odic=nothing : wscript.quit
end if
msg="The following files are missing : " & vbcrlf & vbcrlf
for each skey in odic.keys
    msg=msg & skey & vbcrlf
next
set odic=nothing
wscript.echo msg
For instance, if there is a file named SIMS_MON.2003.X.csv, it will be taken as having that file uploaded which may not be what you intend to see.

- tsuji

ps. I take this opportunity to correct the previous script where I did not uniformize the case (upper or lower) before testing existence. The corresponding lines should be read as follow:
[tt] s=[red]lcase(ots.readline)[/red][/tt]
and
[tt] if odic.exists([red]lcase(ofile.name)[/red]) then odic.remove([red]lcase(ofile.name)[/red])[/tt]
Thanks for the attention.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top