🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Batch file magic, possible?

Started by
4 comments, last by cozzie 3 years, 1 month ago

Hi all,

I'm working on automating more steps in my asset pipeline, and I'm now struggling with something.

  • I have mesh assets to convert which is sometimes in batch and sometimes per single file
  • there is ALWAYS a .ini file going along with the object (same filename, except extension)

Would it possible to do something like this in a normal batch file (no shell extensions stuff):

  • in current dir
  • find *.ini file → guaranteed to be 1 always (per subdirectory/ in the current dir)
  • execute my converter with the found .ini file above from step 2, but with different extension
  • example: convert.exe myfoundfile.obj (based on myfoundfile.ini)

Btw, I could write a simple c++ (console) application that does the above per folder, but I'm curious if it's possible with just using a batch file.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

What you describe is possible with a few simple commands. As you wrote batch file, you don't mean ShellScript so Windows CMD only right?

You need to store the filename found in an environment variable and then can do string manipulation on it

%myVar:~-N%

Returns the contents of myVar up to Length - N characters so you could replace N with 4 and then get the filename without “.ini”. Concatenation is also supported by just using set again

set "myVar=%myVar%.obj"

Doing the above in a for loop for each file found and if you want to make sure an corresponding obj file exists, you can also perform an

if exist "%myVar%" (do action here)

cozzie said:
I could write a simple c++ (console) application that does the above per folder

There are a ton of different solutions to perform that, starting with a Powershell script, C# script (available with .NET 5 for example), a make-ish tool up to complex build tools. Take whatever makes your liefe easy and if you are looking for a solution, you might also have a look at what we use in our game engine project on GitHub

This is the kind of task at which scripting languages like Python excel. You might be able to solve this particular problem in a batch file, but the more complex a batch file is, the more likely it is that you'll eventually want to add some functionality that can't be done in a batch file, at which point you'll have to rewrite it in a real language anyway. So why not skip the rewriting by using a real language from the beginning?

The FOR command can iterate over files or directories and run a command for each item it finds.

But shouldn't you be using a proper build system instead? Even Make would be more robust and efficient than a batch file.

Omae Wa Mou Shindeiru

Thanks, and that's probably good advice indeed.

In the meantime I did some improvements, all I need now is a way to execute my converter per subfolder, without any params, which should be very straight forward.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement