We help IT Professionals succeed at work.

I need a VB or even a Powershell Script that monitors file build up within a folder.

Medium Priority
787 Views
Last Modified: 2013-12-14
I need a Powersheel Script that  monitors file build up within a NTFS folders.

a. If files are discovered, how many files and then run an executable ( BLAT.exe to an email address ).

I'm an admin, not a programmer....so can someone help me write a short script that checks a specified folders, and BLAT.exe emails me the results, I would greatly appreciate it.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi there,

Do you need to know whether new files have been added, or are you just looking for a entire directory listing on a periodic basis?

From a command prompt, this will give you an entire directory listing, including subfolders:
dir /s /b "\\server\folder\*.*"

The /b is a "basic" output command, which will output the file paths only.

Regards,

Rob.

Author

Commented:
I am looking to find out if any files have been deposited in that directory by the time the script is ran.

Commented:
Something like this (powershell)
$date = Get-Date $LastTimeItRan
dir $path -rec | ?{$_.LastWriteTime -gt $date}

Open in new window

LearnctxEngineer
CERTIFIED EXPERT

Commented:
Say you run the script once a week, you could try something like this. If you wanted to include folders as well as files you can remove $_.PSIsContainer -eq $false -and.
$path = "C:\temp" # edit for the path you want to search
$lastrun = $(Get-Date).AddDays(-7) # adjust the number to look back x amount of days
gci $path -rec | ? {$_.PSIsContainer -eq $false -and $_.LastWriteTime -ge $lastrun} | select name,directory,lastwritetime

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, you can use this function to determine the amount of files in the folder as well....

Regards,

Rob.
strFolder = "C:\Temp\Temp"
intCount = CountFiles(strFolder)
MsgBox strFolder & " has " & intCount & " files in it."
 
Function CountFiles(strFolderPath)
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Const intForReading = 1
	strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
	strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strFolderPath).ShortPath & " > """ & strTempFile & """"
	objShell.Run strCommand, 0, True
	Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
	intCount = 0
	While Not objFile.AtEndOfStream
		objFile.SkipLine
		intCount = intCount + 1
	Wend
	objFile.Close
	objFSO.DeleteFile strTempFile, True
	CountFiles = intCount
End Function

Open in new window

Author

Commented:
Thakns for everyone's responses. The DIR I need to monitor is an UNC path.  \\servername\folder.

I tried your scripts but I think it is having a hard time functioning becuase of the UNC path inste of a local path. Can you confirm?

Commented:
Powershell doesn't care whether the path is local or UNC... it acts the same.

Author

Commented:
RobSampson,

Your script worked well with UNC, but it displayed the results. I would like to have those results either dropped into an email or a file in which I can Blat out to email.

What is my next step?

Author

Commented:
BSonPosh,

I am testing your results...
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, this will use blat.exe to send an email with the count of files in the folder.

You can modify these as required:

strBlatPath = "C:\Blat.exe"
strBody = strFolder & " has " & intCount & " files in it."
strServer = "YourSMTPServer"
strTo = "recipient@domain.com"
strFrom = "sender@domain.com"
strSubject = "This is the subject"

Regards,

Rob.
strFolder = "C:\Temp\Temp"
intCount = CountFiles(strFolder)
Set objShell = CreateObject("WScript.Shell")
strBlatPath = "C:\Blat.exe"
strBody = strFolder & " has " & intCount & " files in it."
strServer = "YourSMTPServer"
strTo = "recipient@domain.com"
strFrom = "sender@domain.com"
strSubject = "This is the subject"
strCommand = strBlatPath & " """ & strBody & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 1, True
MsgBox "Blat has been run."
 
