Clicky

Clean up your environment variables with a simple Ruby script

The more you install and uninstall software, the more your environment variables get filled with dead paths.

Just now, while I was looking through my environment variables for no apparent reason, I found some dead links. So, I decided to clean them up.

An example of a dead path in my environment variables
I uninstalled Hyper terminal a long time ago but its path is still in my environment variable

What is an Environment Variable

An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process

To explain further, an environment variable simply contains values which help processes to function. For example, to find the directory in which software is installed, you would refer to the PROGRAMFILES environmental variable which contains the path to the Program Files directory or to find your operating system’s temporary directory, you would refer to the TEMP variable which contains the path to the operating system’s temporary directory. Environmental variables can also store other information such as your computer’s username or processor type.

The Path Environment Variable

The PATH environment variable is one of the many available environment variables.
It is used by the system to locate the needed executable from the command line. So basically, when we type any command such as dir or move or time, the operating system will search through the paths in the environment variable PATH to locate the needed executable file, if it does not find it, then it returns the ‘command not recognized’ error.

How does it get filled with dead paths?

Whenever we install software that comes along with an executable CLI, its path is added to the environment variable PATH to allow you to access the executable from wherever you are. However, when you uninstall the application, the added path is not removed from your environment variable, so the installing and uninstalling of software will make it end up with a chunk of dead paths.

This can affect the performance of your computer. When you execute a command the operating system searches through the paths listed in the PATH variable until it finds the executable. If there are many dead paths, the search can take longer.

Cleaning it up…

We are going to create a simple Ruby script which will iterate through all the paths in the environment variable PATH and remove the dead links. 
In Ruby, all your environment variables are available under a single object called ENV
Use the code below to see all the environment variables in Ruby.

require "pp"
pp ENV

This will print the environment variables plus their values.

The code for the script is below.

require "win32/registry"

paths = ENV['path']
paths = paths.split(";")

valid = Array.new
valid_string = ""

paths.each do |x|
  valid.push(x) if Dir.exists? x
end

valid.each do |x|
  valid_string += "#{x};"
end

Win32::Registry::HKEY_CURRENT_USER.open('Environment',Win32::Registry::KEY_WRITE) do |reg|
  reg['path'] = valid_string
end

First of all, we imported the "win32/registry" module. Then we assigned the environment variable ENV['path'] to a variable called paths. This returns a string.

Then we split it by ’;’ since all the paths in the variable are concatenated and separated by ’;‘. This will turn the paths variable into an array.
We then created a new array called valid (this is the array which will hold the valid paths).
The valid_string variable will also hold the stringified version of the valid array.

Then to the iteration, we iterate through each item in the paths array, and then we use Dir.exists? to check if the path exists, if it does, it is added to the valid array.
Then we iterate through the valid array and add each path to the valid_string variable.

In the next part, we open the registry and access the environment, we then assign our new environment variable and that’s all.
Our environment variable is good as new😊.