[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 757
  • Last Modified:

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

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.
0
GWMerrell
Asked:
GWMerrell
  • 14
  • 12
  • 2
  • +1
1 Solution
 
RobSampsonCommented:
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.
0
 
GWMerrellAuthor Commented:
I am looking to find out if any files have been deposited in that directory by the time the script is ran.
0
 
BSonPoshCommented:
Something like this (powershell)
$date = Get-Date $LastTimeItRan
dir $path -rec | ?{$_.LastWriteTime -gt $date}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LearnctxEngineerCommented:
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

0
 
RobSampsonCommented:
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

0
 
GWMerrellAuthor 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?
0
 
BSonPoshCommented:
Powershell doesn't care whether the path is local or UNC... it acts the same.
0
 
GWMerrellAuthor 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?
0
 
GWMerrellAuthor Commented:
BSonPosh,

I am testing your results...
0
 
RobSampsonCommented:
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

0
 
GWMerrellAuthor 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?
0
 
RobSampsonCommented:
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

0
 
GWMerrellAuthor 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>

0
 
RobSampsonCommented:
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

0
 
GWMerrellAuthor 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?
0
 
RobSampsonCommented:
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

0
 
GWMerrellAuthor Commented:
Hey Rob,

It works great but it will not close. I have to endtask on the blat process.
Wow, new problem!
0
 
RobSampsonCommented:
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.
0
 
GWMerrellAuthor 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?
0
 
GWMerrellAuthor 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. :)
0
 
RobSampsonCommented:
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.
0
 
GWMerrellAuthor 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" _
      )
0
 
RobSampsonCommented:
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.
0
 
RobSampsonCommented:
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

0
 
RobSampsonCommented:
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

0
 
GWMerrellAuthor 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!
0
 
GWMerrellAuthor Commented:
HIghest Level of Excellence from Rob.
0
 
RobSampsonCommented:
Thanks for the grade.

Regards,

Rob.
0
 
GWMerrellAuthor Commented:
Rob - There is a new question posted if you are interested. :)
ID: 24204032
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 14
  • 12
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now