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:
- search for <texName>.dds first
- if first is fail then search <texName>_dx10.dds
- 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
Also, you should mention that this is maxscript (since not everyone may automatically recognize it).
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.
</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