Running a command-line tool on all the files of a directory and sub-folders (batch)

Videos, guides, manuals, documents and tutorials about using tools and performing tasks
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by aluigi »

Just a quick tutorial for who wants to run any command-line tool in batch mode on all the files of a folder instead of running it manually for every single file.
So, for example, if you have 1000 fsb or xwb archives and you want to run fsbext and unxwb on them with a single command, that's the trick.

Create a file called file.bat in the target folder containing the following syntax:

Code: Select all

for /r %%G in ("*") do COMMAND %%G

Where COMMAND must be replaced with the full command-line tool and argument you want to run, for example

Code: Select all

for /r %%G in ("*.xwb") do unxwb -l %%G
As you can see I replaced also the * (for ANY file) with *.xwb for running the tool only on the files with XWB extension.
It's even possible to run multiple commands in sequence by appending &&:

Code: Select all

for /r %%G in ("*.xwb") do md %%G_output && unxwb -d %%G_output %%G


You can also use this method from the cmd.exe console, you have only to replace %%G with %G.

Please note that the G in %%G can be anything, so if you prefer %%a you can use it too.

This topic is for anyone who wants to add other methods, tips, suggestions, examples and questions.
It's something that is often asked by users so it's very important to have a topic about it.

P.S.: a good link about this topic http://ss64.com/nt/for_r.html
AceK
Posts: 5
Joined: Thu Nov 24, 2016 9:28 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by AceK »

Thanks for the insight.

When I tried the last script, I get unsatisfactory results sadly.

Image

Each .xwb file that gets applied with unxwb.exe results in a 00000000.wav file, so it would lead to override requests. I tried to apply this batch to see how it goes, and it results in a request for an override to the new "00000000.wav" files, without change in file name (or replicate using the original file name with the new .wav extension).

Code: Select all

for %%G in (*.xwb) do unxwb.exe "%%G"
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by aluigi »

You forgot the && between the md command and unxwb.
AceK
Posts: 5
Joined: Thu Nov 24, 2016 9:28 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by AceK »

Ah okay, tried it now and it gave 00000000.wav files in their respective generated folders as needed:

Code: Select all

for %%G in ("*.xwb") do md %%G_output && unxwb.exe -d %%G_output %%G


Now to try and figure how to rename the files inside the folder to match with the folder name...

Thanks aluigi <3!!!