Function CountFiles(strFolderPath)
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Const intForReading = 1
	strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
	strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strFolderPath).ShortPath & " > """ & strTempFile & """"
	objShell.Run strCommand, 0, True
	Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
	intCount = 0
	While Not objFile.AtEndOfStream
		objFile.SkipLine
		intCount = intCount + 1
	Wend
	objFile.Close
	objFSO.DeleteFile strTempFile, True
	CountFiles = intCount
End Function

Open in new window

Author

Commented:
Thanks RobSampson,

I think I am unable to get your Blat script to work. I get a pop up indicating it ran, but I received no email. Flip side of that, If I test the BLAT but the following line, it works.

 C:\Jobs\Blat.exe -to glenn@spr.com, -s "Orphaned Files" -f admin@spr.com -server mail.spr.com


Any thoughts?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
OK, using the parameters you specified above, try this.  You should see the result of the BLAT command now.

Regards,

Rob.
strFolder = "C:\Temp\Temp"
intCount = CountFiles(strFolder)
Set objShell = CreateObject("WScript.Shell")
strBlatPath = "C:\Jobs\Blat.exe"
strBody = strFolder & " has " & intCount & " files in it."
strServer = "mail.spr.com"
strTo = "glenn@spr.com"
strFrom = "admin@spr.com"
strSubject = "Orphaned Files"
strCommand = "cmd /k " & strBlatPath & " """ & strBody & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 1, True
MsgBox "Blat has been run."
 
Function CountFiles(strFolderPath)
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Const intForReading = 1
	strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
	strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strFolderPath).ShortPath & " > """ & strTempFile & """"
	objShell.Run strCommand, 0, True
	Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
	intCount = 0
	While Not objFile.AtEndOfStream
		objFile.SkipLine
		intCount = intCount + 1
	Wend
	objFile.Close
	objFSO.DeleteFile strTempFile, True
	CountFiles = intCount
End Function

Open in new window

Author

Commented:
I think we are close - when I launch it, I get the following error:

Blat v2.6.2 w/GSS encryption (build : Feb 25 2007 12:06:19)
unknown error code 2 when trying to open \\gwmerrel\temp\ has 2 files in it.
C:\Documents and Settings\gwmerrel\Desktop>

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hmmm, it looks like the body has to be read from a text a text file.  Try this. It will write the body text to a text file, and try to use that during the send.

Regards,

Rob.
strFolder = "C:\Temp\Temp"
intCount = CountFiles(strFolder)
Set objShell = CreateObject("WScript.Shell")
strBlatPath = "C:\Jobs\Blat.exe"
strBody = strFolder & " has " & intCount & " files in it."
strServer = "mail.spr.com"
strTo = "glenn@spr.com"
strFrom = "admin@spr.com"
strSubject = "Orphaned Files"
Set objFSO = CreateObject("Scripting.FileSystemObject")
strBodyFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "BodyText.txt"
Set objBody = objFSO.CreateTextFile(strBodyFile, True)
objBody.Write strBody
objBody.Close
Set objBody = Nothing
strCommand = "cmd /k " & strBlatPath & " """ & strBodyFile & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 1, True
objFSO.DeleteFile strBodyFile, True
MsgBox "Blat has been run."
 
Function CountFiles(strFolderPath)
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Const intForReading = 1
	strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
	strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strFolderPath).ShortPath & " > """ & strTempFile & """"
	objShell.Run strCommand, 0, True
	Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
	intCount = 0
	While Not objFile.AtEndOfStream
		objFile.SkipLine
		intCount = intCount + 1
	Wend
	objFile.Close
	objFSO.DeleteFile strTempFile, True
	CountFiles = intCount
End Function

Open in new window

Author

Commented:
Awesome Rob.
I tried to make changes to it but was not successful, but how do I add more (strFolder) DIRs to be scanned?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, what about this?  The array at the top can have your folders in it.  The body text is appended to with each folder.

Regards,

Rob.
arrFolders = Array( _
	"C:\Temp\Temp", _
	"C:\Temp\Temp", _
	"C:\Temp\Temp" _
	)
 
Set objShell = CreateObject("WScript.Shell")
strBlatPath = "C:\Jobs\Blat.exe"
strServer = "mail.spr.com"
strTo = "glenn@spr.com"
strFrom = "admin@spr.com"
strSubject = "Orphaned Files"
	
strBody = "Please see below the folder details:"
For Each strFolder In arrFolders
	intCount = CountFiles(strFolder)
	strBody = strBody & VbCrLf & strFolder & " has " & intCount & " files in it."
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
strBodyFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "BodyText.txt"
Set objBody = objFSO.CreateTextFile(strBodyFile, True)
objBody.Write strBody
objBody.Close
Set objBody = Nothing
strCommand = "cmd /k " & strBlatPath & " """ & strBodyFile & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 1, True
objFSO.DeleteFile strBodyFile, True
MsgBox "Blat has been run."
 
