External File Methods

The following methods provide access to external files and directories.

getFiles <wild_card_filename_string> recurse:<boolean> 

Returns an array of file names that match the given wild-card path name. If recurse is specified as true, recursively searches directories starting at the specified directory. The default is false.

FOR EXAMPLE,

The following code gets an array of all the .max scene files in c:\foo and then loops over the array, opening each file and printing the objects in each:

files = getFiles "c:\\foo\\*.max"
for f in files do (loadMAXFile f; print objects)

getFiles() can also be used to determine if a file or file pattern exists.

FOR EXAMPLE

the following function will return true if the specified file name or pattern exists:

fn existFile fname = (getfiles fname).count != 0

See also doesFileExist() which checks for a single file only and does not support wildcard patterns.

getDirectories <wild_card_directory_name_string> recurse:<boolean>  

Returns an array of directory paths that match the given wild-card directory path name. If recurse is specified as true, recursively searches directories starting at the specified directory. The default is false.

SCRIPT

fn getFilesRecursive root pattern =
(
dir_array = GetDirectories (root+"/*")
for d in dir_array do
  join dir_array (GetDirectories (d+"/*"))
my_files = #()
for f in dir_array do
  join my_files (getFiles (f + pattern))
my_files
)
--get all .ms files from the folder c:/temp
--and all its subfolders:
getFilesRecursive "c:/temp" "*.ms"
makeDir <directory_path_string> all:<boolean> 

Creates a new directory with the given name.

Returns true if the path was created successfully or if it existed already.

Returns false if the path could not be created either because the argument was not a valid path or due to insufficient writing rights.

If the optional all: keyword argument is set to true (default since 3ds Max 9, in 3ds Max 8 the default was false ), all sub-directory levels specified in the path will be created if they do not exist.

MAXScript does not provide a method for deleting directories. See the last example for HiddenDOSCommand() for a possible workaround.

EXAMPLES

makeDir @"c:\temp\test\deleteme\if\you\can" all:false
false --unless the path already exists up to the \you folder
makeDir @"c:\temp\test\deleteme\if\you\can" all:true
true --creates all the folders listed in the path
makeDir @"c:\temp\test\deleteme\if\you" all:true
true --even if the previous call had created the path, still ok
makeDir @"a:\temp"
false --unless you still have a Floppy drive and a disk for it!
<boolean>removeDir <directory_path_string> 

Removes the specified directory. Returns true if the directory was removed.

deleteFile <filename_string> 

Deletes the named file.

Fails if the file is open in MAXScript.An open file can be closed using the respective close() method for ASCII or Binary streams, or if the handle is not accessible due to local scope, by calling manual Garbage Collection using gc().

Does not work on directories created using makeDir() , only on actual files. For a way to delete a directory, see the last example for

HiddenDOSCommand()

.

Returns true on success, false on failure.

renameFile <old_filename_string> <new_filename_string> 

Renames the old file to the new file.

This can also be used to move a file between directories.

Fails if the new file already exists or if the old file is open in MAXScript.

Returns true on success, false on failure.

copyFile <existing_filename_string> <target_filename_string> 

Copies the existing file to the new file.

Fails if the existing file is open in MAXScript, the target file cannot be created, or the target file already exists. In the latter case, you can use deleteFile() to remove the existing target file first.

Returns true on success, false on failure.

getFileSize <filename_string> 

Returns the size of the specified file in bytes.

Returns 0 if the file could not be found.

EXAMPLE

GetFileSize (getDir #maxRoot + "\\3dsmax.exe")
--> 9881600
getFileAttribute <filename_string> <attribute> 
setFileAttribute <filename_string> <attribute> <boolean> 

Get and set the attributes associated with a file.

The get function returns true or false depending on the state of the specified attribute.

The set function sets the state of the individual attribute specified (leaving other attributes as they were).

The valid <attribute> values are:

#readOnly #hidden #system #directory #archive #temporary #normal 
getFileModDate <filename_string> 

Returns a String value containing the modification date for the specified file, for example "1/29/99 1:52:05 PM" .

getFileCreateDate <filename_string> 

Returns a String value containing the creation date for the specified file.

getFileVersion <filename_string> 

Returns the file and product version for the specified file, or unknown if this data is not specified in the file. This data is typically specified only for executable and application extension (i.e., .dll) files.

EXAMPLE

GetFileVersion (getDir #maxRoot + "\\3dsmax.exe")
--> "12,0,0,91 12,0,0,91"
getFileSecurityInfo <file_name> <attribute> testFileAttribute: <boolean> 

Returns true if the specified file exists and the specified security attribute is set, where <attribute> is one of #read , #write , #execute , #all .

If <file_name> is a directory and <attribute> is #write or #all and the directory's security attributes show the directory is writable, the method will also attempt to create a temporary file in the directory to confirm it is writable.

if testFileAttribute: is false or not specified, or <file_name> specifies a directory, the file attributes on the file are not included in the test.

If testFileAttribute: is true and <file_name> specifies a file, the file attributes (read, write, execute) are AND-ed with the security attributes.

Available in 3ds Max 2010 and higher.

isDirectoryWriteable <dir_name> 

Returns true if the specified directory exists and the directory can be written into.

Available in 3ds Max 2010 and higher.

getMAXIniFile() 

In 3ds Max 6 and higher, returns the current 3dsmax.ini file as a string. For some products that store the ini settings in the registry, this method returns undefined. See also Accessing INI File Keys.

MORE EXAMPLES:

for f in getFiles "3dsmax\\maps\\*.jpg" do deleteFile f
for d in getDirectories "D:\\foo\\*" do
for f in getFiles (d + "*.*") do
copyFile f ("C:\\temp\\" + getFilenameFile f + getFilenameType f)
if (getFiles "foo*.max").count == 0 do
print "No Files Match Pattern 'foo*.max'!"
getEncoding <filename_string>

NEW in 3ds Max 2022.2 Update:: Returns the character encoding of the specified text file. Returns #Unknown if the specified text file cannot be found or read.

Possible return values are:

#ANSI
#ASCII
#UTF7_With_BOM
#UTF7
#UTF8_With_BOM
#UTF8
#UTF16_LE_With_BOM
#UTF16_BE_With_BOM
#UTF16_LE
#UTF16_BE
#UCS4_LE_With_BOM
#UCS4_BE_With_BOM
#UCS4_LE
#UCS4_BE
#Unknown

EXAMPLE

filename = @"C:\Program Files\Autodesk\3ds Max 2022\en-US\3dsMax.ini"
getEncoding filename
--> #UTF8_With_BOM