Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: FSO Problems - Need help summing unique files in a folder


Message #1 by "Eric Robbins" <erobbins@w...> on Wed, 29 May 2002 23:26:23
I posted this in the asp_forms newsgroup...and then moved it here..

I have a folder with files that are in this format: -1511086.000- The 
suffix is any series of numbers like .001 .343 .222 etc.. there is no 
standard.  However, the prefix is sometimes the same ie. 1511086.000, 
1511086.512, 1511086.111, 1511086.226.  I am trying to count the total 
times I have a like file...for instance in the previous line I have 4 
instances of 1511086.XXX.  My code is not working correctly.  Could 
someone help me out?

<%
Dim FSO,Folder,SubFolders,FolderContents,file
Dim PhysicalPath
Dim fileCheck, curFile, fileCount, myFlag

PhysicalPath = Server.Mappath("./PICTURE/")
' Create the FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject") 
' Get the current Folder
Set Folder = FSO.GetFolder(PhysicalPath) 
' Assign the SubFolders collection to the SubFolders variable
set SubFolders = Folder.SubFolders 
' Assign the Files collection to the FolderContents variable
Set FolderContents = Folder.Files

' Iterate through the files
'myFlag=1
fileCount=1
For Each file in FolderContents  
	'Get the filename
	curFile = Left(file.name,7)

	if myFlag=1 then
		fileCheck = Left(file.name,7)
		fileCount = 0 		
	end if


	If curFile = fileCheck then
		fileCount=fileCount+1
		myFlag=0

	else	
		myFlag=1
		fileCount = 1
	
	end if
Response.Write curFile &" has "& fileCount & " images<BR>"
	
	curFile=""

Next  

Set FSO=nothing
%>
Following is what I see when I run my code.

I am receiving files in the manner where the numbers on the left side of 
the "." my or may not relate 
such as 1587745.225, 1587745.342, 1587745.101 as well as unrelated files 
2787475.005 and 8484715.022.  I have no control over how I get these, 
however I must sum the like files and post to a db.  for instance 
1587745.XXX has 3 files,2787475.XXX has 1 file and 8484715.XXX has 1 file.

So far I am getting results on my folder such as this when I run my code:
1472765 has 1 images
1472765 has 2 images
1472765 has 3 images
1472765 has 4 images
1475272 has 1 images
1481492 has 1 images
...
1527298 has 1 images
1527298 has 1 images
1527298 has 2 images
1527298 has 3 images <--Should say 4 at this point
...
1823242 has 1 images
1823256 has 1 images
1823256 has 2 images
1823276 has 1 images
1823276 has 1 images <--Should say 2 at this point
Notice that for some reason my code works and then it starts to step into 
a count weird 1527298 should have 4 not 3 images.
Message #2 by "phil griffiths" <pgtips@m...> on Thu, 30 May 2002 09:46:52
Personally I would use a dictionary object to store the count, and the FSO 
method GetBaseName to get the filename, like this:

    Dim dct As Scripting.Dictionary
    Set dct = CreateObject("Scripting.Dictionary")
    For Each oFile In fso.GetFolder(sPhysicalPath).Files
    
        sFileName = fso.GetBaseName(oFile)
        If dct.Exists(sFileName) Then
            dct(sFileName) = dct(sFileName) + 1
        Else
            dct.Add sFileName, 1
        End If
        
    Next
    
    Dim key
    For Each key In dct
        Response.Write "File " & key & " has " & CStr(dct(key)) & " images"
    Next

HTH
Phil
--------------------------------
> I posted this in the asp_forms newsgroup...and then moved it here..

> I have a folder with files that are in this format: -1511086.000- The 
s> uffix is any series of numbers like .001 .343 .222 etc.. there is no 
s> tandard.  However, the prefix is sometimes the same ie. 1511086.000, 
1> 511086.512, 1511086.111, 1511086.226.  I am trying to count the total 
t> imes I have a like file...for instance in the previous line I have 4 
i> nstances of 1511086.XXX.  My code is not working correctly.  Could 
s> omeone help me out?

> <%
D> im FSO,Folder,SubFolders,FolderContents,file
D> im PhysicalPath
D> im fileCheck, curFile, fileCount, myFlag

> PhysicalPath = Server.Mappath("./PICTURE/")
'>  Create the FileSystemObject
S> et FSO = CreateObject("Scripting.FileSystemObject") 
'>  Get the current Folder
S> et Folder = FSO.GetFolder(PhysicalPath) 
'>  Assign the SubFolders collection to the SubFolders variable
s> et SubFolders = Folder.SubFolders 
'>  Assign the Files collection to the FolderContents variable
S> et FolderContents = Folder.Files

> ' Iterate through the files
'> myFlag=1
f> ileCount=1
F> or Each file in FolderContents  
	> 'Get the filename
	> curFile = Left(file.name,7)

> 	if myFlag=1 then
	> 	fileCheck = Left(file.name,7)
	> 	fileCount = 0 		
	> end if

> 
	> If curFile = fileCheck then
	> 	fileCount=fileCount+1
	> 	myFlag=0

> 	else	
	> 	myFlag=1
	> 	fileCount = 1
	> 
	> end if
R> esponse.Write curFile &" has "& fileCount & " images<BR>"
	> 
	> curFile=""

> Next  

> Set FSO=nothing
%> >
F> ollowing is what I see when I run my code.

> I am receiving files in the manner where the numbers on the left side of 
t> he "." my or may not relate 
s> uch as 1587745.225, 1587745.342, 1587745.101 as well as unrelated files 
2> 787475.005 and 8484715.022.  I have no control over how I get these, 
h> owever I must sum the like files and post to a db.  for instance 
1> 587745.XXX has 3 files,2787475.XXX has 1 file and 8484715.XXX has 1 
file.

> So far I am getting results on my folder such as this when I run my code:
1> 472765 has 1 images
1> 472765 has 2 images
1> 472765 has 3 images
1> 472765 has 4 images
1> 475272 has 1 images
1> 481492 has 1 images
.> ..
1> 527298 has 1 images
1> 527298 has 1 images
1> 527298 has 2 images
1> 527298 has 3 images <--Should say 4 at this point
.> ..
1> 823242 has 1 images
1> 823256 has 1 images
1> 823256 has 2 images
1> 823276 has 1 images
1> 823276 has 1 images <--Should say 2 at this point
N> otice that for some reason my code works and then it starts to step 
into 
a>  count weird 1527298 should have 4 not 3 images.
Message #3 by "Drew, Ron" <RDrew@B...> on Thu, 30 May 2002 08:25:51 -0400
OK how about this...
...............................................
Dim hit, savname
hit =3D 0
savname =3D ""
'first check to make sure you have items in the folder
If folder.Size > 0 Then
    For Each file in folder.Files
         if hit =3D 0 then
            savname =3D file.Name
         end if
         if savname <> file.Name then
            Response.Write savname &" has "& hit & " images<BR>"
            savname =3D file.Name
            hit =3D 1
         else
            hit =3D hit + 1
         end if
     next
 else
   Response.Write "No Items in Folder"
end if
...................................................
-----Original Message-----
From: Eric Robbins [mailto:erobbins@w...]
Sent: Wednesday, May 29, 2002 7:26 PM
To: ASP Web HowTo
Subject: [asp_web_howto] FSO Problems - Need help summing unique files
in a folder


I posted this in the asp_forms newsgroup...and then moved it here..

I have a folder with files that are in this format: -1511086.000- The
suffix is any series of numbers like .001 .343 .222 etc.. there is no
standard.  However, the prefix is sometimes the same ie. 1511086.000,
1511086.512, 1511086.111, 1511086.226.  I am trying to count the total
times I have a like file...for instance in the previous line I have 4
instances of 1511086.XXX.  My code is not working correctly.  Could
someone help me out?

<%
Dim FSO,Folder,SubFolders,FolderContents,file
Dim PhysicalPath
Dim fileCheck, curFile, fileCount, myFlag

PhysicalPath =3D Server.Mappath("./PICTURE/")
' Create the FileSystemObject
Set FSO =3D CreateObject("Scripting.FileSystemObject")
' Get the current Folder
Set Folder =3D FSO.GetFolder(PhysicalPath)
' Assign the SubFolders collection to the SubFolders variable set
SubFolders =3D Folder.SubFolders
' Assign the Files collection to the FolderContents variable Set
FolderContents =3D Folder.Files

' Iterate through the files
'myFlag=3D1
fileCount=3D1
For Each file in FolderContents 
	'Get the filename
	curFile =3D Left(file.name,7)

	if myFlag=3D1 then
		fileCheck =3D Left(file.name,7)
		fileCount =3D 0 	=09
	end if


	If curFile =3D fileCheck then
		fileCount=3DfileCount+1
		myFlag=3D0

	else=09
		myFlag=3D1
		fileCount =3D 1
=09
	end if
Response.Write curFile &" has "& fileCount & " images<BR>"
=09
	curFile=3D""

Next 

Set FSO=3Dnothing
%>
Following is what I see when I run my code.

I am receiving files in the manner where the numbers on the left side of

the "." my or may not relate
such as 1587745.225, 1587745.342, 1587745.101 as well as unrelated files

2787475.005 and 8484715.022.  I have no control over how I get these,
however I must sum the like files and post to a db.  for instance
1587745.XXX has 3 files,2787475.XXX has 1 file and 8484715.XXX has 1
file.

So far I am getting results on my folder such as this when I run my
code: 1472765 has 1 images 1472765 has 2 images 1472765 has 3 images
1472765 has 4 images 1475272 has 1 images 1481492 has 1 images ...
1527298 has 1 images 1527298 has 1 images 1527298 has 2 images 1527298
has 3 images <--Should say 4 at this point ... 1823242 has 1 images
1823256 has 1 images 1823256 has 2 images 1823276 has 1 images 1823276
has 1 images <--Should say 2 at this point Notice that for some reason
my code works and then it starts to step into
a count weird 1527298 should have 4 not 3 images.

---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=3Dnosim/theprogramm
e
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=3Dnosim/theprogramm
e
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=3Dnosim/theprogramm
e
r-20
Message #4 by "Eric Robbins" <erobbins@w...> on Thu, 30 May 2002 16:27:46
Thanks for your code offers, however, I must ONLY sum like file 
names...not the totality of files in a folder.  Hence my problem when I 
output my code.  Sometimes it gives me two "1's" when it should go "1,2".
Message #5 by "Eric Robbins" <erobbins@w...> on Thu, 30 May 2002 17:34:50
Ok, I tried the above.  I never knew about the dictionary object.  Thanks 
a thousand times for introducing me to that.  It works with a bit more 
tweaking, I now have it automatically updating my db.

  Return to Index