1

I am really new at Python. I've created a script with some help of various online resources and some talented acquaintances, but I've completely hit a brick wall. I'm hoping there's some talented people here that can help solve this problem.

I have a working script, but it needs to be run on every folder 1 level (only) ABOVE the root folder. It definitely can not go BELOW. There is in excess of 6000 subfolders from the root, all with their own file which the script needs to be run on. e.g.

ROOT
---------MODEL 1
                ------FILE.XMODEL_EXPORT
---------MODEL 2
                ------FILE.XMODEL_EXPORT
---------MODEL 3
                ------FILE.XMODEL_EXPORT

and so on.

Here's a very cut down version of the code that gets run on these files.

import glob
import os

command = "export2bin.exe /s *.xmodel_export"
os.system(command)

dirpath = os.path.split(os.getcwd())[1]

# Put all files with XMODEL_EXPORT extension into variable "myFiles"
myFiles = glob.glob('*.XMODEL_EXPORT')

for file in myFiles:
  filename = file.rstrip('_LOD0.XMODEL_EXPORT') # strip the text off the end of the variable. This becomes the model name in APE.

  # create the gdt filename with extension
  filename_with_extension = filename + '.gdt'
   # GDT CREATION STARTS HERE # ------------------------------------------------------------------------

  gdt = '{\r'
  gdt += '"'
  gdt += filename
  gdt += '" '
  gdt += '( "xmodel.gdf" \r'
  gdt += '  {\r'
  gdt += '      "arabicUnsafe" "0"\r'
  gdt += '      "autogenLod4" "0"\r'
  gdt += '      "autogenLod4Percent" "13"\r'
  gdt += '      "autogenLod5" "0"\r'
  gdt += '      "autogenLod5Percent" "13"\r'
  gdt += '      "filename" "'
  gdt += dirpath
  gdt += '\\\\'
  gdt += 'xmodels'
  gdt += '\\\\'
  gdt += filename
  gdt += '\\\\'
  gdt += filename
  gdt += '.XMODEL_BIN"\r'
  gdt += '  }\r'
  gdt += '}\r'

  # GDT CREATION ENDS HERE # ------------------------------------------------------------------------

  #save the GDT into the gdt file
  with open(filename_with_extension, 'w') as gdtfile:
    gdtfile.write(gdt)

What I need to achieve here is getting this on a rightclick Shell menu. (that's the easy part)

I should be able to rightclick the ROOT folder, and it will walk the subfolders, find the XMODEL_EXPORT file, convert it to an XMODEL_BIN file, and write a GDT file to the same directory as the file that has just been processed.

At the moment, I can enter any directory with the XMODEL_EXPORT file in it and the code works fine. As soon as I go back 1 or 2 levels, it doesn't work anymore.

I hope I've been descriptive enough.

Thanks in advance.

RaGe
  • 13
  • 4

1 Answers1

0

The reason the code doesn't work if you're not in the same folder is because by default, glob.glob only checks files in the folder you're in, not subfolders.

To get around that, change this line:

myFiles = glob.glob('*.XMODEL_EXPORT')

to this line:

myFiles = glob.glob('**/*.XMODEL_EXPORT', recursive=True)

The **/* means accept pathnames which include subfolders, and recursive=True means check subfolders as well.

Hope this helps.

EDIT:

I got the information from here

Ed Ward
  • 2,333
  • 2
  • 10
  • 16