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.

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 
| improve this answer | |
  • 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 Oct 5 '19 at 13:25
  • @uncaged Answer updated. Sorry, I couldn't test the code not having KPScript.exe installed. – JosefZ Oct 5 '19 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 Oct 5 '19 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 Oct 5 '19 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.