Notifications
Clear all

[Closed] How to fix invalid symbol of a filename?

Hi,
I have a problem of handle filename,the window file does not allow filename include these symbols:
/:*?”<>|

For example,I input a filename in edittext:
“C: estpath\err?:””“ortest.jpg”
So how to make the edittext of maxscript to create a valid name to:
“C: estpath\errortest.jpg”


filename=""
rollout test_filename "test" 
( 
	editText path_test pos:[5,30]
	on path_test changed txt do 
	(
		if txt!= undefined then 
		(
			if txt!="/" then filename=txt
		)
		print filename
	)
)
CreateDialog test_filename 300 100

4 Replies

a quick fix could be


rollout test_filename "test" 
( 
 fn fixName txt = (
  invalidCharsAr = #("/", ":", "*", "?", "\"", "<", ">", "|" )  
  for c in invalidCharsAr do ( txt = substituteString txt c "" )
 
  txt
 )
 
 editText path_test pos:[5,30]
 on path_test changed txt do 
 (
  path_test.text = txt = fixName txt
  print txt
 )
)
CreateDialog test_filename 300 100

1 Reply
(@momo2012)
Joined: 11 months ago

Posts: 0

works !perfect,too much thanks!

Ripped from Render To Texture script that is shipped with 3ds max.

-- function to make a filename valid by replacing illegal characters with a '_'
	function MakeFileNameValid fileName = -- fname must be just filename, no path....
	(
		local illegal_characters =":/\\~!@#$%^&*()+=|'?><;[]{}\"" -- illegal characters in file name
		local res = copy fileName
		
		-- make sure the object name is legal for a filename
		local count = res.count
		for i = 1 to count do
			if (findString illegal_characters res[i]) != undefined do -- replace illegal characters with _
				res[i] = "_"
		
		res
	)
1 Reply
(@momo2012)
Joined: 11 months ago

Posts: 0

I think this is a better way to fix,thanks.