How To - Practical Examples >

# How To ... Access Windows System Data

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.

Related Topics:

System Information

Executing External Commands using DOScommand

External File Methods

FileStream Values

NATURAL LANGUAGE
Create a function to reuse the code in the future.
Execute the Path command from the Command Prompt and redirect output to file.
Read the resulting file and collect all paths between the starting = and the following ; characters.
Store the paths in a list and when ready, return the list as result of the function.
Delete the temporary file to reduce disk pollution.

MAXSCRIPT
    fn getEnvironmentPaths =
    (
     dosCommand "path >env.txt"
     path_array = #()
     in_text = openfile "env.txt"
     skipToString in_text "="
     while not eof in_text do
     (
      str = readDelimitedString in_text ";"
      if str != "\n" then append path_array str
     )
     close in_text
     deleteFile "env.txt"
     path_array
    )

## Step-By-Step

    fn getEnvironmentPaths = (

This is going to be a user-defined function without any arguments.

    DosCommand "path >env.txt"

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.

Executing External Commands

    path_array = #()

We will need an array to store the single path definitions. We will initialize the array to the empty array in the beginning.

    in_text = openfile "env.txt"

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 ";".

FileStream Values

    SkipToString in_text "="

So we skip to the position of the "=" sign to start reading the first path.

FileStream Values

    while not eof in_text do (

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.

While and Do Loops

    str = readDelimitedString in_text ";"

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.

FileStream Values

    if str != "\n" then append path_array 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.

    ) close in_text

Now we are ready with the reading and can close the file.

FileStream Values

    DeleteFile "env.txt"

It was just a temporary file so we can delete it from the disk.

External File Methods

    path_array

Lastly, we can return the array with the collected paths as the result of the function.

    )

## Using the Script

As this is a function, we have to call it. It has no parameters, so we just call it with ()

    env_paths = GetEnvironmentPaths()

The variable env_paths will contain an array of the environment paths set under Windows.

## Where to go from here

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.

Back to

"How To" Tutorials Index Page