I found a code which applies random passwords to PDF files and saves it in a separate directory. Can somebody help me in modifying the code as per following requirement.
Need a single password for all pdfs in a folder Read pdf files having spaces in their filename. Source https://gist.github.com/glallen01/3098389
@ECHO OFF
setlocal EnableDelayedExpansion
md out
for /f %%G in ('dir /b "*.pdf"') do (
call:_pwgen passwd
pdftk %%G output out/%%G user_pw !passwd!
echo '%%G', '!passwd!' >> out/passwords.csv
)
goto :EOF
:_pwgen passwd
setlocal ENABLEEXTENSIONS
set _RNDLength=8
set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set _Str=%_Alphanumeric%987654321
set passwd=%~1
:_LenLoop
if not "%_Str:~18%"=="" set _Str=%_Str:~9%& set /A _Len+=9& GOTO :_LenLoop
set _tmp=%_Str:~9,1%
set /A _Len=_Len+_tmp
set _count=0
set _RndAlphaNum=
:_loop
set /a _count+=1
set _RND=%Random%
set /A _RND=_RND%%%_Len%
set _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
if !_count! lss %_RNDLength% goto _loop
set passwd=!_RndAlphaNum!
endlocal&set %~1=%passwd%
GOTO:EOF
答案1
@echo off
cd/d "%~dp0" & setlocal
set "_pdftk=C:\Program Files (x86)\PDFtk\bin\pdftk.exe"
set "_pwd=your password here" && 2>nul mkdir "%cd%\out\."
for %%G in (*.pdf)do (
"%_pdftk%" "%%~G" output ".\out\%%~nxG" user_pw "%_pwd%"
echo/'%%~G', '%%~nxG', '%_pwd%') >".\out\passwords.csv"
endlocal & timeout /t 5 | echo\is done! & goto :EOF
You need more than only remove um call/label
1. Go to your folder, if it is the same as the bat, use cd /d "%~dp0"
, if not cd /d "D:\Path\To\Folder"
2. If any (or all) of the pdf files have a space in the name, use tokens^=*
3. If the destination folder exists, you can still use mkdir
, omitting error/warning if it exists. The 2>nul
is a predictive treatment of a potential error with no relevant impact.
4. Replace for /f ...(dir/...
to normal/regular for ... (*.pdf)do ...
5. Why not set and use the full path to pdftk.exe
:
set "_pdftk=C:\Program Files (x86)\PDFtk\bin\pdftk.exe"