2

So I am trying to a procedure of converting images in a directory, but it just doesn't run at all, and I fail to see what's incorrect in my batch code:

FOR /R %a in (*.png) DO (
    files\pngnq -s 1 -n 16 %~fa 
    ren *nq8.png "%~na.png"
    files\gimconv "%~na.png" --format_style psp --format_endian little --pixel_order faster --image_format index4
    del "%~na.png"
    )

the error it outputs is in french "in was not expected" (in était inattendu).

EDIT: this is the orginal (working code) code:

set file=*.png
files\pngnq -s 1 -n 16 %file% 
ren *nq8.png 12345.png
files\gimconv 12345.png --format_style psp --format_endian little --pixel_order faster --image_format index4
del 12345.png
ren *.gim READY4BIT.gim
2

I fail to see what's incorrect in my batch code:

FOR /R %a in (*.png) DO (
    files\pngnq -s 1 -n 16 %~fa 
    ren *nq8.png "%~na.png"
    files\gimconv "%~na.png" --format_style psp --format_endian little --pixel_order faster --image_format index4
    del "%~na.png"
    )

I can see two obvious issues with the above code:

  1. In a batch file you need to replace % with %% (in a batch file use %%a, in a cmd shell use %a)

  2. There is a possibility some files may get processed twice, so you should use for /f together with dir).

There may be other things wrong as well, but I don't have the required programs to test it.

Use the following batch file instead:

for /f "tokens=*" %%a in ('dir /b *.png') do (
    files\pngnq -s 1 -n 16 %%~fa 
    ren *nq8.png "%%~na.png"
    files\gimconv "%%~na.png" --format_style psp --format_endian little --pixel_order faster --image_format index4
    del "%%~na.png"
    )

Note:

It is critical that you use FOR /F and not the simple FOR.

The FOR /F gathers the entire result of the DIR command before it begins iterating, whereas the simple FOR begins iterating after the internal buffer is full, which adds a risk of renaming the same file multiple times.

as advised by dbenham in his answer to add "text" to end of multiple filenames:


Further Reading

3
  • Thank you for the clarafication, but your code is half working, it doesn't take the name of the file, it take the first line of the result that dir gives you, the volume of disk D: is named blabla – Omarrrio Jan 18 '16 at 19:10
  • 1
    Oops. Small mistake. My apologies. We need to add /b to the dir command. Answer updated. – DavidPostill Jan 18 '16 at 19:11
  • 1
    That's great! ;) – DavidPostill Jan 18 '16 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.