1

The following command works fine:

"C:\Program Files (x86)\KeePass Password Safe 2\KPScript.exe" -c:GetEntryString "C:\Users\Me\Documents\KeePass Database.kdbx" -useraccount -field:Password -ref-Title:"Data Partition" -FailIfNotExists -FailIfNoEntry

It returns a password out of a KeePass database. In a batch file, I want to put the result of that into a variable, so I can pass it to another program.

I tried the following in a batch file:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=* usebackq" %%F IN (`"C:\Program Files (x86)\KeePass Password Safe 2\KPScript.exe" -c:GetEntryString "C:\Users\Me\Documents\KeePass Database.kdbx" -useraccount -field:Password -ref-Title:"Data Partition" -FailIfNotExists -FailIfNoEntry`) DO SET pw=%%F
ECHO %pw%
ENDLOCAL

But calling this batch file returns:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

I tried putting quotes around the command in FOR:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=* usebackq" %%F IN (`""C:\Program Files (x86)\KeePass Password Safe 2\KPScript.exe" -c:GetEntryString "C:\Users\Me\Documents\KeePass Database.kdbx" -useraccount -field:Password -ref-Title:"Data Partition" -FailIfNotExists -FailIfNoEntry"`) DO SET pw=%%F
ECHO %pw%
ENDLOCAL

And this time I got:

\KeePass was unexpected at this time.

What is the correct syntax to make this work? Thanks.

0

1 Answer 1

2

Apply the latter approach and escape the closing parenthesis using a caret as follows:

FOR /F "tokens=* usebackq" %%F IN (`""C:\Program Files (x86^)\KeePass …

or

FOR /F "tokens=* usebackq" %%F IN (`""%ProgramFiles(x86):)=^)%\KeePass …

Moreover, always

  • set variables with Delayed Expansion disabled to prevent expansion of (possibly present) ! exclamation marks;
  • set variables using double quotes (as SET "pw=%%F" below) to escape (possibly present) cmd-poisonous characters like &, <, | etc…;
  • use variables with Delayed Expansion enabled.

Then, your script could look as follows (updated):

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
rem remove variable `pw`
set "pw="
FOR /F "tokens=* usebackq" %%F IN (`""C:\Program Files (x86^)\KeePass Password Safe 2\KPScript.exe" -c:GetEntryString "C:\Users\Me\Documents\KeePass Database.kdbx" -useraccount -field:Password -ref-Title:"Data Partition" -FailIfNotExists -FailIfNoEntry"`) DO (
   rem set only first output line to variable `pw`
   if NOT defined pw SET "pw=%%F"
)
SETLOCAL EnableDelayedExpansion
ECHO !pw!
ENDLOCAL 
4
  • JosefZ: This is awesome. Thank you so much. New problem: When I run just that first command I gave above, it returns 2 lines, the password and "OK: Operation completed successfully." Unfortunately, %pw% is getting set to the latter.
    – uncaged
    Commented Oct 5, 2019 at 13:25
  • @uncaged Answer updated. Sorry, I couldn't test the code not having KPScript.exe installed.
    – JosefZ
    Commented Oct 5, 2019 at 14:57
  • Wow, I'm so impressed. If you're on Fiverr or UpWork, please let me know. I'd happily pay you for occasional help.
    – uncaged
    Commented Oct 5, 2019 at 15:04
  • 😎 If my answer was helpful, please consider marking it as accepted. See this page for an explanation of why this is important.
    – JosefZ
    Commented Oct 5, 2019 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .