The code below runs perfect as is. The problem is when I uncomment the "For" loop in order to cycle through all of the PDFs in a given directory instead of just a single pdf. At that point the variables no longer behave as they should and my output is garbage. Any suggestions? I've spent tons of time on this and would really hate to have to rewrite it.
REM Initialize label file
echo RECID,COMPANY,ATTN,ADD1,ADD2,CITY,ST,CNTRY,Zip,phone,UPSSERV,BILLTO,PACKTYPE,REF1,REF2,REF5,PACKWT,LNGTH,WDTH,HGTH,SHCOM,SHATTN,SHADD1,SHCITY,SHST,SHZIP,SHCNTRY,SHPACCT,SHPHONE,BILLTO,ATT2,BILLADD1,BILLADD2,BILLCITY,BILLST,BILLCNTRY,BILLZIP,BILLPHONE,BILLACCT> labels.csv
REM Initialize document count file
del documentcount.txt
REM Initialize page count file
del pagecount.txt
REM for %%i in ("\\TOPMFG-MWS-FS01\Prepress\OptumComplete\DownloadedPDFs\Dev\*.pdf") do (
REM extract address block
C:\poppler\Library\bin\pdftotext -f 1 -l 2 -x 72 -y 125 -W 250 -H 7 \\TOPMFG-MWS-FS01\Prepress\OptumComplete\DownloadedPDFs\Dev\test.pdf Name.txt
C:\poppler\Library\bin\pdftotext -f 1 -l 2 -x 72 -y 137 -W 250 -H 7 \\TOPMFG-MWS-FS01\Prepress\OptumComplete\DownloadedPDFs\Dev\test.pdf Street.txt
C:\poppler\Library\bin\pdftotext -f 1 -l 2 -x 72 -y 145 -W 250 -H 11 \\TOPMFG-MWS-FS01\Prepress\OptumComplete\DownloadedPDFs\Dev\test.pdf CityStZip.txt
REM get Name
set /p firstLast=< Name.txt
REM get Street
set /p add=< Street.txt
REM parse City State Zip from CityStateZip
set /p str=< CityStZip.txt
call :strLen str strlen
call :lastSpace str lastSpace
set /a "zipLen=%strlen%-%lastSpace%"
set "_donor=%str%"
set /a "city_startchar=0"
set /a "city_length=%strlen%-%zipLen%-3"
set /a "state_startchar=%strlen%-%zipLen%-2"
set /a "state_length=2"
set /a "zip_startchar=%strlen%-%zipLen%+1"
set /a "zip_length=%zipLen%-1"
REM get city
CALL SET "city=%%_donor:~%city_startchar%,%city_length%%%"
REM get state
CALL SET "state=%%_donor:~%state_startchar%,%state_length%%%"
REM get zip
CALL SET "zip=%%_donor:~%zip_startchar%,%zip_length%%%"
REM get Pages
C:\poppler\Library\bin\pdfinfo \\TOPMFG-MWS-FS01\Prepress\OptumComplete\DownloadedPDFs\Dev\test.pdf|find "Pages:">pages.txt
set /p pages=< pages.txt
call :strLen pages strlen
call :lastSpace pages lastSpace
set /a "pagesLen=%strlen%-%lastSpace%"
set "_donor=%pages%"
set /a "pages_startchar=%strlen%-%pagesLen%+1"
set /a "pages_length=%pagesLen%-1"
CALL SET "docpages=%%_donor:~%pages_startchar%,%pages_length%%%"
REM Increment page count
setlocal enabledelayedexpansion
if exist pagecount.txt (
set /p totalpages=<pagecount.txt
set /a "totalpages=totalpages+docpages"
(echo !totalpages!)>pagecount.txt
) else (
(echo !docpages!)>pagecount.txt
)
REM Increment document count
setlocal enabledelayedexpansion
if exist documentcount.txt (
set /p totaldocs=<documentcount.txt
set /A totaldocs=totaldocs+1
(echo !totaldocs!)>documentcount.txt
) else (
(echo 1)>documentcount.txt
)
REM Append label file
echo ,%firstLast%,,%add%,,%city%,%state%,,%zip%,,1DP,TP,CP,22406,test.pdf,,1,,,,THOUSAND OAKS,SHIPPING,5334 STERLING CENTER DR,WESTLAKE VILLAGE,CA,91361,US,XXXXXXX,8187968330,THOUSAND OAKS PRINTING,SHIPPING,5334 STERLING CENTER DR,,WESTLAKE VILLAGE,CA,US,91361,8187068330,V55633>> labels.csv
REM)
REM Generate summary report
exit /b
:strLen
setlocal enabledelayedexpansion
:strLen_Loop
if not "!%1:~%len%!"=="" set /a len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
goto :eof
:lastSpace
setlocal enabledelayedexpansion
:lastSpace_Loop
set firstchar="!%1:~%strlen%!"
if not "!firstchar:~1,1!"==" " set /a strlen=strlen-1 & goto :lastSpace_Loop
(endlocal & set %2=%strlen%)
goto :eof