0

The following windows batch file will result in either a 1 or a 0. I am trying to get that 1 or 0 output into a variable so that I can use logic to do further processing:

@echo off
set page=1    
pdftr.exe -searchtext "INFORME A LOS PADRES - KINDERGARTEN" -pagerange %page% in-whole.pdf' | 'find /C "Search keyword in page"

I thought I could do it like this based on other information found here on StackOverflow, however, I don't think that pipes are allowed because I get an error "| was unexpected at this time." Here is what I tried:

@echo off
set page=1
for /f %%i in ('pdftr.exe -searchtext "INFORME A LOS PADRES - KINDERGARTEN" -pagerange %page% in-whole.pdf' | 'find /C "Search keyword in page") do set RESULT=%%i
echo The result is: %RESULT%

Is there a way I can take my command and output the results (the 1 or 0) to a variable so I can use it in other logic within my batch file?

Also, just or some background, what I am ultimately trying to do is determine the grade level and language (english or spanish) of a pdf file, so that I can add an appropriate insert to the pdf using cpdf. But I am stuck on this part.

Squashman
  • 13,649
  • 5
  • 27
  • 36
user3513237
  • 995
  • 3
  • 9
  • 26
  • 3
    just escape the pipe symbol with a caret and skip the `'` before and after (the command is the whole thing before, including and after the pipe) : `....pdf ^| find ...` – Stephan Feb 09 '18 at 21:30
  • Nice! That allows my command to run. I must have something else wrong however, because the variable always contains nothing. Maybe that's because of the "do". – user3513237 Feb 09 '18 at 21:45
  • I ended up using Stephan's answer above! Thanks! I would mark it as the correct answer, but don't think I can in the comments. – user3513237 Feb 10 '18 at 01:22
  • an empty variable? Probably your code is in a code block, which needs [delayed expansion](https://stackoverflow.com/a/30284028/2152082) – Stephan Feb 10 '18 at 12:37

1 Answers1

0

You need to escape | which is a special character with a caret.

@echo off
 set page=1
 for /f %%i in ('pdftr.exe -searchtext "INFORME A LOS PADRES - KINDERGARTEN" -pagerange %page% in-whole.pdf' ^| 'find /C "Search keyword in page") do set RESULT=%%i
echo The result is: %RESULT%
Gerhard
  • 22,678
  • 7
  • 27
  • 43