0

I am trying to convert a set of PNG images to WEBP images using cwebp encoder. I managed to do it using a JAVA program but now I want to write a batch file to do this task. I already got metal03326's solution after some searching. But I want one modification in this that 9 patch images should be skipped.

Here is what I did :

pushd %1

set "DOTNINE=.9"

echo %DOTNINE%

for /f "delims=" %%n in ('dir /b /s /a-d-h-s') do (

set MY=%%~nn

set MY=%MY:~-2%

rem IF NOT ".webp" == "%%~xn" IF NOT "MY" == ".9" ("%~dp0cwebp.exe" -q 80 "%%n" -o "%%n.webp") ELSE (echo File already WEBP.)

)

popd

But I'm getting a syntax error. What is wrong in the above code?

2
  • No syntax error shown here on Win 7. Is it the entire batch script? Commented Jul 28, 2015 at 8:48
  • Removed the android tag, since this question has nothing to do with Android. Commented Jul 28, 2015 at 8:53

1 Answer 1

1

To re-assign a variable (namely MY) multiple times in a loop you have to use delayed expansion:

setlocal enableDelayedExpansion
pushd "%~1"
for /f "delims=" %%n in ('dir /b /a-d-h-s') do (
    set MY=%%~nn
    set MY=!MY:~-2!
    IF NOT ".webp" == "%%~xn" IF NOT "!MY!" == ".9" ("%~dp0cwebp.exe" -q 80 "%%n" -o "%%n.webp") ELSE (echo File already WEBP.)
)
popd
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It did the job :) One more thing - Is there a way to do this at one place i.e instead of creating MY variable do it in IF NOT condition ?
set "MY=%%~nn" & set "MY=!MY:~-2!" & IF ....., for example. Another method would be to use for /f.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.