37
loading...
This website collects cookies to deliver better user experience
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
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 system's temporary directory. Environmental variables can also store other information such as your computer's username or processor type.dir
or move
or time
, the terminal 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 an error. ENV
.require "pp" #Imports the pretty-print module
pp ENV # pretty prints the env object to stdout
require "win32/registry"
paths = ENV['path'] #Access the path variable and assign to the value 'paths'
paths = paths.split(";") #We split the value by ';' to get the individual paths
valid = Array.new #We create a new array in which we will store the valid paths
valid_string = "" #This will contain the strinfied form of the valid paths
paths.each do |x|
valid.push(x) if Dir.exists? x #We iterate through the paths and append the path to the valid array if it exists
end
valid.each do |x| #We then iterate through the valid array and stringify it
valid_string += "#{x};"
end
Win32::Registry::HKEY_CURRENT_USER.open('Environment',Win32::Registry::KEY_WRITE) do |reg| #We open the environment in the registry and assign our new value
reg['path'] = valid_string
end
ENV['path']
to a variable called paths
. This returns a string.paths
variable to an array.valid
(this is the array which will hold the valid paths).valid_string
variable will also hold the stringified version of the valid array.paths
array, then we use Dir.exists?
to check if the path exists, if it does, it is added to the valid array.valid_string
variable.