Page 1 of 1 1
Topic Options
#60927 - 2001-11-30 05:12 PM Watch DIR for files and email if new ones arrive
Ryan Breding Offline
Fresh Scripter

Registered: 2001-11-30
Posts: 5
I am trying to write a script to watch a directory for files being placed there by a vendor. When the files arrive I need to automate an email that it was received successfully and is pending validation. Question is where would be a good place to start since I didn't find anything that fit when doing a search through the past topics.
Top
#60928 - 2001-11-30 05:46 PM Re: Watch DIR for files and email if new ones arrive
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
this is a script that I use to watch my users PC's to see if they are installing programs behind my back...

code:

:NTInst ; ****************** Detect Installs ***********************
;
; This code detects a change in the number
; of subdirectories in the $dir directory.
;

; variables
$dir=DIR("c:\program files\*.*")
$contents="$logon\inventory\ProgFls.txt"
$loopctr=0

; determine the current number of subdirectories in $dir
While $dir <> "" and @ERROR = 0
$loopctr=$loopctr+1
$dir=Dir()
Loop

; compare the current number of directories to the last reported number
$oldcount=readprofilestring($contents,"Dir_Count","@wksta")

; if there's a difference, send a message to $DCSA
if $loopctr<>$oldcount
$=sendmessage("$dcsa","The dir count on @wksta has changed from $oldcount to $loopctr")
? " dir count on @wksta has changed from $oldcount to $loopctr"
$=writeprofilestring($contents,"Dir_Count","@wksta","$loopctr")
endif
return


_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#60929 - 2001-11-30 07:16 PM Re: Watch DIR for files and email if new ones arrive
Ryan Breding Offline
Fresh Scripter

Registered: 2001-11-30
Posts: 5
What is in the contents of $logon\inventory\ProgFls.txt ?
Top
#60930 - 2001-11-30 07:30 PM Re: Watch DIR for files and email if new ones arrive
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
this is a sample... basically it is just a count of directories, where a change of value will send an message to me


[Dir_Count]
HEMMENN=24
CARPENTJ=18
GRIGGB=18
ALFIERIS=24
MANNINGV=18
ABRAMSA=23
etc...

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#60931 - 2001-11-30 07:33 PM Re: Watch DIR for files and email if new ones arrive
Ryan Breding Offline
Fresh Scripter

Registered: 2001-11-30
Posts: 5
Are those User Machines and the count of the directories in Program Files ?
Top
#60932 - 2001-12-06 12:48 AM Re: Watch DIR for files and email if new ones arrive
Ryan Breding Offline
Fresh Scripter

Registered: 2001-11-30
Posts: 5
As a followup to this here is what I have come up with by mangling a couple scripts together to get somewhat of where I want to go with it.

First, the script gets a directory listing and outputs that to a file with the name of the file along with the date. Next it checks the count of the files in the directory to see if it has changed. Based on change or no change it sends off an email with the output of the directory listing and then updates the file count for that directory.


;----------- Change status-file to current times for Directory List ------------------------

$IniFile = Dir($workingdir + "\*.*")
While $IniFile <> "" And @Error = 0
$NewTime = GetFileTime($workingdir + "\" + $IniFile)
$
$Err = WriteProfileString($StatusFile, "FileTime", $IniFile, $NewTime)
$IniFile = Dir()
Loop

;---------------Check Count to determine if email needs to be sent---------------------------

$readdir=DIR($workingdir + "\*.*")
$contents=($rootdir + "\count.txt")
$loopctr=0
$adminmachine = "admin1"


While $readdir <> "" and @ERROR = 0
$loopctr=$loopctr+1
$readdir=Dir()
Loop


;-----------compare the current number of directories to the last reported number-------------

$oldcount=readprofilestring($contents,"Dir_Count","@wksta")
;$oldcount=$oldcount-2

;------------if there's a difference, send a message------------------------------------------

if $loopctr<>$oldcount
$=sendmessage("$adminmachine","The dir count on @wksta has changed from $oldcount to $loopctr")
? " dir count on @wksta has changed from $oldcount to $loopctr"
shell "blat.exe $rootdir\inistatus.txt -t user@mail.com -server smtp.mail.com -s subject -f user2@mail.com -noh2 -q"
$=writeprofilestring($contents,"Dir_Count","@wksta","$loopctr")
endif

;------------Cleanup--------------------------------------------------------------------------

del $statusfile

One of the things I would like to do is add filesize into the directory listing, and then change the email around to be more like a form to include disclaimers and the following:

Project Number: XXXXXX
Project Name : XXXXXXXXXXX
File Name : Filename.zip
File Size : Size in bytes
Transfer Date : Long date


Project # and Name would be static variables per directory folder, but File name, size, and date would need to be pulled for each file.

One of my problems however is this script obviously has to be scheduled to run, therefore what if the script runs while a file is being uploaded....obviously it will read the filesize of it at exactly that moment (before it is done) and report the incorrect FINAL size. Is there a way to tell if a file is open/still being completed so that it doesn't give a fales positive until it is acutally completed ?

Top
#60933 - 2001-12-07 06:14 PM Re: Watch DIR for files and email if new ones arrive
Ryan Breding Offline
Fresh Scripter

Registered: 2001-11-30
Posts: 5
Come to find out that if the file is open (Large file that takes awhile to transfer) this process doesn't show the file in the list.

This means that the email needs to be sent AFTER then file is complete and not While it is in transfer.


Any Ideas ?

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 392 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.055 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org