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

counting specific files in a directory

Status
Not open for further replies.

jbarnie

MIS
Jan 22, 2007
4
US
HI,

I am trying to create a vbscript to count specific file types (.IQ) in a folder and write that count to a text file. The only thing I can find is to count all files in a directory. Unfortunately there are several other file types in this specific folder and that code will not work in this case.

Any help would be appreciated.

Joe
 
and that code will not work in this case
Which code ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
have a look at Files collection and iterating through it checking the file extention then updating a counter
 
Sorry,

here is the code that wont work because it counts all files in a folder.

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(strDelDir)
Set fc = f.Files

'Logs the file count in the log file

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strLogDir & strScriptLog, ForAppending)
objFile.WriteLine (Date & " -- " & fc.count & " Log(s) deleted.")
objFile.Close
 
intCounter = 0
For Each aFile In fc
If UCase(Right(aFile.Name, 3)) = ".IQ" Then
intCounter = intCounter + 1
End If
Next

Wscript.Echo intCounter
 
I have added your code and all I get is a count of 0, no matter what directory I point it to. I have also tried changing the extension it is looking for to .txt


Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("C:\testlogs")
Set fc = f.Files

intCounter = 0
For Each aFile In fc
If UCase(Right(aFile.Name, 3)) = ".IQ" Then
intCounter = intCounter + 1
End If
Next

Wscript.Echo intCounter
 
It should work. If you try txt you need to do it without the dot. The script is looking at the last three characters in the filename.

Alternately, you could try.

If Instr(UCase(aFile.Name), ".TXT") Then
intCounter = intCounter + 1
End If

or

If Instr(UCase(aFile.Name), ".IQ") Then
intCounter = intCounter + 1
End If

though mrmovie's method should work and ensure you're only looking at the extension piece.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Yu could also use the File.Type property for more flexibility. Using Instr you could get false positives depending on name.

For example:

This.TXT.BMP would return as a TXT file but it is a BMP file.



I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
the cleanest way to do this, is to get the file type. and look at that.
you can use file property - file.type
this will return "text document" for .txt and "IQ file" for .IQ
Code:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("C:\testlogs")
Set fc = f.Files
intCounter = 0
For Each aFile In fc
   if left(afile.type,2) = "IQ" then 
      intCounter = intCounter + 1
   end if  
Next
Wscript.Echo intCounter







 
Or use a function like this if you want to search for more than one file type. This makes it easier to add or remove file extensions you're looking for. This is the same as one I posted in another thread except it uses a different fucntion name. To add another extension just add a pipe (|) followed by the extension inside the parenthesis where IQ is currently in. i.e. go from (iq) to (iq|txt) for iq and txt files.

Code:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("C:\temp")
Set fc = f.Files
intCounter = 0
For Each aFile In fc
   If CheckExtension(aFile.Name) Then 
      intCounter = intCounter + 1
   End If  
Next
Wscript.Echo intCounter

Function CheckExtension(strFileName)
'     On Error Resume Next
    
    Dim RegEx:    Set RegEx = New RegExp
    RegEx.Pattern = ".*\.(iq)$"
    RegEx.IgnoreCase = True
    CheckExtension = RegEx.Test(strFileName)
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hi,
You could also get a count of files using WMI.

'----Start----
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Path = '\\Foldername\\' and Extension = 'IQ'")
WScript.Echo colFiles.count
'----End----

If the computer has two disk drives, ie c and d, this script will look for the folder on both drives. This script assumes that the Foldername is on the root of the drive. You must use double \\ in place of \ with this WMI query. If it is a nested folder within Foldername, it would look like \\Foldername\\subfolder\\.

You could also list the files, like any other collection.
For each objFile in colFiles
wscript.echo objFile.name
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top