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

count files in folder thendisplay result

Status
Not open for further replies.

dottiemommy

Technical User
Jun 8, 2012
1
I am new to vbscript (very new) and I am having problems with the logic. I am counting files with a specific extension in a folder then send a message when it reaches the threshold.

'Test for the existence of 8 log files

Option Explicit
'On Error Resume Next

Dim strDirectory, nThreshold, counter, extension, msgtext
Dim objFSO, objFolder, objFile, Logfile

strDirectory = "C:\users\dotties\test"
counter = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirectory)

For Each objFile In objFolder.Files

counter = counter + 1
Next

msgtext = "The number of txt files in C:|textfiles is:"

Logfile = "C:\users\dotties\test\*.txt"
Const forReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(Logfile) Then
Else
Set objFile = objFSO.GetFile(Logfile) 'Getting an error on this line
End If

If (counter => 8) Then
'This is where I get confused

objFile.write = "The number of text files is:" & counter & vbcrlf
objFile.close
 
There are a few errors in your code; some syntactical, some logical. Building on the first 1/2 of your posted code, here is a version that simply counts the number of files with a given extension.

Code:
[COLOR=blue]Option[/color] [COLOR=blue]Explicit[/color]  
[COLOR=green]'On Error Resume Next[/color]

[COLOR=blue]Dim[/color] strDirectory, nThreshold, counter, extension, msgtext  
[COLOR=blue]Dim[/color] objFSO, objFolder, objFile, Logfile  

[COLOR=green]'file extension to look for[/color]
extension [COLOR=blue]=[/color] "txt"  

[COLOR=green]'directory to look in[/color]
strDirectory [COLOR=blue]=[/color] "C:\users\dotties\test"  

counter [COLOR=blue]=[/color] 0  

[COLOR=blue]Set[/color] objFSO [COLOR=blue]=[/color] CreateObject("Scripting.FileSystemObject")  
[COLOR=blue]Set[/color] objFolder [COLOR=blue]=[/color] objFSO.GetFolder(strDirectory)  

[COLOR=blue]For[/color] [COLOR=blue]Each[/color] objFile [COLOR=blue]In[/color] objFolder.Files  
	if LCase((objFSO.GetExtensionName(objFile))) [COLOR=blue]=[/color] LCase(extension) [COLOR=blue]then[/color]  
		counter [COLOR=blue]=[/color] counter [COLOR=blue]+[/color] 1  
	end [COLOR=blue]if[/color]  
[COLOR=blue]Next[/color]  

msgbox("The number [COLOR=blue]of[/color] txt files [COLOR=blue]in[/color] " [COLOR=blue]&[/color] strDirectory [COLOR=blue]&[/color] " is: " [COLOR=blue]&[/color] counter)


From your posted code, I'm not quite sure what you want to do when 8 or more such files are found. But you can test for 8 or more files like so:

Code:
if counter >= 8 then
  'add code for 8 or more files found
end if
 
A) For future reference, instead of looping through files and counting them one at a time, you can obtain the number of files or folders in a directory by using the .count method.

Code:
numFiles = objFolder.Files.Count
numFolders = objFolder.SubFolders.Count

B)[blue] You are getting an error because you are trying to get a file only if it doesn't exist. How can you get what isn't there.[/blue] [red]Furthermore, objFSO.FileExists checks to see if a single file exists. It does not accept wildcards ([link]http://raqxtr.sstar.com/caspdoc/html/vbscript_filesystemobject_object_fileexists_method.htm[/url]). Your code is looking for a file named *.txt.[/red] Use jges suggestion above to count all .txt files

Code:
[red]Logfile = "C:\users\dotties\test\*.txt" [/red]
Const forReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
[blue]If objFSO.FileExists(Logfile) Then
Else[/blue]
   [red]Set objFile = objFSO.GetFile(Logfile)[/red]    'Getting an error on this line
End If

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top