Variable cleanup
Posted by Rustynails
Monday, August 14, 2006
Description: Cleans unused variables from your Variables list

This script cleans up your variables list by looping through all of your scripts (including Aliases.ini and Popups.ini) and collects a list of all of your active variables (Except variables set by %variable. [ $+ [ value ] ]) and displays a log of what variables were found, where they were found (which scripts) and a list of removed variables including their values.
A brand new function that was added is the /restore function to restore all the variables from a supplied log file that you have removed.
Usage: /clean
Usage: /restore [log file]
Example: /restore [17.08.06][05.17.16].log
Special comments: Thanks to ENE|Kyrillos for this idea!



;; /clean alias
alias clean {
  if ($var(*,0) > 0) {
    var %clean::ticks = $ticks
    var %clean::date $+($date([dd.mm.yy]),$time([hh.nn.ss]),.log)
    if (!$isdir(cleanup)) mkdir cleanup
    var %clean::initialize 1 | while (%clean::initialize <= $clean::type) {
      if ($hget($clean::type(%clean::initialize))) hfree $clean::type(%clean::initialize)
      inc %clean::initialize
    }
    var %clean::initialize 1 | while (%clean::initialize <= $clean::type) { hmake $clean::type(%clean::initialize) 100 | inc %clean::initialize }
    var %clean::fopen 1 | while (%clean::fopen <= $calc($script(0) + $alias(0) + 1)) {
      var %clean::file $iif(%clean::fopen > $script(0),$iif(%clean::fopen != $calc($script(0) + $alias(0) + 1),$alias($calc(%clean::fopen - $script(0))),$mircdirPopups.ini),$script(%clean::fopen))
      .fopen $+(clean::file::,%clean::fopen) $+(",%clean::file,")
      while (!$feof($+(clean::file::,%clean::fopen))) {
        var %clean::line = $fread($+(clean::file::,%clean::fopen))
        if ((%clean::line) && ($calc($gettok(%clean::line,0,37) - 1) > 0)) {
          var %clean::get 1
          while (%clean::get <= $calc($gettok(%clean::line,0,37) - 1)) {
            var %clean::variable $+($chr(37),$gettok($gettok($gettok($gettok(%clean::line,$calc(%clean::get + 1),37),1,44),1,41),1,32))
            if ((* !isin %clean::variable) && ($len(%clean::variable) > 1)) {
              if ($+(",$nopath(%clean::file),") !isin $hget(clean::variables,%clean::variable)) {
                hadd clean::variables %clean::variable $iif($hget(clean::variables,%clean::variable),$hget(clean::variables,%clean::variable) $+(",$nopath(%clean::file),"),$+(",$nopath(%clean::file),"))
              }            
              inc %clean::get
            }
            else inc %clean::get
          }
        }
      }
      .fclose $+(clean::file::,%clean::fopen) | inc %clean::fopen
    }
    var %clean::variables 1, %clean::removed 0
    while (%clean::variables <= $var(*,0)) {
      if (!$hget($clean::type(1),$var(*,%clean::variables))) {
        hadd $clean::type(2) $var(*,%clean::variables) $var(*,%clean::variables).value
        hdel $clean::type(1) $var(*,%clean::variables)
        clean::unset $var(*,%clean::variables)
        inc %clean::removed
        if (%clean::variables <= $var(*,0)) dec %clean::variables
      }
      inc %clean::variables
    }
    var %clean::remove 1 | while (%clean::remove <= $hget($clean::type(1),0).item) {
      if ($hget($clean::type(1),%clean::remove).data == $+(",$nopath($script),")) hdel $clean::type(1) $hget($clean::type(1),%clean::remove).item
      if ($+(",$nopath($script),") isin $hget($clean::type(1),%clean::remove).data) hadd $clean::type(1) $hget($clean::type(1),%clean::remove).item $remove($hget($clean::type(1),%clean::remove).data,$+(",$nopath($script),"))
      inc %clean::remove
    }
    var %clean::list 1 | while (%clean::list <= $clean::type) {
      if (%clean::list == 1) write $+(cleanup/,%clean::date) *** NON-REMOVED VARIABLES ***
      if (%clean::list == 2) write $+(cleanup/,%clean::date) *** REMOVED VARIABLES ***
      if ($hget($clean::type(%clean::list),0).item < 1) write $+(cleanup/,%clean::date) $iif(%clean::list == 1,No variables throughout your scripts were kept.,No variables were removed.)
      var %clean::listadd 1
      while (%clean::listadd <= $hget($clean::type(%clean::list),0).item) {
        write $+(cleanup/,%clean::date) Variable: $+($hget($clean::type(%clean::list),%clean::listadd).item,$chr(44)) $iif(%clean::list == 1,Found in:,Value:) $iif(%clean::list == 1,$hget($clean::type(%clean::list),$hget($clean::type(%clean::list),%clean::listadd).item),$hget($clean::type(%clean::type),%clean::listadd).data)
        inc %clean::listadd
      }
      inc %clean::list
    }
    if ($hget(clean::variables)) hfree clean::variables
    echo -a $+([^K],$color(info),*) Removed %clean::removed $iif(%clean::removed == 1,variable,variables) successfully in $calc(($ticks - %clean::ticks) / 1000) seconds..
    if ($exists($+(cleanup/,%clean::date))) echo -a $+([^K],$color(info),*) To view a log file of this process, simple type "/run $+(cleanup/,%clean::date,"..)
  }
  else echo -a $+([^K],$color(info),*) There are no variables in your Variables list..
}
alias -l clean::unset unset $1
alias clean::type {
  if (!$1) return 2
  if ($1 == 1) return clean::variables
  if ($1 == 2) return clean::notfound
}
;; /restore alias
alias restore {
  if ($exists($+(cleanup/,$1))) {
    var %restore::line 1, %restore::add 0, %restore::ticks $ticks
    .fopen restore::file $+(",$scriptdircleanup/,$1,")
    while (!$feof(restore::file)) {
      var %restore::read = $fread(restore::file)
      if (!%restore::start) {
        if (%restore::read == *** REMOVED VARIABLES ***) var %restore::start = 1
      }
      else {
        if ((%restore::read != No variables were removed.) && (%restore::read)) {
          set $gettok($gettok(%restore::read,2,32),-1,44) $gettok(%restore::read,4-,32)
          inc %restore::add
        }
      }
    }
    .fclose restore::file
    echo -a $+([^K],$color(info),*) Restored %restore::add $iif(%restore::add == 1,variable,variables) from log file $+(",cleanup/,$1,") in $calc(($ticks - %restore::ticks) / 1000) seconds..
  }
  else echo -a $+([^K],$color(info),*) Please provide a valid log file..
}
Great idea
Posted by cs2r`nonamee
Monday, August 14, 2006 06:49pm PDT
Lovin' the idea, haven't cleaned my variables in around 2 years. I only wished that it checked all scripts (aliases.ini). Also, the time that it tells you that it takes to delete the variables is incorrect.

[^K]14* Removed 186 variables successfully in 2mins 2secs..

It only took around 15 seconds, but it DID remove 186 variables. This is a script I will definitely use in the future.

I have never thought or heard of this being made so great job ENE|Kyrillos for the idea, and Rustynails for making it.
Fixes
Posted by Rustynails
Monday, August 14, 2006 07:55pm PDT
Fixed. The script now includes Aliases.ini and Popups.ini in the variable search. The time should be fixed as well.
nice
Posted by Log1x
Tuesday, August 15, 2006 12:21am PDT
i like it :)
Script removal!
Posted by Rustynails
Wednesday, August 16, 2006 10:11pm PDT
The script now excludes itself from displaying it's own variables inside of the search!

NEW FIX!
    Before, the script only searched all of your Remote scripts, Aliases.ini, and Popups.ini. As of now, it searches all of your Remote scripts, all of your Alias scripts and your Popups.ini
this is a comment
Posted by bunkahumpa
Thursday, August 17, 2006 10:53am PDT
Works as advertised, good job. I liked the logging idea.
sexy
Posted by Othello
Thursday, August 17, 2006 11:26am PDT
Great script! Love the idea and it works great!
Script restore
Posted by Rustynails
Thursday, August 17, 2006 02:27pm PDT
NEW FIX!
    Now, with the /restore alias you can restore all of the variables lost from a certain log file!

    Example: /restore [17.08.06][05.17.16].log
awesome
Posted by Technique
Thursday, August 31, 2006 01:45am PDT
Very good job rusty, I love the idea :)!
great script
Posted by Sara
Thursday, March 01, 2007 11:39pm PST
love it, works just as you say, i'm definitely keeping it. nice work.
Submit a comment
Oops! You need to login or register before you can post a comment!

ebaum's world