Home Coding, Scripting, Shaders

(MXS) use pattern with specific string

polycounter lvl 12
Offline / Send Message
Tosyk polycounter lvl 12
I'm trying to search through files but I can't manage to put file extension from pattern. here's my code:
fn FindAndReplace str fnd rpl = (
   addpos = findString str fnd
   if (addpos != undefined) do (replace str addpos fnd.count rpl)
)

fm = openFile "D:\\path\\00BCB111D67783\\1FA83D69D89B24_materials.txt"  mode:"rt"
line_cnt = 0
while not eof fm do (
   readLine fm
   line_cnt += 1
)
seek fm 0

for i=1 to line_cnt do (
   eachLine = readline fm
   texNameTemp = (FindAndReplace eachLine "TEXT " "")
   if texNameTemp != undefined then (
      tokens = filterString texNameTemp " "
      texName = substituteString tokens[1] "-" ""

      fileEnd = #(".dds", "_dx10.dds", ".tga")

      print texName
      searchSubDirs = (dotnetClass "System.IO.SearchOption")
      sFiles = dotnetClass "System.IO.Directory"
      theFile = (sFiles.GetFiles @"D:\path\_ex\Materials" (texName + fileEnd) searchSubDirs.AllDirectories)[1]
      if theFile == undefined then (print "file not found")
      else print (theFile + " found.")
   )
   else ()
)
so the file might be <texName>.dds, <texName>_dx10.dds or <texName>.tga. and I want to:
  1. search for <texName>.dds first
  2. if first is fail then search <texName>_dx10.dds
  3. if second is fail then search for <texName>.tga
the 'Materials' folder is really huge and don't want to terrorize my hdd by triple search one by one for each extension. so how can I achieve this?

Replies

  • PolyHertz
    Options
    Offline / Send Message
    PolyHertz polycount lvl 666
    Could you provide an example txt file that you're trying to parse? It's important to know how the files lines are structured, else there's no way to be sure we're giving you sound advise.
    Also, you should mention that this is maxscript (since not everyone may automatically recognize it).
  • Tosyk
    Options
    Offline / Send Message
    Tosyk polycounter lvl 12
    PolyHertz said:
    Could you provide an example txt file that you're trying to parse? It's important to know how the files lines are structured, else there's no way to be sure we're giving you sound advise.
    Also, you should mention that this is maxscript (since not everyone may automatically recognize it).
    sure. it's like:
    Material <matName>
    ==========================
    TEXT <texName> 1
    TEXT <texName> 1
    TEXT <texName> 0

    Material <matName>
    ==========================
    TEXT <texName> 1
    TEXT <texName> 0
    TEXT <texName> 1
    TEXT <texName> 1
    <matName> - it's a name of the folder (M_<matName>) containing all the listed textures. I've tried to search for that folders first and copy them into specific folder but I don't know how to search for folders. so I came with this bright idea.
    also the structure of the tree is supposed to be any. Right now I’m trying to understand smaller problem. Next I might list all the founds into the list view. Also thanks for the response. 
  • Tekoppar
    Options
    Offline / Send Message
    Tekoppar polycounter lvl 10
    I'm not used to Maxscript and I'm not entirely sure this is exactly what you wanted, but.

    old code
    </code><pre class="CodeBlock"><code>/*structs were created to make it easier to handle and move the data<br>the first struct contains the filepath along with all the texture names<br>while the second struct contains the filepath and the texture name*/<br>struct textures (filePath, fileStrings = #())<br>struct foundFiles (filePath, fileName, fileExtension)<br><br>file = dotnetClass "System.IO.File"<br> <br>/*path to the file that contains the infromation*/<br>textPath = "C:\\Users\\Tekoppar\\Pictures\\test.txt"<br> <br>/*path to the base folder where everything is kept*/<br>globalFolderPath = "C:\\Users\\Tekoppar\\Pictures\\"<br>textValues = openFile textPath mode:"r"<br> <br>/*array for the folder path and all the texture names*/<br>filePaths = #()<br> <br>/*used to avoid having the first loop add an empty array*/<br>filePathIndex = 0<br> <br>/*temp variable to store the looped information*/<br>values = textures()<br> <br>while not (eof textValues) do (<br>stringValue = readLine textValues<br> <br>/*change the pattern to whatever it is you're using, you can also add an if instead of an else if you only want to get<br>certain information from the file and there might be other information that you don't care for*/<br>if (matchPattern stringValue pattern:"path:*" ignorecase:false) then (<br> if (filePathIndex > 0) do (<br> append filePaths values<br> values = textures()<br> )<br> filePathIndex = filePathIndex + 1<br> /*replace stringValue 1 5, 1 refers to the start of the string while 5 is the length that we want to remove*/<br> newString = replace stringValue 1 5 ""<br> values.filePath = newString<br> ) <br>else (<br> /*replace stringValue 1 5, 1 refers to the start of the string while 5 is the length that we want to remove*/<br> newString = replace stringValue 1 5 ""<br> append values.fileStrings newString<br> )<br>)<br><br>/*appends the last values stored*/<br>append filePaths values<br> <br>/*file endings, add and remove as needed*/<br>fileEnd = #(".png", ".jpg")<br><br>/*array to store all the filepaths and texture names*/<br>foundTextures = #()<br><br>/*temp variable to store the filepath and the texturenames*/<br>foundTexture = foundFiles()<br><br>for values in filePaths do (<br> for textureName in values.fileStrings do (<br> for end in fileEnd do (<br> /*we check if the file exists, if it does we store the information*/<br> if file.Exists(globalFolderPath + values.filePath + "\\" + textureName + end) do (<br> foundTexture.filePath = (globalFolderPath + values.filePath)<br> foundTexture.fileName = textureName<br> foundTexture.fileExtension = end<br> append foundTextures foundTexture<br> )<br> )<br> )<br>)<br><br>for texture in foundTextures do (<br> print texture.filePath<br> print (texture.fileName + texture.fileExtension)<br>)<br><br>/*should close the file stream, but doesn't. Maybe someone else know's why*/<br>flush textValues<br>close textValues
    I realized last night that the middle loop isn't needed and it's code could be moved to the first one so.

    new code

    /*structs were created to make it easier to handle and move the data<br>the first struct contains the filepath along with all the texture names<br>while the second struct contains the filepath and the texture name*/<br>struct textures (filePath, fileStrings = #())<br>struct foundFiles (filePath, fileName, fileExtension)<br><br>file = dotnetClass "System.IO.File"<br>		<br>/*path to the file that contains the infromation*/<br>textPath = "C:\\Users\\Tekoppar\\Pictures\\test.txt"<br>		<br>/*path to the base folder where everything is kept*/<br>globalFolderPath = "C:\\Users\\Tekoppar\\Pictures\\Bilder\\"<br>	<br>/*used in the loop to set the temp folder path*/<br>currentFolderPath = ""<br>textValues  = openFile textPath mode:"r"<br>	<br>/*file endings, add and remove as needed*/<br>fileEnd = #(".png", ".jpg")<br><br>/*array  to store all the filepaths and texture names*/<br>foundTextures = #()<br><br>/*temp variable to store the filepath and the texturenames*/<br>foundTexture = foundFiles()<br><br>while not (eof textValues) do (<br>stringValue = readLine textValues<br>	<br>/*change the pattern to whatever it is you're using, you can also add an if instead of an else if you only want to get<br>certain information from the file and there might be other information that you don't care for*/<br>if (matchPattern stringValue pattern:"path:*" ignorecase:false) then (<br>	/*replace stringValue 1 5, 1 refers to the start of the string while 5 is the length that we want to remove*/<br>	currentFolderPath = replace stringValue 1 5 ""<br>	) <br>	else (<br>		/*replace stringValue 1 5, 1 refers to the start of the string while 5 is the length that we want to remove*/<br>		textureName = replace stringValue 1 5 ""<br>		for end in fileEnd do (<br>			if file.Exists(globalFolderPath + currentFolderPath + "\\" + textureName + end) do (<br>				foundTexture.filePath = (globalFolderPath + currentFolderPath)<br>				foundTexture.fileName = textureName<br>				foundTexture.fileExtension = end<br>				append foundTextures foundTexture<br>				)<br>			)<br>	)<br>)<br><br>for texture in foundTextures do (<br>	print texture.filePath<br>	print (texture.fileName + texture.fileExtension)<br>)<br><br>/*should close the file stream, but doesn't. Maybe someone else know's why*/<br>flush textValues<br>close textValues





Sign In or Register to comment.