Function CountFiles(strFolderPath)
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Const intForReading = 1
	strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
	strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strFolderPath).ShortPath & " > """ & strTempFile & """"
	objShell.Run strCommand, 0, True
	Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
	intCount = 0
	While Not objFile.AtEndOfStream
		objFile.SkipLine
		intCount = intCount + 1
	Wend
	objFile.Close
	objFSO.DeleteFile strTempFile, True
	CountFiles = intCount
End Function

Open in new window

Author

Commented:
Hey Rob,

It works great but it will not close. I have to endtask on the blat process.
Wow, new problem!
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Does blat actually finish, and return to the command prompt?  If not, I have no idea how we can resolve that....I'm not sure what's wrong with blat in that case...do you have the latest version?

If Blat *does* finish, and it's just the command prompt that stays open, change this bit:

strCommand = "cmd /k " & strBlatPath & " """ & strBodyFile & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 1, True

to this

strCommand = "cmd /c " & strBlatPath & " """ & strBodyFile & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 0, True


Then just check to make sure in Task Manager that Blat.exe is not still running.

Regards,

Rob.

Author

Commented:
OK - I figured out what is hanging the process. After the script runs, a little VB dialouge pops up and says:  BLAT HAS BEEN RUN.

This is what is preventing this script from ending.

DO you have any other "Awesome" suggestions?

Author

Commented:
GOT IT!

The line:  MsgBox "Blat has been run."
was the cluprit that hung the process as it was wiating for me to HIT ok.

Thanks - I think I am good to go here. :)
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Oh yeah, sure, just change this line
MsgBox "Blat has been run."

to this
'MsgBox "Blat has been run."

and that line won't run, and the script will finish silently.

Regards,

Rob.

Author

Commented:
Rob,

I am about to close this and accept the solution. Before I do, allow me to ask one more question:
Can I take the same script and add more folders, and also with those folders have it search for specific files types?  For example:

arrFolders = Array( _
      "C:\Temp1\Temp\*.tiff", _
      "C:\Temp2\Temp\*.tmp", _
                      "C:\Temp3\Temp\*.tmp", _
      "C:\Temp4\Temp" _
      )
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Yes to adding more folder, but not yet for searching for specific file types....that would need a small change....I'll do it now...

Regards,

Rob.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
OK, here you go.

Regards,

Rob.
arrFolders = Array( _
	"C:\Temp\Temp;*.vbs", _
	"C:\Temp\Temp\;*.vbs", _
	"C:\Temp\Temp\", _
	"C:\Temp\Temp" _
	)
 
Set objShell = CreateObject("WScript.Shell")
strBlatPath = "C:\Jobs\Blat.exe"
strServer = "mail.spr.com"
strTo = "glenn@spr.com"
strFrom = "admin@spr.com"
strSubject = "Orphaned Files"
	
strBody = "Please see below the folder details:"
For Each strFolder In arrFolders
	intCount = -1
	strFolderSpec = ""
	strFileSpec = ""
	If InStr(strFolder, ";") > 0 Then
		strFolderSpec = Split(strFolder, ";")(0)
		strFileSpec = Split(strFolder, ";")(1)
	Else
		strFolderSpec = strFolder
		strFileSpec = Null
	End If
	intCount = CountFiles(strFolderSpec, strFileSpec)
	If strFileSpec <> "" Then
		strBody = strBody & VbCrLf & strFolderSpec & " has " & intCount & " " & strFileSpec & " files in it."
	Else
		strBody = strBody & VbCrLf & strFolderSpec & " has " & intCount & " files in it."
	End If
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
strBodyFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "BodyText.txt"
Set objBody = objFSO.CreateTextFile(strBodyFile, True)
objBody.Write strBody
objBody.Close
Set objBody = Nothing
strCommand = "cmd /k " & strBlatPath & " """ & strBodyFile & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
'objShell.Run strCommand, 1, True
objFSO.DeleteFile strBodyFile, True
WScript.Echo strBody
MsgBox "Blat has been run."
 
