Asked by:
Script for counting number of files in folders

Question
-
Hey team,
I'm trying to write a script that will count the number of files in a folder on x2 different servers, and if they number of files exceeds X amount of files then it will report an error.
I've got the script working for one folder on a server but am wondering if there's a way of adding it all in one script so it checks all the folders on the 2 servers in stead of me writing 10 of these scripts and having 10 different scheduled tasks.
Any thoughts on adding to my below script?
Cheers
strComputer = "MyServerName"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Do While True
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='FolderLocation'} Where " _
& "ResultClass = CIM_DataFile")
If colFileList.Count >= 100 Then
Exit Do
End If
Wscript.Sleep 600000
Loop
Wscript.Echo "There are at least 100 log files in the target folder."
- Moved by Bill_Stewart Friday, July 27, 2018 8:36 PM This is not "scripts on demand"
Monday, May 21, 2018 8:13 PM
All replies
-
You could start by using PowerShell and learning something about programming with a script language.
We won't write a script for you.
Here are some resources that will teach you how to write scripts.
Please carefully review the following links to set your expectation for posting in technical forums.
This Forum is for Scripting Question Rather than script requests
Script requests
PowerShell DocumentationFrom a Bill Stewart summary of useful forum links:
- Posting guidelines
- Handy tips for posting to this forum
- How to ask questions in a technical forum
- Rubber duck problem solving
- How to write a bad forum post
- Help Vampires: A Spotter's Guide
- This forum is for scripting questions rather than script requests
\_(ツ)_/
Monday, May 21, 2018 8:18 PM -
Have a look at Get-ChildItem (works as well with UNC paths) and Invoke-Command
A quick example how you could do that for a single directory:
if((Get-ChildItem -Path "C:\Windows" -Filter "*.log").count -gt 5){ "more than 5 logfiles found.." }
Tuesday, May 22, 2018 7:08 AM -
As you can see in my Question I've written the script already, if you bothered to read it at all.
My question was, I was just unsure of the command to run that across multiple folders in one script.
But thanks for nothing anyway....
Tuesday, May 22, 2018 8:12 PM -
As you can see in my Question I've written the script already, if you bothered to read it at all.
My question was, I was just unsure of the command to run that across multiple folders in one script.
But thanks for nothing anyway....
Use a "For Each" loop to enumerate the computer names.
You have not explained the logic needed for multiple computers. "any computer", "sum of all" … etc.
Since you do not know how to script in VBScript you should skip learning it. VBScript is obsolete and will disappear soon. Learn and use PowerShell. It is much easier to use.
\_(ツ)_/
- Edited by jrv Tuesday, May 22, 2018 8:20 PM
Tuesday, May 22, 2018 8:19 PM