Convert pdf if has less than 2 pages Command line

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
tamle
Posts: 3
Joined: 2018-10-22T12:03:04-07:00
Authentication code: 1152

Convert pdf if has less than 2 pages Command line

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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 
snibgo's IM pages: im.snibgo.com
tamle
Posts: 3
Joined: 2018-10-22T12:03:04-07:00
Authentication code: 1152

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
tamle
Posts: 3
Joined: 2018-10-22T12:03:04-07:00
Authentication code: 1152

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

Post 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)
Post Reply