Page 1 of 1

Convert pdf if has less than 2 pages Command line

Posted: 2018-10-22T12:21:43-07:00
by tamle
I am trying to convert PDF files into jpg but only if it has 1 page. So I have run successfully both the command to identify the page and convert files. However, I could get it to work together in a bat file. I am trying to use this code. This bat file is put in a folder of many pdf, and the code seems to identify them correctly. I have not get to the part of conditional conversion yet. The comment out part was working by itself, before get nested.

Code: Select all

pause
set path="C:\Program Files\ImageMagick-7.0.8-Q16\";%path%
FOR /r %%g in (*.pdf) DO (
	for /f %%i in ('identify -format %n %%g') do set pgs=%%i
	echo %pgs%
	echo "%%g"
::convert %%~ng%%~xg %%~ng.jpg
::del %%~ng.pdf
)
pause
It keeps saying the syntax of the command is incorrect. But I tried the "identify..." in command prompt is working correctly. I don't work with command line very often.

Re: Convert pdf if has less than 2 pages Command line

Posted: 2018-10-22T12:34:50-07:00
by snibgo
Your line...

Code: Select all

echo %pgs%
... should be ...

Code: Select all

echo !pgs!
... and you should check you have enableddelayedexpansion.

Your ...

Code: Select all

-format %n 
... should be ...

Code: Select all

-format %%n 

Re: Convert pdf if has less than 2 pages Command line

Posted: 2018-10-22T18:16:06-07:00
by tamle
The new full code is like this and it return "do was unexpected at this time."

Code: Select all

SETLOCAL EnableDelayedExpansion
pause
set path="C:\Program Files\ImageMagick-7.0.8-Q16\";%path%

FOR /r %%g in (*.pdf) DO (
	for /f %%i in ('identify -format %%n %%g') do set pgs=%%i
	echo !pgs!
::convert %%~ng%%~xg %%~ng.jpg
::del %%~ng.pdf
)

pause

Re: Convert pdf if has less than 2 pages Command line

Posted: 2018-10-22T18:45:07-07:00
by snibgo
You can put "rem" inside loops, but not "::".

As you are learning the basics of BAT scripting, I suggest you find a good manual or webpage on the subject.

Re: Convert pdf if has less than 2 pages Command line

Posted: 2018-10-22T18:59:43-07:00
by tamle
snibgo wrote: 2018-10-22T18:45:07-07:00 You can put "rem" inside loops, but not "::".

As you are learning the basics of BAT scripting, I suggest you find a good manual or webpage on the subject.
Your suggestion is correct. This language seems to be far stricter and precise in structure compare to the languages that I am familiar with. Do you happen to know a good source for it? Since my knowledge of the subject is very limited, I would hardly know which one is good.

Also, for some image, this conversion don't look very good (quite pixelated), is there a parameter that I could use to improve that? (there are so many complex options)