Loop through similar files and execute commands for that

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
countryqt30
Posts: 2
Joined: 21 Jan 2012 01:47

Loop through similar files and execute commands for that

#1 Post by countryqt30 » 21 Jan 2012 01:53

Hey guys,

I'd like to achieve the following:

Find all files ending with ".hlsl", "fxc" them with an input of ".psh" and ".vsh". Okay, seems a bit confusing, so here is an example:

Code: Select all

fxc /T vs_5_0 /E VxShader /Fo Terrain.vsh Terrain.hlsl
fxc /T ps_5_0 /E PxShader /Fo Terrain.psh Terrain.hlsl


In this case, the target name is "Terrain.hlsl". Unfortunately, this is doing it's job very well - but only for one file at once. Doing this for many files can become very very confusing and is unclear style i think.

So i need to do these 2 steps for EVERY *.hlsl file in the current directory.
How would i do this? Can this be done with batch or should I use something else?

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Loop through similar files and execute commands for that

#2 Post by aGerman » 21 Jan 2012 13:13

You could use a simple FOR loop.
Untested:

Code: Select all

for %%i in (*.hlsl) do (
  fxc /T vs_5_0 /E VxShader /Fo "%%~ni.vsh" "%%i"
  fxc /T ps_5_0 /E PxShader /Fo "%%~ni.psh" "%%i"
)

Regards
aGerman

countryqt30
Posts: 2
Joined: 21 Jan 2012 01:47

Re: Loop through similar files and execute commands for that

#3 Post by countryqt30 » 21 Jan 2012 13:27

@aGerman
Thank you, it works like a charm!

Can you explain what "%%~ni.vsh" really stands for? I know it means the <old name without extension> + ".vsh" but this CMD syntax is more than strange..
It's like hell for every scripter i think. :D

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Loop through similar files and execute commands for that

#4 Post by aGerman » 21 Jan 2012 13:53

It's one of the well documented options of FOR variables. Execute FOR /? to get the help message.

Code: Select all

[...]
    %~nI        - expands %I to a file name only
[...]

Regards
aGerman

Post Reply