Passing arguments to a subroutine

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Passing arguments to a subroutine

#1 Post by tinfanide » 06 Nov 2022 01:03

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
)

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Passing arguments to a subroutine

#2 Post by ShadowThief » 06 Nov 2022 02:08

"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.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Passing arguments to a subroutine

#3 Post by miskox » 06 Nov 2022 14:01

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Passing arguments to a subroutine

#4 Post by aGerman » 06 Nov 2022 14:52

Another guess: I suspect "mypassword" is something different in real world. If it contains characters like % or ^ the CALL command will alter it.

Steffen

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: Passing arguments to a subroutine

#5 Post by tinfanide » 08 Nov 2022 03:40

Your guess is right. The repeated double quotes break it. The password is a letter password, with no special characters though. Thank you all.

Post Reply