ScriptSpot

Forum topic · General Scripting

detect unicode file names and rename them!

By artrender.info · 2013-04-02

Description

How to DETECT non-english characters inside file names and rename them???

Please help!

Comments (12)

jazznazz · 2021-05-25

Fixed Function that is essentially a whitelist for a string - it srtips all characters that are not present in the latinChars string:

	latinChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

	function OnlyLatinChars str =
	(
		local strCnt = str.count
		if strCnt != 0 do
		(
			for i = strCnt to 1 by -1 where (findString latinChars str[i] == undefined) do
			(
				str = replace str i 1 ""
			)
		)
		str
	)

	newStr = OnlyLatinChars "ヒグループホールデФЦЦШЩЧЧ.Qwerty32___"

the output is "Qwerty32"

barigazy · 2021-06-11

Two more versions ;)


-- VER#1
	fn OnlyLatinChars str = if isKindOf str string and str.count != 0 do
	(
		string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ; value = ""
		for i = 1 to str.count where (findString string str[i] != undefined) do append value str[i]
		value
	)
 
	newStr = OnlyLatinChars "ヒグループホールデФЦЦШЩЧЧ.Qwerty32___"

-- VER#2	
	fn OnlyLatinChars str letters:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" = 
	(
		if isKindOf str string and str.count != 0 do
		(
			value = ""
			for i = 1 to str.count where (findString letters str[i] != undefined) do append value str[i]
			value
		)
	)
 
	newStr = OnlyLatinChars "ヒグループホールデФЦЦШЩЧЧ.Qwerty32___"

dajono · 2020-02-21

Unicodes file creates a complex error during the running program and stocking everything and also creating a really big bug. So that many helpful medium https://www.topdissertations.org/edubirdie-review/ help the developer for this purpose.

amir3dsmax · 2014-03-19

can any one give me the code . i need this

miauu · 2013-04-03

Glad you found the solution.

THX! SOLVED

artrender.info · 2013-04-03

Thank you so much

amir3dsmax · 2014-03-19

can you give code please . i need to rename nonlatine

this idea is great!

artrender.info · 2013-04-03

what is theChars?

Except this I want first to detect if a filename contains at least a non-latin character and to display it

if m.filename conatins non-latin
then print m.filename

It is because: If I have a texture with nonlatin name (nonlatin.jpg) then after deleting nonlatin characters you could achive a file .jpg without name

or after renaming 2 textures:

"nonlatin1"001.jpg and "nonlatin2"001.jpg you could get 2 textures with the same name!

In order to detect first if the file contains nonlatin characters there is no need to control the name till the end and
it means inside "for" I should break the cicle:


	local latinChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
	function foreignfn fname =
	(
		
		local counter=0
		for i = fname.count to 1 by -1 where (findString latinChars fname[i] == undefined) do
			if (counter=counter+1)==1 then
				False
			else
				True
	)


	for m in getClassInstances BitmapTexture where (foreignfn m.filename == true) do
 		print m.filename

Could YOU help me to make corrections in this script PLEASE?!!!

Try this

miauu · 2013-04-03


(
	local latinChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	
	function OnlyLatinChars str =
	(
		local strCnt = str.count
		if strCnt != 0 do
		(
			for i = strCnt to 1 by -1 where (findString theChars str[i] == undefined) do
			(
				str = replace str i 1 ""
			)
		)
		str
	)
	
	newStr = OnlyLatinChars latinChars
)

You can add it in the function above.

thx

artrender.info · 2013-04-03

but what if a filename has russian characters or chinese, or greek or any other? They can contain not exactly characters from this list! They could contain others as well!

Should I give allowed latin alphabet with numbers or there is other way as well?

Try this function:

miauu · 2013-04-03


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

I've found smth like this for php and javascript

artrender.info · 2013-04-03

php


if (preg_match('/[^\x30-\x7F]/', $_POST['full_name']) > 0)
{
    $error = true;
}

x30 will exclude: !"#$%&'()*+,-./.

java


var foreignCharacters = $("#foreign_characters").val();
var rforeign = /[^\u0000-\u007f]/;

if (rforeign.test(foreignCharacters)) {
  alert("This is non-Latin Characters");
} else {
  alert("This is Latin Characters");    
}

how to implement these things in maxscript:



for m in getClassInstances BitmapTexture where (foreignfn m.filename == true) do

rename m.filename

For the other hand we have:

character.set parameter for maxscript! I'm still confused!!!