if condition inside a for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
audrey
Posts: 3
Joined: 20 Jul 2014 18:13

if condition inside a for loop

#1 Post by audrey » 20 Jul 2014 18:59

hello everyone,

I want to loop through pdf files, find the ones that are portrait, and move them to a seperate folder.
pdftk is an executable that gives me this information, so when i type
for /f "tokens=2" %%a in ('pdftk %%g dump_data ^| find "PageMediaDimensions"') do (
set /a dim1=%%a
)

dim1 represents one of the dimensions and dim2 the other.
That part works well.

After, I just have to compare dim1 and dim2 to see if %dim1% GTR %dim2%, and if it is, move the file to the appropriate Folder

But... it doesn't work. I think the problem comes from using dim1 inside a for loop, it is not seen as a number
Any ideas ?

Thanks,
Audrey


for %%g in (*.pdf) do (
for /f "tokens=2" %%a in ('pdftk %%g dump_data ^| find "PageMediaDimensions"') do (
set /a dim1=%%a
)
for /f "tokens=3" %%b in ('pdftk %%g dump_data ^| find "PageMediaDimensions"') do (
set /a dim2=%%b
)
if %dim1% GTR %dim2% (
move %%g myFolder
)
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if condition inside a for loop

#2 Post by foxidrive » 20 Jul 2014 20:20

Try this - see if the echo command shows the two items correctly before removing the echo and pause:


Code: Select all

@echo off
for %%g in (*.pdf) do (
   for /f "tokens=2,3" %%a in ('pdftk "%%g" dump_data ^| find "PageMediaDimensions" ') do (
         echo if %%a GTR %%b move "%%g" "myFolder"
         pause
   )
)


Your problem was essentially that variables can't be set or changed within a for loop unless you use
setlocal enabledelayedexpansion and !variable! syntax.

audrey
Posts: 3
Joined: 20 Jul 2014 18:13

Re: if condition inside a for loop

#3 Post by audrey » 20 Jul 2014 21:59

Hell Foxidrive,
That's what i see :

if 1191 GTR 842 move "doc1.pdf" "myFolder"
Press any key to continue . . .
if 842 GTR 1191 move "doc2.pdf" "myFolder"
Press any key to continue . . .

Nothing seems to happen i'm afraid...

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: if condition inside a for loop

#4 Post by Squashman » 20 Jul 2014 22:03

Remove the ECHO command.

audrey
Posts: 3
Joined: 20 Jul 2014 18:13

Re: if condition inside a for loop

#5 Post by audrey » 20 Jul 2014 22:09

Thanks everyone !
It works.

Post Reply