Edit: Got everything done for this rip, working on .pos modifications. This may seem off-topic, but do you know if unxwb will support loop pointer extraction in the future? The ADPCM format seems to play fine (in P4 Ultimax's case, offset 0xA4 ~ 0xA7 = Loop Start... offset 0xA8 ~ 0xAB = Loop Length starting from string "WBND"). I haven't looked at the other format types of .xwb files out there, but I'd guess that it would be different.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by aluigi »

No, there will be no support for these loops.
It extracts files from the XWB archives, that's it.
alwayslookin2
Posts: 11
Joined: Sat May 04, 2019 4:25 am

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by alwayslookin2 »

aluigi wrote:No, there will be no support for these loops.
It extracts files from the XWB archives, that's it.


I found some batch code on stackoverflow. Is there a way to run this code, and have it extract to the folder the .tex file is in (even if it ends up overwriting the file).

Currently this script asks if I want to create a new folder for every file with a Y/N question which is difficult when there are thousands of files to process, and it would create too many folders rather than leaving the folder/file structure as it is (I found out about the -Y option but it is still preferable to just overwrite the files)

Code: Select all

for /R %%i in (*.tex) DO quickbms.exe -o script.bms "%%~i" "%%~dpni"
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by aluigi »

I just reply on the fly so I may be wrong.
quickbms has 2 options, -d and -D that automatically create folders for the destination files.
Not sure if that's what you mean but you can try.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by aluigi »

Two examples about how to run offzip on multiple files in the current folder:

Code: Select all

for %%G in ("*.ext") do md %%G_output && offzip -a %%G %%G_output

Code: Select all

for %%G in ("*.zip") do md %%G_output && offzip -a -z -15 -Q %%G %%G_output

I didn't use /r because it forces the command to run on all the extracted files too... yeah very useless and annoying in most cases, except if you specify a different output folder:

Code: Select all

for /r %%G in ("*") do md c:\output%%~pG%%~nG%%~xG && offzip -z -15 -Q -a %%G c:\output%%~pG%%~nG%%~xG


If you are interested in the additional syntax for %%G:

Code: Select all

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file


P.S.: my original example in the first post missed the "&&" for some unknown reasons, now fixed.
Nameless32
Posts: 35
Joined: Sun Dec 21, 2014 7:43 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by Nameless32 »

aluigi wrote:Two examples about how to run offzip on multiple files in the current folder:

Code: Select all

for %%G in ("*.ext") do md %%G_output && offzip -a %%G %%G_output

Code: Select all

for %%G in ("*.zip") do md %%G_output && offzip -a -z -15 -Q %%G %%G_output

I didn't use /r because it forces the command to run on all the extracted files too... yeah very useless and annoying in most cases, except if you specify a different output folder:

Code: Select all

for /r %%G in ("*") do md c:\output%%~pG%%~nG%%~xG && offzip -z -15 -Q -a %%G c:\output%%~pG%%~nG%%~xG


If you are interested in the additional syntax for %%G:

Code: Select all

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file


P.S.: my original example in the first post missed the "&&" for some unknown reasons, now fixed.


you can use " /f " with some tricks...


for /f "delims=* tokens=*" %%a in ('dir /b /s *.zip *.rar') do ......

you can set a folder or concatenate another commands too

for /f "delims=* tokens=*" %%a in ('dir /b /s *.zip *.rar ^| findstr "english"') do echo %%~nxa
ExtractResponseUnit
Posts: 12
Joined: Tue Sep 08, 2020 3:31 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by ExtractResponseUnit »

As far as I understand is the method you are provide here exactly what I was looking for
a long time...

I can use this for every cmd-tool even the program don't support multiple file handling by itself right?

Edit:

Code: Select all

for /r %%G in ("*.radp") do radp2wav.exe -l %%G


Any idea why it didn't work?
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by aluigi »

@ExtractResponseUnit
If you use that command from console (cmd.exe) replace %% with %
ExtractResponseUnit
Posts: 12
Joined: Tue Sep 08, 2020 3:31 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by ExtractResponseUnit »

Thanks for your responce aluigi

Actually I tried the command in a batch file. I also tried your suggestion above.

Same error log:

Code: Select all

usage: radp2wav <inputfile> <outputfile>
input file does not exist
File "C:\radp2wav\test\-a" not found.
usage: radp2wav <inputfile> <outputfile>
input file does not exist
File "C:\radp2wav\test\-a" not found.


By the way: I'm a big fan of your QuickBMS tool, many thanks for inventing it.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by aluigi »

As far as I can see that tool requires different arguments.
Maybe try:

Code: Select all

for /r %%G in ("*.radp") do radp2wav.exe %%G %%G.wav


There is also a comment here regarding radp2wav on all the files from the command-line:
http://blog.gib.me/2009/06/21/prototype/comment-page-3/
ExtractResponseUnit
Posts: 12
Joined: Tue Sep 08, 2020 3:31 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by ExtractResponseUnit »

Code: Select all

usage: radp2wav <inputfile> <outputfile>


How to go round, I've tried it with different arguments it results always the same error...

I guess you got much more of experience than me so if it don't bother you I could upload the programm or
even the source code if you prefer
ExtractResponseUnit
Posts: 12
Joined: Tue Sep 08, 2020 3:31 pm

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by ExtractResponseUnit »

I hope at least one of the 12 guys who downloaded the source find a solution
nofakehappy
Posts: 1
Joined: Tue Feb 09, 2021 12:12 am

Re: Running a command-line tool on all the files of a directory and sub-folders (batch)

Post by nofakehappy »

I think adding quotes to the %%G and %%G.wav would help.

Example: for /r %%G in ("*.radp") do radp2wav.exe "%%G" "%%G.wav"