Page 1 of 1

Assigning a variable based on text search of a file

Posted: 28 Mar 2017 13:13
by tlwalker3
I need to search a PDF file based on a string, then grab the value that follows and assign it to a variable name. If I open the PDF file in notepad, I can see the value and it is always preceded by the string "12 0 0 12 108 524.5 Tm(" .

So as an example: The PDF file contains "12 0 0 12 108 524.5 Tm(10168536)", I need to assign the value "10168536" as a dos variable so I can create a folder by the same name.

I am trying to use the FOR and/or FINDSTR commands to locate the value and assign the variable. Below is what I've tried so far and it is not working. Does anyone know if a better way to do this?

@echo off
setlocal enableextensions enabledelayedexpansion
set revision=
for /f "delims=" %%a in ("Setup Verification.pdf") do (
set line=%%a
if "x!line:~0,17!"=="
12 0 0 12 108 524.5 Tm
(" (
set revision=!line:~17!
)
)
echo !revision!
endlocal

Re: Assigning a variable based on text search of a file

Posted: 28 Mar 2017 13:29
by aGerman
Most likely you would need a utility to prepare the pdf content for command line processing. Have a look at PDFtk. In order to help you we would need the original pdf file.

Steffen

Re: Assigning a variable based on text search of a file

Posted: 28 Mar 2017 15:22
by tlwalker3
Steffen, Thank you for the quick response. I am attaching one of the PDF files as an example. I need to extract the work order number (10168536) from the PDF and store it as a variable to create a directory by the same name.

Re: Assigning a variable based on text search of a file

Posted: 28 Mar 2017 15:31
by tlwalker3
Edited post to remove file content.

Re: Assigning a variable based on text search of a file

Posted: 28 Mar 2017 15:49
by aGerman
The site supports ZIP files.

You may have noticed that 12 0 0 12 108 524.5 Tm and the number you are looking for are not in the same line in your pasted content. No idea what the forum software did here. Please upload the file packed in a zip archive.

Steffen

Re: Assigning a variable based on text search of a file

Posted: 31 Mar 2017 15:59
by igor_andreev

Code: Select all

textw "Setup Verification.pdf" > tmp.txt
for /f "tokens=integer delims=delimiter" %%a in ('type tmp.txt') do ....
....

textw.exe(win32 version 16Kb) here: http://adoxa.altervista.org/misc/index.html
See "Text v1.00 Display readable text within binary files"

Re: Assigning a variable based on text search of a file

Posted: 13 Apr 2017 14:22
by Samir
What is the output of your current batch file? It looks like it should work to me. :?: