windows - Using a batch program, how to loop and read lines in a csv file, get the matching value, then assign it as password to a PDF -


i need again. doing batch program following:

in \reports\ folder several text files stored. example,

  - a_000001.txt  - a_000002.txt  - b_000001.txt  - b_000002.txt  - c_000001.txt  - c_000002.txt  - d_000001.txt  - d_000002.txt  - e_000001.txt  - e_000002.txt  - f_000001.txt  - f_000002.txt 
  1. the batch program individually converts text files pdf use of "adultpdf txt pdf converter" command line functions.

  2. the converted pdf files have same last 6 characters on filenames (example, a_000001.pdf,...f_000001.pdf) combined single pdf given output filename r_000001.pdf. combined pdf files stored \reports\pdf reports\ folder.

  3. the last thing want in batch program assign password combined pdf file. password come csv file (passwords.csv) contains following columns:

filename of combined pdf,password pdf

for example:

  - r_000001.pdf,abcd1234  - r_000002.pdf,efgh5678  - r_000003.pdf,ijkl0910 

in order address items number 2 & 3, installed program pdftk (or pdf toolkit). has command line functions combines pdf files different parameters, including adding password pdf file. below line batch program shows how combine multiple pdf files one, , adding password pdf file.

pdftk *%%j.pdf cat output "pdf reports\r_%%j.pdf" user_pw abcd1234

i know how wherein example, if combined pdf's filename r_000002.pdf, batch program through filenames listed in passwords.csv , if matching filename found, password next (which efgh5678) assigned combined pdf.

below complete batch program have accomplished far. appreciated.

@echo off setlocal enabledelayedexpansion  set prevsuff=::  /f "tokens=1,2 delims=_." %%i in ('dir ?_*.txt /b ^|sort /+2') if not !prevsuff!==%%j (  set prevsuff=%%j  if exist a_%%j.txt txttopdf a_%%j.txt a_%%j.pdf -pps4 -pfs10.9  if exist b_%%j.txt txttopdf b_%%j.txt b_%%j.pdf -pps4 -pfs8.9  if exist c_%%j.txt txttopdf c_%%j.txt c_%%j.pdf -pps4 -plm50 -prm50 -pfs7.9  if exist d_%%j.txt txttopdf d_%%j.txt d_%%j.pdf -pps4 -plm60 -prm60 -pfs8.9  if exist e_%%j.txt txttopdf e_%%j.txt e_%%j.pdf -pps5 -pot -pfs10  if exist f_%%j.txt txttopdf f_%%j.txt f_%%j.pdf -pps5 -pot -pfs12  pdftk *%%j.pdf cat output "pdf reports\r_%%j.pdf" user_pw abcd1234 ) 

to read csv, use

for /f "delims=, tokens=1,2" %%p in (csvfile) command 

for each line in csvfile, command called. %%p contain pdf filename , %%q contain password.

to search specific pdf:

for /f "delims=, tokens=1,2" %%p in (csvfile) if /i "%%p"=="r_%%j.pdf" pdftk *%%j.pdf cat output "pdf reports\r_%%j.pdf" user_pw %%q 

Comments