How to generate random passwords for PDF files using a batch program -


i need help. doing batch program following:

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

a_000100.txt, a_000200.txt, b_000100.txt, b_000200.txt, c_000100.txt, c_000200.txt, d_000100.txt, d_000200.txt, e_000100.txt, e_000200.txt, f_000100.txt, f_000200.txt

  1. all text files must individually converted pdf using command line. address this, installed program "adultpdf txt pdf converter". has command line functions converts text files pdf different parameters. below line shows how text file converted pdf. in sample code below, output pdf file's paper set letter size (-pps4) , font size 10.9 (-pfs10.9).

txttopdf a_000100.txt a_000100.pdf -pps4 -pfs10.9

  1. after each text files individually converted pdf, pdf files have same last 6 characters on filenames (example, a_000100.pdf, b_000100.pdf, c_000100.pdf, etc.) should combined single pdf , output filename r_000100.pdf. output pdf file stored in same folder.

  2. the combined pdf file (example, r_000100.pdf) must encrypted password, should randomly generated. passwords of each combined pdf file should different 1 another.

    • to 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 shows how combine multiple pdf files one, , adding random password pdf file.

pdftk *%%i.pdf cat output r_%%i.pdf owner_pw %random%

  1. and lastly, log file (.csv) should created list filenames of combined pdf files randomly generated passwords each pdf file. below code used this:

echo %%i r_%%i.pdf %random% >> passwords.csv

actually, generation of random password , storing them csv file i'm having problem. doesn't seem work. can't figure out how correctly. can proficient in batch programming please me? below complete batch program have accomplished far. if there corrections need done program make simpler, don't hesitate correct it. appreciated. thanks!

@echo off /f "tokens=1,2 delims=abcdef_." %%i in ('dir *.txt *.pdf /b') (  txttopdf a_%%i.txt a_%%i.pdf -pps4 -pfs10.9 txttopdf b_%%i.txt b_%%i.pdf -pps4 -pfs8.9 txttopdf c_%%i.txt c_%%i.pdf -pps4 -plm50 -prm50 -pfs7.9 txttopdf d_%%i.txt d_%%i.pdf -pps4 -plm60 -prm60 -pfs8.9 txttopdf e_%%i.txt e_%%i.pdf -pps5 -pot -pfs10 txttopdf f_%%i.txt f_%%i.pdf -pps5 -pot -pfs12 pdftk *%%i.pdf cat output pdf\r_%%i.pdf owner_pw %random% echo %%i r_%%i.pdf %random% >> passwords.csv  )  echo                *************************************************** echo                *                                                 * echo                *       pdf reports generated.       * echo                *       please type exit @ command prompt.       * echo                *                                                 * echo                *************************************************** pause exit 

delayed expansion - within block, %var% including %random% replaced value @ time block (parenthesised statement-sequence) parsed.

solution invoke delayed expansion using statement

setlocal enabledelayedexpansion 

after @echo off

then need access changing value of random accessing !random! rather %random%

but need send same random value file used password , !random! generate new value each time it's accessed - need put varying random value variable , use varying value stored in variable both password , data in csv

set pass=!random! pdftk *%%i.pdf cat output pdf\r_%%i.pdf owner_pw !pass! echo %%i r_%%i.pdf !pass! >> passwords.csv 

naturally, need !pass! not %pass% access new value of pass varies within loop.

now - random number may 1-5 digits in length. may wish have longer or constant-length password

set pass=!random!!random!!random!!random!!random!!random! 

would set pass 6 30-digit string.

set pass=!pass:~0,8! 

would select first 8 of these characters. (the probability of fewer 8 characters low)


edit 20130718t1446z

hmm. interesting little adventure. solved password issue, no doubt misled statement "actually, generation of random password , storing them csv file i'm having problem"

ok - first, there's no point in listing both *.txt , *.pdf. *.txt processed txttopdf executable. delims... yes - putting alphas in there neatly got rid of leading alpha , underscore, leaving numeric portion.

so... why repeats? first dir examine current directory , build list of matching files, (i'll truncate) a_001, a_002, a_003...b_001, b_002...f_001, f_002, f_003 , pick out 001,002,003...001,002,003...001,002,003 , process them. consequently, 001, 002, 003 each processed 6 times. final processing of particular number cause a_,b_,...f_ executed, overwriting previous x_999.pdf last password applied inthe case of particular number final version.

revised code:

@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  set pass=!random!!random!!random!!random!!random!!random!!random!!random!  set pass=!pass:~-8!  pdftk *%%j.pdf cat output pdf\r_%%j.pdf owner_pw !pass!  echo %%j r_%%j.pdf !pass! >> passwords.csv  )  goto :eof 

(ok- i've left off message-block frippery...)

initially, prevsuff st :: - non-empty string cannot possibly filename. dir executed, finding files match ?_*.txt , list sorted in alphabetical order on second column - _. consequently, when for processes data, lines presented of number part grouped. applies prefix letter %%i , numeric part %%j because of _ in delims. . in delims drops .txt suffix %%j

(well, ok - have been written tokens=2delims=_. apply numeric part %%i - exercise interested...)

now if numeric part (%%j) not match previous suffix in prevsuff process one. first adjust prevsuff 1 we're process, process it. when next number list, it'll match skip until have next change...

the if exists suppress ugly messages if there's file missing (a_001 exists, d_001 doesn't...); 8 randoms cascaded pass there 8..40 digits assigned, , last 8 chosen !pass:~-8!

from there, should plain sailing.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -