How To > Access Windows System Data |
MAXScript lets you access some system information like file attributes, files and directories etc. Still, most areas of the system environment outside of 3ds Max are not directly covered by MAXScript itself. Fortunately, there is also the dosCommand method which lets you execute DOS commands in the command prompt console.
In the following example, we will use the dosCommand method to get the PATH environment variables set in Windows.
Executing External Commands using DOScommand
This is going to be a user-defined function without any arguments.
The dosCommand method expects a string parameter containing the command to be executed. You can supply any command sequences you could type in the Windows Command Prompt. In this case, we execute the PATH command which shows the currently set OS environment paths. We also tell Windows to write the result to a text file called "env.txt". Then we can use this file to read in the results.
We will need an array to store the single path definitions. We will initialize the array to the empty array in the beginning.
Now let’s open the file created by the dosCommand execution. The content of the file will look like this: "PATH=D:\WINDOWS\SYSTEM32;D:\WINDOWS; " etc. The exact paths will of course vary from system to system. We can see that the first path starts after the "=" character and ends at the ";" character. Each path after the first starts after the ";" and ends with ";".
So we skip to the position of the "=" sign to start reading the first path.
Using a while loop which will repeat the code within the brackets until End Of File of in_text has been detected, we will extract all paths found in the file.
Because we know that all all paths are delimited by a ";", we can use the readDelimitedString function to read text fragments from the file. We will store them in the user variable str.
Next we will have to check whether the str variable does not contain the last End Of Line symbol before the End Of File and if it doesn’t, we can add the string to the array of paths.
Now we are ready with the reading and can close the file.
It was just a temporary file so we can delete it from the disk.
Lastly, we can return the array with the collected paths as the result of the function.
As this is a function, we have to call it. It has no parameters, so we just call it with ()
The variable env_paths will contain an array of the environment paths set under Windows.
You could use the same method to develop variations of the script to access other OS-related data like the environment TEMP path name, the name of the Hard Disk Drive etc.