Assigning a variable based on text search of a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tlwalker3
Posts: 3
Joined: 28 Mar 2017 12:46

Assigning a variable based on text search of a file

#1 Post by tlwalker3 » 28 Mar 2017 13:13

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 28 Mar 2017 13:29

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

tlwalker3
Posts: 3
Joined: 28 Mar 2017 12:46

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

#3 Post by tlwalker3 » 28 Mar 2017 15:22

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.

tlwalker3
Posts: 3
Joined: 28 Mar 2017 12:46

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

#4 Post by tlwalker3 » 28 Mar 2017 15:31

Edited post to remove file content.
Last edited by tlwalker3 on 28 Mar 2017 21:03, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#5 Post by aGerman » 28 Mar 2017 15:49

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

igor_andreev
Posts: 16
Joined: 25 Feb 2017 12:55
Location: Russia

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

#6 Post by igor_andreev » 31 Mar 2017 15:59

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"

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

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

#7 Post by Samir » 13 Apr 2017 14:22

What is the output of your current batch file? It looks like it should work to me. :?:

Post Reply