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

count files containing specific string in a filename 1

Status
Not open for further replies.

causative

Technical User
Mar 6, 2009
12
PL
Hi forum members!
This code below count the number of txt files.
What should be changed to count files containing specific string in a filename?

Code:
Option Explicit 
 Dim fso, count, src, folder, file 
 Set fso = CreateObject("Scripting.FileSystemObject") 
 src = "C:\Users\Mark\Desktop\test" 
 Set folder = fso.GetFolder(src) 
 count = 0 
 For Each file In folder.files 
 If LCase(fso.GetExtensionName(file)) = "txt" Then 
 count = count + 1 
 End If 
 Next 
 WScript.Echo "Count: " & count


Thx in advice
 
Use the InStr function

Not tested:
Code:
Option Explicit 
 Dim fso, count, src, folder, file 
 Set fso = CreateObject("Scripting.FileSystemObject") 
 src = "C:\Users\Mark\Desktop\test"
 [highlight #FCE94F]stringtofind = "abc"[/highlight]
 Set folder = fso.GetFolder(src) 
 count = 0 
 For Each file In folder.files 
 [highlight #FCE94F]If instr(file.name, stringtofind, 0) > 0 Then[/highlight]
 count = count + 1 
 End If 
 Next 
 WScript.Echo "Count: " & count
 
It returns error on line 2 - stringtofind is not declared so I changed line 1 to
Code:
Dim fso, count, src, folder, file, [highlight #FCE94F]stringtofind[/highlight]
but now it returns error on line 9
Code:
If instr(file.name, stringtofind, 0) > 0 Then
Type Mismatch
 
Sorry, I was going for a non-case-sensitive search, now I'm thinking this would be better (tested this time):

[tt]If instr(LCase(file.name), LCase(stringtofind)) > 0 Then[/tt]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top