Need some advanced help with this task!

nnCron and nnCron LITE discussion

Need some advanced help with this task!

Postby LuckMan212 » Sun, 20 Nov 2005, 18:19

greetings Valery, Nicholas:
I am hoping you can provide me with some help, I have some advanced questions on this task I have been working on. It is a task I mentioned a couple weeks ago in a different post here. But I have gotten it working 99% it just needs a little more tweaking.

First I will summarize what the task is supposed to do, and then I will have my questions at the bottom.

Task functions:
1) read in a text file called "procs_that_use_temp.txt" line by line and check to see if any of the processes in that file are running.
2) read in a text file called "temp_folders.txt" and check each folder listed if it is greater than 100Mb (subfolders included).
3) if any folder is >100mb, then either:
a) pop up a balloon hint (if no processes from step#1 were running) which shows which folder is 'oversize' and if user clicks it then explorer.exe opens the folder -or-
b) if a process from step#1 was running, then just show a generic balloon that a large folder was found but will be ignored because a process may be using it

All of this is already working in my task below. I am trying to add a couple of features and solve 1 problem also so that is where I need your help if you can.

My questions:
1) I would like to make the format of the "temp_folders.txt" file like this:
Code: Select all
c:\path\to\some\folder,100,1
c:\path\to\a\different\folder,500,0

where in this case the first parameter is the folder path (obvious) and the 2nd parameter is the max. Megabytes (quota) to use when checking (instead of default 100mb for all folders), and the last parameter is a boolean 1 or 0 to decide whether it matters to check against a running process or not.

So in that example the first folder will check if its >100mb and if any running processes from the "procs_that_use_temp" file are running. The 2nd folder will be checked if its >500mb and doesn't care even if there is a process running it will still show the balloon.

2)In the first part of the program where it is looping to check for running processes, it would be more efficient to break out of the loop as soon as any process from the list is found. I cannot figure out a way to forcefully exit from a BEGIN - WHILE - REPEAT loop... can you tell me such a way? (I have already tried "LEAVE" and it gave an error)

3) Can you tell me how to test for an expression like (a and b) and (c and d) ? would it be
Code: Select all
a b and c d and and
I do not think that is right...?

4) If my task is run twice in a row, the 2nd time I get an error from nnCron about "invalid file handle". I guess the first instance of the task is closing the file handle while the 2nd instance is still reading from it. How can I get each instance of the task to open its own file handle? or as alternate, how can I make sure not to close file handle if another task is using it?

And lastly Here is The task so far:
Code: Select all
#( check_temp_folders
AsLoggedUser
Time: 15 */4 * * * *
VARIABLE temp_list
VARIABLE proc_list
VARIABLE someProcsRunning
VARIABLE warnButDoNothing
VARIABLE size_in_mb
VARIABLE size_show
2VARIABLE size
CREATE size_type 66 ALLOT
CREATE cur_folder 258 ALLOT
CREATE cur_proc 258 ALLOT
: user_click
   warnButDoNothing @ 0= IF
      0 S" explorer.exe /e,/root, %QUOTE%%cur_folder COUNT%%QUOTE%" EVAL-SUBST GUIStartApp DROP
   ELSE
      NOOP
   THEN
;
Action:
someProcsRunning OFF
warnButDoNothing OFF

