Notifications
Clear all

[Closed] Testing directory paths from a file path

Is this the tidiest way to go about testing if a folder is writable when given a filepath? The main thing that I feel is a bit clunky is the way that I used replace to get rid of the backslash that’s left on the end after you use getFilenamePath. But if I don’t strip off the end backslash it always returns false and is waiting for more input cause it’s treating the last slash as an escape character. Is there a more elegant way to do this?


 theFile = @"C:	emp	heFile.txt"
 thePath = getFilenamePath theFile
 thePath = replace thePath thePath.count 1 ""
 isDirectoryWriteable thePath 
 

Cheers,
Cg.

8 Replies

that’s an interesting situation.
you want to save a file to some directory, and you want to know that the directory is writable.
but if it’s not… what is your next step?

These are not the answers you’re looking for.

But I’ll tell you anyway.

This is part of a function that will test whether the directory of a listed file is actually there. The filepath is from an edittext box that most often will get populated by browsing to the spot with getSaveFileName and assigning the file. BUT it is also able to be edited by the user, and this is why I want to add this check to make sure it’s all fine before continuing. If not, it will return false, not go any further in the script, report that to the user asking for a valid file. But really? Did you need to know that? You have too much time on your hands Dennis.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

there was a reason in my question… now i see that the better function to use in your case is doesfileexist
it works both for files and directories

Edit:

Note sure if it's helpful, but there's also another function for this.

getFileSecurityInfo <file_name> testFileAttribute:

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

Or this(from RenderToTexture script):

--	check if directory specified as outputpath exist
		function ValidateDirectory _dirNameIn  =
		(
			local _dirName = copy _dirNameIn
			-- remove terminating '\' or '/' character
			local c
			for i = _dirName.count to 1 by -1 while (c=_dirName[i]; c=="\\" or c== "/") do
				_dirName = subString _dirName 1 (i-1)
			
			if doesFileExist _dirName then
				dirExists = true
			else
				dirExists = false
			
			dirExists
		)

…and [color=Lime]isDirectoryWriteable is [/color]available only in 3ds Max 2010 and higher.

Well, this is answered well already, isDirectoryWriteable() is OK for Max 2010+ and doesFileExist() is fine for testing directories, but nothing bad to append some function though…

fn isDirectory s = doesFileExist(trimRight (getFilenamePath s) "[\\]( http://forums.cgsociety.org/ )")

Great!! It seems that doesFileExist also works without having to strip off the last backslash. TIDY!!

Also I’ve never seen/used trimright before. Very useful!!!

Thanks to all of you for your replies.

Cheers,
Cg.