Notifications
Clear all

[Closed] edit or rewrite external txt file with maxscript

Hi everybody can anyone help me with this please
in drive C we have a 001.txt file .in a scene i have 3 teapots with radius 10 and 3 sphere radius 20
select all sphere and run this


  file_001txt=createfile "C:\\001.txt"	--create 001.txt file in C drive
  
  --select all sphere as array
  Ar= selection as array
  for o in ar do
  (
  Filename= "C:\\001.txt"
  Sradius= o.radius as string
  setINISetting Filename (o.name+"_Rad") "Lradius" Sradius
  )
  
  /*
  --the result is 
  [Sphere002_Rad]
  Lradius=17.0604
  [Sphere003_Rad]
  Lradius=17.0604
  [Sphere001_Rad]
  Lradius=17.0604
  

Now my question is how to rewrite the 001.txt file and keep all radius value in the 001.text file but just replace the [Sphere002_Rad] with [Teapot002_Rad]
I mean when we select all sphere and run the edited script it just replace the name of teapot Ar not radius of teapot

Thanks in advance

7 Replies

I found how to replace the text but don’t know how to do it in array part


 
  fn replaceTxtInFile fileName oldTxt newTxt =
  (
 	 local s = dotnetClass "System.IO.File"
 	 s.WriteAllText fileName ((s.ReadAllText fileName asdotnetobject:on).Replace oldTxt newTxt)
  ) 
  replaceTxtInFile "C:\\001.txt"  "Sphere"  "teapot"
 
 

i do my side hope sb save my time

This time thanks myself

Since you are using the ini format, getINISetting to a variable, delIniSetting the section and setINISetting the renamed section.

Hey man
I’m really exhausted with this part i saw what you’ve said in maxscript help, honestly i couldn’t understand would you give me an example or any page

Oh, so you want to rebuild everything? Well, I thought you want to replace just a specific section heading, it is doable this way, too, though it’s a bit convoluted. Who cares, let’s do it just for the fun of it So, if we create the inifile:

(
	local filename = @"C:\001.ini"
	local file = if not doesFileExist filename do createFile filename -- check before overwriting the file

	local spheres = $selection/sphere* as array

	for obj in spheres where isProperty obj #radius do -- in case some of the spheres are collapsed
		setINISetting filename (obj.name + "_Rad") "Lradius" (obj.radius as string)

	close file -- close the file, otherwise we won't be able to edit it later
)

we can collect all the values and create the file anew, with some values replaced:

(
	struct iniNode (section, key, value)

	local filename = @"C:\001.ini"

	local sections = getINISetting filename
	local iniNodes = #()

	for section in sections do
	(
		local keys = getINISetting filename section
		for key in keys do
			append iniNodes (iniNode section:section key:key value:(getINISetting filename section key))
	)

	local file = createFile filename

	for i in iniNodes do
		setINISetting filename (substituteString i.section "Sphere" "Teapot") i.key i.value

	close file
)

I’m terribly sorry for my bad example and really appreciated for the detailed example and reply.

The problem comes from my bad example,i didn't know there will be such an answer, by the array part i meant  Anything like

Ar1= #( “A”,“sphere”,“R_cnt_001”) and Ar2=#(“test”,“teapot”,“L_cnt001”)

diffrent name in array but all have a Common value (here radius) and i need first element in Ar1 replaced by first Ar2 element and second and....
but i write an similar example which are just different in number,

Don't know it is possible or not

Best regard

It’s not all that different, for example:

(
	local filename = @"C:\001.ini"
	local Ar1 = #("A", "Sphere", "R_cnt_001")
	local Ar2 = #("test", "Teapot", "L_cnt001")

	local sections = getINISetting filename

	for i = 1 to Ar1.count do
	(
		local prefix = Ar1[i]

		for section in sections
			where matchPattern section pattern:(prefix + "*") ignoreCase:false do
			(
				local newSection = substituteString section prefix Ar2[i]
				local keys = getINISetting filename section
				local values = for key in keys collect
					dataPair key (getINISetting filename section key)

				delIniSetting filename section

				for v in values do
					setINISetting filename newSection v.v1 v.v2
			)
	)
)

Thanks so much Swordslayer

Your answer put a happy End to the script which i’ve begun ,
I’ve really needed those lines

Merry Christmas and Thanks again