HELP! please I need to convert large number, divide by 1024

nnCron and nnCron LITE discussion

HELP! please I need to convert large number, divide by 1024

Postby LuckMan212 » Wed, 09 Nov 2005, 11:02

hello,
I have made the following task that checks (every 4 hours) for 'temp' files that are leftover from dvd conversions, etc. The task reads a list of folders to check from a text file called "temp_folders.txt" and then also reads a file called "procs_that_use_temp.txt" and checks to see if any processes are running from that list-- if they are, then the task does nothing (it assumes that the process is still using/creating those files)

if it finds a folder with files totalling >100mb, a balloon hint is shown and the folder is opened via windows explorer so you can clean up.
here is the task:
Code: Select all
#( check_temp_folders
AsLoggedUser
Time: * */4 * * * *
VARIABLE temp_list
VARIABLE proc_list
VARIABLE someProcsRunning
2VARIABLE size
CREATE cur_folder 258 ALLOT
CREATE cur_proc 258 ALLOT
Action:
   someProcsRunning OFF
   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
   THEN
    REPEAT
    DROP
    proc_list @ CLOSE-FILE DROP

   someProcsRunning @ 0=
   IF
      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!
      104857600. size 2@ D<
      IF
         3 BalloonIcon !
         BALLOON: "System Notification" "`%cur_folder COUNT%` contains %size 2@ <# #S #>% bytes that may no longer be in use.  Please review and delete files as needed."
         START-APP: explorer.exe /e,/root, "%cur_folder COUNT%"
         PAUSE: 5000
         BALLOON: "" ""
      THEN
      REPEAT
      DROP
      temp_list @ CLOSE-FILE DROP
   THEN
)#

the task works OK, but I would like to display folder sizes in MB instead of bytes, because it is easier to read. I have tried various things like
Code: Select all
size @2 1024. / size_in_mb !

and also
Code: Select all
size @2 1024. UM/MOD size_in_mb !

etc etc, but these dont work or give stack errors. I am not really Forth expert (obvious) so can you please help me to convert this double number into a single and display it as megabytes?
thanks!! :wink:
LuckMan212
 
Posts: 133
Joined: Mon, 04 Jul 2005, 11:19

Postby VK » Wed, 09 Nov 2005, 12:13

UM/MOD is a common Forth word for dividing a double number dividend by a single number divisor. It gives you two single numbers on stack: quotient and remainder. You can find more just by using Google: http://www.google.com/search?hl=en&lr=& ... tnG=Search .

Now you should use nnCron console for some experimenting (Tools - Console).

Enter '1024. 2 UM/MOD .S' and press ENTER. What do you see? '0 512 Ok..'. Here are two values on a stack. So you should explicitely remove the second value (using 'DROP') after you use the first one.

Smth like this:
Code: Select all
size @2 1024 UM/MOD size_in_mb ! DROP


HTH!
CU
VK
User avatar
VK
 
Posts: 680
Joined: Wed, 14 Jul 2004, 19:17

Postby LuckMan212 » Wed, 09 Nov 2005, 17:14

Code: Select all
#( check_temp_folders
AsLoggedUser
Time: 15 */4 * * * *
VARIABLE temp_list
VARIABLE proc_list
VARIABLE someProcsRunning
VARIABLE size_in_mb
2VARIABLE size
CREATE cur_folder 258 ALLOT
CREATE cur_proc 258 ALLOT
Action:
   someProcsRunning OFF
   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
   THEN
    REPEAT
    DROP
    proc_list @ CLOSE-FILE DROP

   someProcsRunning @ 0=
   IF
      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 1024 UM/MOD size_in_mb ! DROP
      104857600. size 2@ D<
      IF
         3 BalloonIcon !
         BALLOON: "System Notification" "`%cur_folder COUNT%` contains %size_in_mb @ <# #S #>% bytes that may no longer be in use.  Please review and delete files as needed."
         START-APP: explorer.exe /e,/root, "%cur_folder COUNT%"
         PAUSE: 5000
         BALLOON: "" ""
      THEN
      REPEAT
      DROP
      temp_list @ CLOSE-FILE DROP
   THEN
)#

I tried this and I get a CRONTAB ERROR when I try to start nnCron. It says "stack has garbage (24)"
:roll:
LuckMan212
 
Posts: 133
Joined: Mon, 04 Jul 2005, 11:19

Postby Valery_Kondakoff » Wed, 09 Nov 2005, 18:09

maybe just because there was an error in your initial example (@2 instead 2@) which was not fixed by me?
CU
VK
User avatar
Valery_Kondakoff
Site Admin
 
Posts: 769
Joined: Thu, 01 Jul 2004, 20:49
Location: Moscow, Russia

Postby LuckMan212 » Wed, 09 Nov 2005, 19:27

ok yes thank you Valery :D i thought you were checking... I sure was not!!

anyway I have it working 99% now:
Code: Select all
#( check_temp_folders
AsLoggedUser
Time: 15 */4 * * * *
VARIABLE temp_list
VARIABLE proc_list
VARIABLE someProcsRunning
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
   START-APP: explorer.exe /e,/root, "%cur_folder COUNT%"
;
Action:
   someProcsRunning OFF
   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
   THEN
    REPEAT
    DROP
    proc_list @ CLOSE-FILE DROP

   someProcsRunning @ 0=
   IF
      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
         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: 5000
         BALLOON: "" ""
      THEN
      REPEAT
      DROP
      temp_list @ CLOSE-FILE DROP
   THEN
)#

only problem is when user clicks balloon, explorer does not open correct folder, but opens Root C: drive instead. I think it is because of postfix notation for GuiStartApp I cannot enclose the %cur_folder COUNT% in quotes like they need to be. If I do
Code: Select all
: user_click
0 S" explorer.exe /e,/root, "%cur_folder COUNT%"" GUIStartApp DROP
;

then I get crontab errors
and if I do:
Code: Select all
: user_click
START-APP: explorer.exe /e,/root, "%cur_folder COUNT%"
;
then I get `nnCron service has generated erros and will now be closed` from Windows :cry:

can you please give me one last bit of help to solve this? thanks!! :D
LuckMan212
 
Posts: 133
Joined: Mon, 04 Jul 2005, 11:19

Postby LuckMan212 » Wed, 09 Nov 2005, 19:32

never mind! I solved it myself!! :D
Code: Select all
: user_click
   0 S" explorer.exe /e,/root, %QUOTE%%cur_folder COUNT%%QUOTE%" EVAL-SUBST GUIStartApp DROP
;


thank you for pointing me in the right direction!! nnCron is always amazing me how useful it can be :lol:
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 1 guest