Solved Batch File Loop help

July 1, 2013 at 14:01:21
Specs: Windows 7
I have a .txt file with the following lines in it:

urn_lps_onepass_rp_invoicemanagement_pa_cert_encryption
urn_lps_onepass_rp_invoicemanagement_prod_cert_encryption
urn_lps_onepass_rp_invoicemanagement_qa1_cert_encryption
urn_lps_onepass_rp_ssoadmin_codev2_cert_encryption

I wrote a batch file that works with one single line:

@echo Off


echo Certificate Name = %cert%

C:\openssl\bin\openssl pkcs12 -passin pass:cert4wintel -in "c:\ssl\%cert%.pfx" -nocerts -nodes -out "c:\ssl\keys\%cert%_key.pem"

c:\openssl\bin\openssl pkcs12 -passin pass:cert4wintel -in "c:\ssl\%cert%.pfx" -nokeys -out "c:\ssl\certs\%cert%_cert.pem"

type c:\ssl\roots.txt >> c:\ssl\certs\%cert%_cert.pem

However I am trying to get it to work with a text file and multiple lines. this is what I am using:

FOR /f "Tokens=*" %%a in (certs.txt) do call multicert.bat %%a

It runs, but only processes the first line 4 times. I need it to run that subroutine for earch line in secession. Any ideas?

Thanks


See More: Batch File Loop help

Report •


✔ Best Answer
July 3, 2013 at 18:41:11
I'm really confused.... It looks like you "internalized" the code from post #1 script #2 (the called multicert.bat) into the main script in your last post.. At that point, %1 is now taken directly from the commandline, and no longer submitted from a "caller" batch. Your file-loop terminates at line #1 (echo %%a), so after echoing the contents of certs.txt, the loop is done:
FOR /f "Tokens=*" %%a in (certs.txt) do echo %%a
:: certs.txt is now closed and the file-iteration-loop is terminated.
echo Certificate Name = %1
:: %1 can only come from the commandline, since this is no longer a called sub-routine, and if that is your intent, you need to validate that %1 at least exists:
IF "%1" EQU "" ECHO PARAMATER REQUIRED
everything else you have after the loop termination will only act on %1 which is NOT from certs.txt, but from the commandline. If I read this right, you want to do this:
for /f %%a in (certs.txt) do (
C:\openssl\bin\openssl pkcs12 -passin pass:cert4wintel -in "c:\ssl\%%a.pfx" -nocerts -nodes -out "c:\ssl\keys\%%a_key.pem"

c:\openssl\bin\openssl pkcs12 -passin pass:cert4wintel -in "c:\ssl\%%a.pfx" -nokeys -out "c:\ssl\certs\%%a_cert.pem"

type c:\ssl\roots.txt >> c:\ssl\certs\%%a_cert.pem
)

But not being proficient in reading minds, I may be "off the beam" as per your intent. Maybe you do want a commandline var, in which case, then substitute %1 for %%a.
Also, (my habit for testing), first capture the material and examine it, before trying to do any real operations (openssl, zip) on it. It doesn't matter, but it just makes things easier to see because you don't have all that extra verbiage/baggage.




#1
July 1, 2013 at 14:36:39
Multicert.bat script must have somewhere in the code a %1 external variable that is substituted by the current text line catched by the %%a in the call statement.

Report •

#2
July 3, 2013 at 08:04:34
Ok, I swear this was working yesterday. Here is the new script:

@echo Off

FOR /f "Tokens=*" %%a in (certs.txt) do echo %%a

echo Certificate Name = %1

C:\openssl\bin\openssl pkcs12 -passin pass:cert4wintel -in "c:\ssl\%1.pfx" -nocerts -nodes -out "c:\ssl\keys\%1_key.pem"

c:\openssl\bin\openssl pkcs12 -passin pass:cert4wintel -in "c:\ssl\%1.pfx" -nokeys -out "c:\ssl\certs\%1_cert.pem"

type c:\ssl\roots.txt >> c:\ssl\certs\%1_cert.pem

if not exist "c:\Program Files (x86)\WinZip\wzzip.exe" GOTO 7zip
IF Exist GOTO winzip

if not exist "c:\Program Files (x86)\7-zip\7z.exe" GOTO winzip
IF Exist GOTO 7zip

:winzip
"c:\Program Files (x86)\winzip\wzzip.exe" -rp %1.zip c:\ssl\keys\%1_key.pem
"c:\Program Files (x86)\winzip\wzzip.exe" -rp %1.zip c:\ssl\certs\%1_cert.pem
"c:\Program Files (x86)\winzip\wzzip.exe" -rp %1.zip c:\ssl\%1.pfx


:7zip
"c:\Program Files (x86)\7-zip\7z.exe" a C:\NewCSScerts\%1.7z c:\ssl\keys\%1_key.pem
"c:\Program Files (x86)\7-zip\7z.exe" a C:\NewCSScerts\%1.7z c:\ssl\certs\%1_cert.pem
"c:\Program Files (x86)\7-zip\7z.exe" a C:\NewCSScerts\%1.7z c:\ssl\%1.pfx

Move c:\ssl\certs\%1_cert.pem c:\ssl\certs\archive

Move c:\ssl\keys\%1_key.pem c:\ssl\keys\archive

Move c:\ssl\%1.pfx c:\ssl\certs\archive

The variable never gets passed to the %1 for the rest of the script. Any ideas?


Report •

#3
July 3, 2013 at 09:25:51
You could reduce this:

"c:\Program Files (x86)\7-zip\7z.exe"

to:

7z

by having it in your PATH.

Beyond that, are you calling the bat with a parameter that ought to become %1?

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
July 3, 2013 at 18:41:11
✔ Best Answer
I'm really confused.... It looks like you "internalized" the code from post #1 script #2 (the called multicert.bat) into the main script in your last post.. At that point, %1 is now taken directly from the commandline, and no longer submitted from a "caller" batch. Your file-loop terminates at line #1 (echo %%a), so after echoing the contents of certs.txt, the loop is done:
FOR /f "Tokens=*" %%a in (certs.txt) do echo %%a
:: certs.txt is now closed and the file-iteration-loop is terminated.
echo Certificate Name = %1
:: %1 can only come from the commandline, since this is no longer a called sub-routine, and if that is your intent, you need to validate that %1 at least exists:
IF "%1" EQU "" ECHO PARAMATER REQUIRED
everything else you have after the loop termination will only act on %1 which is NOT from certs.txt, but from the commandline. If I read this right, you want to do this:
for /f %%a in (certs.txt) do (
C:\openssl\bin\openssl pkcs12 -passin pass:cert4wintel -in "c:\ssl\%%a.pfx" -nocerts -nodes -out "c:\ssl\keys\%%a_key.pem"

c:\openssl\bin\openssl pkcs12 -passin pass:cert4wintel -in "c:\ssl\%%a.pfx" -nokeys -out "c:\ssl\certs\%%a_cert.pem"

type c:\ssl\roots.txt >> c:\ssl\certs\%%a_cert.pem
)

But not being proficient in reading minds, I may be "off the beam" as per your intent. Maybe you do want a commandline var, in which case, then substitute %1 for %%a.
Also, (my habit for testing), first capture the material and examine it, before trying to do any real operations (openssl, zip) on it. It doesn't matter, but it just makes things easier to see because you don't have all that extra verbiage/baggage.



Report •

#5
July 8, 2013 at 05:23:19
That worked. I think my logic was a little flawed.

Report •


Ask Question