View Full Version : File count script
pedro
01-02-2002, 11:35 AM
Hi
I would like to create a script that would count the number of files of a specified type in a directory and it's subdirectories.
Example:
I have one directory Pictures and under this 2 others called Family and Landscapes. Inside each of these directories is a number of files with extension .jpg.
I would like to create a script that, after the user has selected Pictures and given the filter *.jpg returns a file like this:
Pictures\Family - 15
Pictures\Landscapes - 12
Is this possible? All sugestions are welcome. If this script already exists, or one like it, where can I find it to take ideas?
Thanks.
MnInShdw
01-03-2002, 06:27 AM
I spent more than 2 hours to write a script as you wish. Though it seemed to be an easy task at first , but it was so complicated.
Copy the following script and save it in any folder or desktop ( for example "findJpg.vbs" ). when u run this script , windows folder selection dialoge box will open. Select the parent folder that you want to check and click "OK".
A dialoge will inform you the No. of files with JPG extension in the selected folder and it's sub folders , and a text file "tree.txt" will be created in the same folder as the script file is saved.
In this text file the tree for the selected folder and it's subfolders and only the name of the JPG and their path will be created. At the bottom of "tree.txt " you will find the count for the .jpg files.
caution:I've writen this script with Wndows Xp Japanese version. As far as I know on win2k pro and windows XP ( pro/home ) it runs with no problem. If Possible , check it on win98 and let me know the result.
Good luck
Dim sLine,sRootDir,sOutFile , jpgcount
jpgcount = 0
Set Fs = WScript.CreateObject("Scripting.FileSystemObject")
Set Shell = WScript.CreateObject("Shell.Application")
sOutFile="tree.txt"
If WScript.Arguments.Count>0 Then
sRootDir=WScript.Arguments(0)
End If
If Fs.FolderExists(sRootDir) Then
Set oRootDir = Shell.NameSpace(sRootDir)
Else
Set oRootDir = Shell.BrowseForFolder(0,"Root file",0)
If IsEmpty(oRootDir) Then WScript.Quit
End If
f=0
Set ts=Fs.CreateTextFile(sOutFile,True)
ts.WriteLine oRootDir.Title
jpgcount = Search (oRootDir.Items.Item , 0)
ts.writeline "Folder " & orootdir.title & " and it's subdirectories contains ---> " & jpgcount & "files with '.jpg Extensinos."
ts.Close
WScript.Echo "Process Finished" & vbcrlf & vbcrlf & _
"Folder " & orootdir.title & " and it's subdirectories contains ---> " & jpgcount & "files with '.jpg Extensinos."
function Search(obj,total)
Dim I , check
I=1
If obj.IsFolder Then
Set oItems=obj.GetFolder.Items
For Each child In oItems
If child.IsFolder Then
tmp= "" & child.Name
Else
if fs.getextensionname(child.name) = "JPG" then
tmp= child.Name
total=total+1
end if
End If
If I=oItems.Count Then
ts.WriteLine sLine & "" & tmp
sLine=sLine & "@"
Else
ts.WriteLine sLine & "" & tmp
sLine=sLine & ""
I=I+1
End If
search child , total
Next
End If
If Len(sLine)>0 Then
sLine=Left(sLine,Len(sLine)-1)
End If
search=total
End function
pedro
01-07-2002, 11:57 AM
Hi;
Thanks for the script. I was not expecting a full script as answer...
I am sorry to report, though, that it doesn't seem to work properly. It always gives a result of 0 files.
I tested in W98 and W2K. I also tested removing a few strange characters that appeared from copy-pasting the listing. The result was the same. Here is an example of the output file:
Disk1
Folder Disk1 and it's subdirectories contains ---> 0files with '.jpg Extensinos.
I know that the folder Disk1 contains 2 .jpg files.
There is also a problem in selecting the desktop as starting folder... An error message with the following text appears:
Script: C:\PEDRO\findjpg.vbs
Line: 36
Char: 1
Error: Needed object:'obj'
Code: 800A01A8
Source: VBScript runtime error
This error message is translated from portuguese. In any case, I thank you again and will try to use this script to create my own.
Thanks
MnInShdw
01-07-2002, 12:30 PM
you're welcome.
I've written this script with my Sony pcg-gr7/k laptop , running on Windows XP pro Japanese version , and when run the mentioned script i detect no errors and gives me the right answer. I will go through it one more time ( not tonight, 'cause i've a lot to do for tonight) and try to trace the problem.
sorry for not being a help :(
pedro
01-08-2002, 12:14 PM
You were very helpful. I am truly thankful and was very pleasantly surprised by your answer. I am a beginer in WSH and really apreciate your script because it will help get more experienced and learn.
I thank you again, and please do not feel bad about anything I said on my previous message. None of it was intended as criticism.
You have been great.
Thanks
Powered by vBulletin™ Version 4.1.0 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.