FILE-EMPTY: "procs_that_use_temp.txt" NOT FILE-EMPTY: "temp_folders.txt" NOT AND
IF
   \ check for running processes that use temp folders
   S" procs_that_use_temp.txt" R/O OPEN-FILE-SHARED THROW proc_list !
   BEGIN cur_proc 1+ 255 proc_list @ READ-LINE THROW WHILE
      cur_proc C!
      \ MSG: "process `%cur_proc COUNT%` checking"
      PROC-EXIST: "%cur_proc COUNT%" IF
         \ MSG: "process `%cur_proc COUNT%` IS running"
         someProcsRunning ON
         \ LEAVE
      THEN
   REPEAT
   DROP
   proc_list @ CLOSE-FILE DROP

   \ check temp folder sizes
   S" temp_folders.txt" R/O OPEN-FILE-SHARED THROW temp_list !
   BEGIN cur_folder 1+ 255 temp_list @ READ-LINE THROW WHILE
      cur_folder C!
      RECURSIVE DIR-SIZE: "%cur_folder COUNT%" size 2!
      size 2@ 1048576 UM/MOD size_in_mb ! DROP
      \ MSG: "size of %cur_folder COUNT% (mb) is: %size_in_mb @%"
      100 size_in_mb @ < IF
         \ There is a folder > 100mb, now fork depending on whether apps are running
         someProcsRunning @ 0= IF
            1000 size_in_mb @ < IF
               size_in_mb @ 1024 / size_show !
               S" Gb" size_type PLACE
               \ MSG: "type is now: %size_type COUNT%"
            ELSE
               size_in_mb @ size_show !
               S" Mb" size_type PLACE
            THEN
            2 BalloonIcon !
            ['] user_click OnBalloonClick !
            BALLOON: "System Notification" "`%cur_folder COUNT%` contains %size_show @% %size_type COUNT% of data that may no longer be in use.  Click here to open the folder so you can review and delete files as needed."
            PAUSE: 10000
            BALLOON: "" ""
         ELSE
            warnButDoNothing ON
            \ LEAVE
         THEN
      THEN
   REPEAT
   DROP
   temp_list @ CLOSE-FILE DROP
   warnButDoNothing @ IF
      1 BalloonIcon !
      BALLOON: "System Notification" "At least one of the temp folders being monitored is over the 100Mb quota.  However, an application is running that may be using these files, so cleanup is not required at this time."
      PAUSE: 10000
      BALLOON: "" ""
   THEN
ELSE
   3 BalloonIcon !
   BALLOON: "Folder Monitor Error" "At least one of the following files is missing or empty:%CRLF%%CRLF%procs_that_use_temp.txt%CRLF%temp_folders.txt"
   PAUSE: 10000
   BALLOON: "" ""
THEN
)#
Thank you greatly for this help!! :D
LuckMan212
 
Posts: 133
Joined: Mon, 04 Jul 2005, 11:19

Postby Valery_Kondakoff » Sun, 20 Nov 2005, 20:41

1) So, you just want to parse this string? I think you could try the regexps: http://www.nncron.ru/help/EN/commands/regexp.htm

2) Just add an another condition (process exists?) before the WHILE. This will force the loop to stop.

3) Your example is correct. Just don't forget to type the 'AND' in an upper case.

4) Why do you allow multiple instances of this task running simultaneously? Use the SingleInstance option to allow only one task instance at the time.

HTH!
CU
VK
User avatar
Valery_Kondakoff
Site Admin
 
Posts: 769
Joined: Thu, 01 Jul 2004, 20:49
Location: Moscow, Russia

Postby LuckMan212 » Sun, 20 Nov 2005, 20:58

Hey there Valery thank you for the reply!
Valery_Kondakoff wrote:1) So, you just want to parse this string? I think you could try the regexps:
ok I will try them, looks like this will work, thanks!!

Valery_Kondakoff wrote:2) Just add an another condition (process exists?) before the WHILE. This will force the loop to stop.
so you mean something like:
Code: Select all
BEGIN cur_proc 1+ 255 proc_list @ READ-LINE THROW someProcsRunning @ 0= AND WHILE
?? is this what you mean?

Valery_Kondakoff wrote:4) Why do you allow multiple instances of this task running simultaneously? Use the SingleInstance option to allow only one task instance at the time.
yes I can do this but my question is more about in general, theory--- how can I avoid this or check for open file handles, is it possible? or is SingleInstance the only way? (for example what about two separate tasks that run concurrently and both are accessing the same file-- then what? thank you :D
LuckMan212
 
Posts: 133
Joined: Mon, 04 Jul 2005, 11:19

Postby LuckMan212 » Mon, 21 Nov 2005, 01:20

ok well just an update, I have now the task working 100% the way I want it. My code is below if anyone maybe can benefit from my many hours of tearing my hair out. Now I have a bald head but at least my task is working :D

only thing left to do now is optimize it, I think it still is not so efficient and is probably coded wrong especially in regards to the variable declarations and probably other errors too, maybe Valery or Nicholas can point out my mistakes [hint-hint :oops: ] ...... but for now, it is at least working :)
Code: Select all
#( check_temp_folders
AsLoggedUser
SingleInstance
Time: 15 */4 * * * *
VARIABLE temp_list
VARIABLE proc_list
VARIABLE someProcsRunning
VARIABLE warnButDoNothing
VARIABLE force_check
VARIABLE size_in_mb
VARIABLE cur_quota
VARIABLE size_show
2VARIABLE size
CREATE size_type 66 ALLOT
CREATE cur_line 258 ALLOT
CREATE cur_proc 258 ALLOT
CREATE cur_folder 258 ALLOT
: user_click
   warnButDoNothing @ 0= IF
      S" explorer.exe /e,/root, %QUOTE%%cur_folder COUNT%%QUOTE%" EVAL-SUBST GUIStartApp
   ELSE
      NOOP
   THEN