Function CountFiles(ByVal strTheFolder, ByVal strFileType)
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Const intForReading = 1
	strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
	If IsNull(strFileType) = False Then
		strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strTheFolder).ShortPath & "\" & strFileType & " > """ & strTempFile & """"
	Else
		strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strTheFolder).ShortPath & " > """ & strTempFile & """"
	End If
	objShell.Run strCommand, 0, True
	Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
	intCount = 0
	While Not objFile.AtEndOfStream
		objFile.SkipLine
		intCount = intCount + 1
	Wend
	objFile.Close
	objFSO.DeleteFile strTempFile, True
	CountFiles = intCount
End Function

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Ooops, I left the BLAT bit commented out....use this instead.

Regards,

Rob.
arrFolders = Array( _
	"C:\Temp\Temp;*.vbs", _
	"C:\Temp\Temp\;*.vbs", _
	"C:\Temp\Temp\", _
	"C:\Temp\Temp" _
	)
 
Set objShell = CreateObject("WScript.Shell")
strBlatPath = "C:\Jobs\Blat.exe"
strServer = "mail.spr.com"
strTo = "glenn@spr.com"
strFrom = "admin@spr.com"
strSubject = "Orphaned Files"
	
strBody = "Please see below the folder details:"
For Each strFolder In arrFolders
	intCount = -1
	strFolderSpec = ""
	strFileSpec = ""
	If InStr(strFolder, ";") > 0 Then
		strFolderSpec = Split(strFolder, ";")(0)
		strFileSpec = Split(strFolder, ";")(1)
	Else
		strFolderSpec = strFolder
		strFileSpec = Null
	End If
	intCount = CountFiles(strFolderSpec, strFileSpec)
	If strFileSpec <> "" Then
		strBody = strBody & VbCrLf & strFolderSpec & " has " & intCount & " " & strFileSpec & " files in it."
	Else
		strBody = strBody & VbCrLf & strFolderSpec & " has " & intCount & " files in it."
	End If
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
strBodyFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "BodyText.txt"
Set objBody = objFSO.CreateTextFile(strBodyFile, True)
objBody.Write strBody
objBody.Close
Set objBody = Nothing
strCommand = "cmd /k " & strBlatPath & " """ & strBodyFile & """ -server " & strServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 1, True
objFSO.DeleteFile strBodyFile, True
'MsgBox "Blat has been run."
 
Function CountFiles(ByVal strTheFolder, ByVal strFileType)
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Const intForReading = 1
	strTempFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempCount.txt"
	If IsNull(strFileType) = False Then
		strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strTheFolder).ShortPath & "\" & strFileType & " > """ & strTempFile & """"
	Else
		strCommand = "cmd /c dir /a-d /s /b " & objFSO.GetFolder(strTheFolder).ShortPath & " > """ & strTempFile & """"
	End If
	objShell.Run strCommand, 0, True
	Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
	intCount = 0
	While Not objFile.AtEndOfStream
		objFile.SkipLine
		intCount = intCount + 1
	Wend
	objFile.Close
	objFSO.DeleteFile strTempFile, True
	CountFiles = intCount
End Function

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I wish I could accept all as a solutions, but I'll just pick the last one!
Thanks for your diligence and patience.

Fortitude is your new middle name!

Author

Commented:
HIghest Level of Excellence from Rob.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Thanks for the grade.

Regards,

Rob.

Author

Commented:
Rob - There is a new question posted if you are interested. :)
ID: 24204032
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.