'Loop thru subdirectories and execute

I run the following in a cmd prompt in order to convert .webp files to .jpg, but have to go into each subfolder, one at a time, and open a new command prompt window, ad infinitum...

for %f in (*.webp) do "X:\12X\11Web\Games\webp (WEBPs)\libwebp-1.0.2-windows-x86\bin\dwebp.exe" -o "%~nf.jpg" "%f"
DEL "*.webp"
EXIT

I want to create a batch file that will cycle thru main folder as well as each subfolder tree, executing the above. I've gleaned from some other stackoverflow posts the following, but it's erroring out.

@echo off
FOR /d /r %%i IN (*) DO (
   pushd "%%i"
COMMAND HERE????????
   popd
)

Any help is appreciated.



Solution 1:[1]

Here you have two ways to do this. Either pushd to the directory where the file is found, run the command, test if the jpg file exists then delete the webp file:

Note, this is meant for a batch-file and not cmd.

@echo off
for /R %%f in (*.webp) do (
    pushd "%%~dpf"    
    "X:\12X\11Web\Games\webp (WEBPs)\libwebp-1.0.2-windows-x86\bin\dwebp.exe" -o "%%~nf.jpg" "%%~nxf"
    if exist "%%~nf.jpg" del /Q "%%~nxf"
    popd
)

Or you can simply run it by converting without pushd by simply telling it the full path to file and output file:

@echo off
for /R %%f in (*.webp) do (
    "X:\12X\11Web\Games\webp (WEBPs)\libwebp-1.0.2-windows-x86\bin\dwebp.exe" -o "%%~dpnf.jpg" "%%~f"
    if exist "%%~dpnf.jpg" del /Q "%%~f"
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gerhard