;
Action:
someProcsRunning OFF
warnButDoNothing OFF
force_check OFF

FILE-EMPTY: "procs_that_use_temp.txt" NOT FILE-EMPTY: "temp_folders.txt" NOT AND
IF
   \ check for running processes that use temp folders
   S" procs_that_use_temp.txt" R/O OPEN-FILE-SHARED THROW proc_list !
   BEGIN cur_proc 1+ 255 proc_list @ READ-LINE THROW someProcsRunning @ 0= AND WHILE
      cur_proc C!
      \ MSG: "process `%cur_proc COUNT%` checking"
      PROC-EXIST: "%cur_proc COUNT%" IF
         \ MSG: "process `%cur_proc COUNT%` IS running"
         someProcsRunning ON
      THEN
   REPEAT
   DROP
   proc_list @ CLOSE-FILE DROP

   \ check temp folder sizes
   S" temp_folders.txt" R/O OPEN-FILE-SHARED THROW temp_list !
   BEGIN cur_line 1+ 255 temp_list @ READ-LINE THROW warnButDoNothing @ 0= AND WHILE
      cur_line C!
      RE-MATCH: "%cur_line COUNT%" "/(.*):(\d+):(\d)/"
      IF
         \ MSG: "$1 = %$1%" \ $1 = folder name
         \ MSG: "$2 = %$2%" \ $2 = megs
         \ MSG: "$3 = %$3%" \ $3 = if =1 then show warning regardless of someProcsRunning flag
         S" %$1%" EVAL-SUBST cur_folder PLACE
         S" %$2%" EVAL-SUBST S>NUM cur_quota !
         S" %$3%" EVAL-SUBST S>NUM force_check !
         RECURSIVE DIR-SIZE: "%cur_folder COUNT%" size 2!
         size 2@ 1048576 UM/MOD size_in_mb ! DROP
         \ MSG: "size of %cur_folder COUNT% (mb) is: %size_in_mb @%"
         cur_quota @ size_in_mb @ < IF
            \ There is a folder over its quota, now fork depending on whether apps are running
            someProcsRunning @ 0= force_check @ 1 = OR IF
               1000 size_in_mb @ < IF
                  size_in_mb @ 1024 / size_show !
                  S" Gb" size_type PLACE
                  \ MSG: "type is now: %size_type COUNT%"
               ELSE
                  size_in_mb @ size_show !
                  S" Mb" size_type PLACE
               THEN
               2 BalloonIcon !
               ['] user_click OnBalloonClick !
               BALLOON: "System Notification" "`%cur_folder COUNT%` contains %size_show @% %size_type COUNT% of data and is over its defined quota.  Click here to open the folder so you can review and delete files as needed."
               PAUSE: 10000
               BALLOON: "" ""
            ELSE
               warnButDoNothing ON
            THEN
         THEN
      ELSE
         MSG: "Error found while parsing the following line:%crlf%%crlf%` %$0% `"
      THEN
   REPEAT
   DROP
   temp_list @ CLOSE-FILE DROP
   warnButDoNothing @ IF
      1 BalloonIcon !
      BALLOON: "System Notification" "At least one of the temp folders being monitored is over its defined quota.  However, an application is running that may be using these files, so cleanup is not required at this time."
      PAUSE: 10000
      BALLOON: "" ""
   THEN
ELSE
   3 BalloonIcon !
   BALLOON: "Folder Monitor Error" "At least one of the following files is missing or empty:%CRLF%%CRLF%procs_that_use_temp.txt%CRLF%temp_folders.txt"
   PAUSE: 10000
   BALLOON: "" ""
THEN
)#

and here is the contents of my "procs_that_use_temp.txt" file which is placed in the nnCron folder:
Code: Select all
Rebuilder.exe
CloneDVD2.exe
DivxToDvd.exe
eMule.exe


and here is the format of the "temp_folders.txt" file, also placed in nnCron folder:
(remember to substitute your username where indicated)
Code: Select all
C:\CloneDVDTemp:1:0
C:\Temp\DVD:100:0
C:\Video:50:1
C:\Documents and Settings\<your username>\Local Settings\Temp:50:1
X:\Temp:10:1

enjoy :P
LuckMan212
 
Posts: 133
Joined: Mon, 04 Jul 2005, 11:19


Return to nnCron forum (English)

Who is online

Users browsing this forum: No registered users and 2 guests