#76988 - 2003-10-07 11:51 PM
Detect New File Presence
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
|
Hello gents...
Got a request to create a script that will run continuously and give an alert whenever a new file is detected in a certain directory.
I have written the below that seems to work...Except for when the newest file is deleted..It then thinks the next newest (before the one deleted) is a new file.
I cant seem to think of a way to get around it. Any suggestions? Or if anyone know of a better way to do it...please let me know.
$directory = "L:\ScannedDocs\" Dim $file, $filedate, $line, $newfile, $newfiledate, $checks While @Error = 0 $dirfile = "%temp%\dirlist.xxx" If Exist ("$dirfile") Del "$dirfile" Endif $dirhandle = FreeFileHandle () Shell '%comspec% /c dir $directory /b /o:d > $dirfile' If Open ($dirhandle, $dirfile) = 0 $line = ReadLine ($dirhandle) If Len ("$line") > 0 $file = $line Endif While @Error = 0 $line = ReadLine ($dirhandle) If Len ("$line") > 0 $file = $line Endif Loop $filedate = GetFileTime ("$directory$file") $null = Close ($dirhandle) Del "$dirfile" Else $null = messagebox ("There was an error getting dir of "+$directory,"Error !!",0) Exit 1 Endif If $newfile <> $file or $newfiledate <> $filedate If $file <> "" and $filedate <> "" $newfile = $file $newfiledate = GetFileTime ("$directory$newfile") If $checks > 0 $null = messagebox ("There is a new file in the '$directory' folder"+@CRLF+"$newfile -- $newfiledate","New File !!",0) Endif Endif Endif $checks = $checks+1 Sleep 15 Loop
[ 07. October 2003, 23:55: Message edited by: CitrixMan ]
|
|
Top
|
|
|
|
#76991 - 2003-10-08 01:24 AM
Re: Detect New File Presence
|
Richie19Rich77
Seasoned Scripter
   
Registered: 2002-08-16
Posts: 624
Loc: London, England
|
Here we go abit rough at the edges but it seems to work.
;Script: Detect New File Presence ;Author: Richard Farthing@nhs.net ;Company: Queen Elizabeth Hospital NHS Trust ;Date: 08/10/03 ;Requirements: DirList Function
;Problems: When the message box appears telling the user that a ; new file has been created, it stops the script, ; so any new files added between clicking on OK will not be detected. ;***************************************************************************************
Break On
;Directory that you want to scan. $Directory = "C:\ScannedDocs"
;KiX Function DirList() Location Call 'D:\KiX\Functions\DirList()'
;Checking that scan directory exists If NOT Exist ($Directory) $null = MessageBox ("Error reading directory path","Error: "+@ERROR,0) Exit 1 EndIf
$DirList = DIRLIST($Directory+'\*.*',1+2) $FileCount = Ubound($dirlist) + 1 $Currentdirlist = DIRLIST($Directory+'\*.*',1)
While @Error = 0 ;Checking that scan directory exists within the loop If NOT Exist ($Directory) $null = MessageBox ("Error reading directory path","Error: "+@ERROR,0) Exit 1 EndIf $Current = $FileCount $dirlist = DIRLIST($Directory+'\*.*',1) $FileCount = Ubound($dirlist) + 1 Select ;Case statement for new files Case $FileCount > $Current $Count = Val(Ubound($dirlist)) + 1 ? 'File Added. Number of File(s): '+$Count For Each $File In $dirlist If AScan($Currentdirlist,$File) = "-1" $newfiledate = GetFileTime ("$directory\$File") $null = MessageBox ("There is a new file in the '$directory' folder"+@CRLF+"'$File' -- '$newfiledate'","New File !!",0) EndIf Next ;Case statement for deleted files Case $FileCount < $Current $Count = Val(Ubound($dirlist)) + 1 ? 'File Deleted. Number of File(s): '+$Count
EndSelect $Currentdirlist = DIRLIST($Directory+'\*.*',1) Sleep 2 Loop ;***************************************************************************************
[ 08. October 2003, 01:49: Message edited by: Richard Farthing ]
|
|
Top
|
|
|
|
#76993 - 2003-10-08 04:14 AM
Re: Detect New File Presence
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
Turned the above script into a function.
Action: Pauses script until a new file appears in the target directory.
Returns: Filename
Break On
$nul = SetOption("WrapAtEOL","On") $nul = SetOption("Explicit","On")
Dim $,$sFile
$sFile = fnFileCreationMon("E:\Scripts") @ERROR " | " @SERROR ?
$sFile ?
Get $
Function fnFileCreationMon($sFolder, Optional $sComputer, Optional $lWithin) If Not Exist($sFolder) Exit(3) EndIf If Not $sComputer $sComputer = "." EndIf If Not $lWithin $lWithin = 10 EndIf If $lWithin < 0 Exit(1) EndIf Dim $objWMI,$colMonEvents,$objEvent $sFolder = Join(Split($sFolder,"\"),"\\\\") $objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $sComputer + "\root\cimv2") If @ERROR<0 Exit VAL("&"+Right(DecToHex(@ERROR),4)) EndIf $colMonEvents = $objWMI.ExecNotificationQuery ("SELECT * FROM __InstanceCreationEvent " + "WITHIN " + $lWithin + " WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and " + "TargetInstance.GroupComponent='Win32_Directory.Name=" + '"' + $sFolder + '"' + "'") If @ERROR<0 Exit VAL("&"+Right(DecToHex(@ERROR),4)) EndIf $objEvent = $colMonEvents.NextEvent $fnFileCreationMon = Join(Split(Split($objEvent.TargetInstance.PartComponent,'"')[1],"\\"),"\") EndFunction
|
|
Top
|
|
|
|
#77004 - 2003-10-08 11:28 AM
Re: Detect New File Presence
|
Richie19Rich77
Seasoned Scripter
   
Registered: 2002-08-16
Posts: 624
Loc: London, England
|
Don't it make you sick, I go to bed thinking I have created a nice small script, then I wake up and lonk had made up a super small script.
And he makes it look so easy.
|
|
Top
|
|
|
|
#77006 - 2003-10-08 02:21 PM
Re: Detect New File Presence
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
I can quickly see this one going to either the FAQ or UDF section. Cool stuff!
Kent
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 641 anonymous users online.
|
|
|