Page 1 of 1

Passing arguments to a subroutine

Posted: 06 Nov 2022 01:03
by tinfanide
How can I fix code snippet A? Thanks!

Code snippet A
This does not work:

Code: Select all

SET pwd=mypassword
SET files_path=C:\Users\User\Downloads\_bat testing\*.pdf
SET qpdf_path=C:\Program Files\qpdf 11.1.1\bin\qpdf.exe
for %%G in ("%files_path%") do (CALL :subroutine "%qpdf_path%" %pwd% "%%G")

:subroutine
%1 --decrypt --password=%2 "%3" "%~n3_unlocked%~x3"
GOTO :eof
Code snippet B
This works:

Code: Select all

setlocal disableDelayedExpansion
for %%G in ("C:\Users\User\Downloads\_bat\*.pdf") do (CALL :subroutine "%%G")

:subroutine
"C:\Program Files\qpdf 11.1.1\bin\qpdf.exe" --decrypt --password=mypassword "%1" "%~n1_unlocked%~x1"
GOTO :eof
Code snippet C
This works:

Code: Select all

SET pwd=mypassword
SET files_path=C:\Users\User\Downloads\_bat testing\*.pdf
SET qpdf_path=C:\Program Files\qpdf 11.1.1\bin\qpdf.exe

setlocal disableDelayedExpansion
for %%G in ("%files_path%") do (
	set name=%%~nG
	set ext=%%~xG
	setlocal enableDelayedExpansion
	"%qpdf_path%" --decrypt --password=%pwd% "%%G" "!name!_unlocked!ext!"   
	endlocal
)

Re: Passing arguments to a subroutine

Posted: 06 Nov 2022 02:08
by ShadowThief
"It does not work" doesn't really give us enough information. What error message are you getting?

Right now, my best guess is that because you pass %%G with quotes surrounding it and then you reference "%3" in the subroutine, you're doubling up the quotes, which is breaking things. Either get rid of the quotes surrounding the %3 or use "%~3" instead.

Re: Passing arguments to a subroutine

Posted: 06 Nov 2022 14:01
by miskox
I would add: use ECHO ON before you call :subroutine and you will see what is passed and how your command looks like. And from there you know if you have to add/remove some characters (as " or ~).

Saso

Re: Passing arguments to a subroutine

Posted: 06 Nov 2022 14:52
by aGerman
Another guess: I suspect "mypassword" is something different in real world. If it contains characters like % or ^ the CALL command will alter it.

Steffen

Re: Passing arguments to a subroutine

Posted: 08 Nov 2022 03:40
by tinfanide
Your guess is right. The repeated double quotes break it. The password is a letter password, with no special characters though. Thank you all.