You are not logged in.

#1 20 Dec 2015 17:58

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Load and escape contents of a text file to a variable

I have a text file (link.txt) that contains:

AQAANCMnd8BFdERjHoAwE/Cl+sBAAAAwnwgPPduaUC6ljynwOT1/gAAAAACAAAAAAZgAAwAAAABAAAAl+ycndzdLempCkfqzPs6JAAAAAASAAACgAAAAEAAAAAwVXP3nwC4eYOserrpPUYAAAAfRNoo6crchVbD+SDmoY3lDnNcxp3m4XFAAAAC5jGcxzKHkl2juJxNdqFSeN

This code has random characters and random length.

In a script of mine I am using the following to load the contents in a variable:

for /f "delims=" %%a in (C:\link.txt) do set passwd=%%a

The problem is that it seems to load it fine but the program it is used for does not accept it. If I copy paste it manually it works.

There's some character in there that is causing issues and the whole string should be escaped.
Thank you

Offline

#2 21 Dec 2015 01:19

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Load and escape contents of a text file to a variable

Your question doesn't allow us to test or analyse the problem because there isn't enough information,
and it is unclear if the line you have posted is one that actually causes the problem.

We don't know how the variable is being used etc

Last edited by foxidrive (21 Dec 2015 01:21)

Offline

#3 21 Dec 2015 09:42

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Load and escape contents of a text file to a variable

AFAIK there's nothing in a BASE64 encoded string (as your sample appears to be) that will offend a "set var=string" type command, except that if you have an extremely long line (the max size of a variable is approx 8k).

You might want to try quote wrapping for safety:

for /f "delims=" %%a in (C:\link.txt) do set "passwd=%%a"

Please provide more info regarding what this means:

Alexander Ceed wrote:

The problem is that it seems to load it fine but the program it is used for does not accept it. If I copy paste it manually it works.

Specifically it would be helpful to see the full code you are running and any output messages produced when it is run.


cmd | *sh | ruby | chef

Offline

#4 21 Dec 2015 14:08

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Load and escape contents of a text file to a variable

This is part of the script that loads keepass on startup. Keepass can be launched using an encrypted BASE64 password in order to open a database automatically.

This works fine:

Keepass.exe C:\db\abc.db -keyfile:C:\db\abc.key -pw-enc:AQAANCMnd8BFdERjHoAwE/Cl+sBAAAAwnwgPPduaUC6ljynwOT1/gAAAAACAAAAAAZgAAwAAAABAAAAl+ycndzdLempCkfqzPs6JAAAAAASAAACgAAAAEAAAAAwVXP3nwC4eYOserrpPUYAAAAfRNoo6crchVbD+SDmoY3lDnNcxp3m4XFAAAAC5jGcxzKHkl2juJxNdqFSeN

Using it like this fails, which is what my code is right now:

set dbPasswordFilePath=%1
for /f "delims=" %%a in (%dbPasswordFilePath%) do set dbEncPassword=%%a

start "keepassLauncher" keepass.exe C:\db\abc.db -keyfile:C:\db\abc.key -pw-enc:%dbEncPassword%

I tried using it in double quotes but it doesn't work.
The error keepass outputs is incorrect password.

Last edited by Eehixohw (21 Dec 2015 14:09)

Offline

#5 21 Dec 2015 14:47

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Load and escape contents of a text file to a variable

I edited my typo below.

Give this a shot and compare the two files.  Initially, check the filesizes.

set dbPasswordFilePath=%1
for /f "delims=" %%a in (%dbPasswordFilePath%) do set dbEncPassword=%%a

>"file.txt" echo(%dbEncPassword%

It's possible that there is a leading or trailing space in the file too.

Last edited by foxidrive (21 Dec 2015 15:48)

Offline

#6 21 Dec 2015 14:56

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: Load and escape contents of a text file to a variable

Well, both examples are not comparable because you changed more than one thing. You should start duplicating in a Batch file the exact same command that works in the command-line; that is:

@echo off
rem This is a *Batch file*!

Keepass.exe C:\db\abc.db -keyfile:C:\db\abc.key -pw-enc:AQAANCMnd8BFdERjHoAwE/Cl+sBAAAAwnwgPPduaUC6ljynwOT1/gAAAAACAAAAAAZgAAwAAAABAAAAl+ycndzdLempCkfqzPs6JAAAAAASAAACgAAAAEAAAAAwVXP3nwC4eYOserrpPUYAAAAfRNoo6crchVbD+SDmoY3lDnNcxp3m4XFAAAAC5jGcxzKHkl2juJxNdqFSeN

Common sense indicate that previous Batch file should work... Then, you should test that the data read by the FOR /F command be the exact same password string:

@echo off
rem This is a *Batch file*!

set dbPasswordFilePath=%1
for /f "delims=" %%a in (%dbPasswordFilePath%) do set dbEncPassword=%%a

>> duplicatedPassword.txt echo %dbEncPassword%

Run previous Batch file and FC compare that both original and created password files be the same. If so, then test that the original password file have the exact same contents of the literal password used in first example above: copy the password string from the file, paste it in a third password file and compare it vs. the previous files.

If after these steps you still don't find the problem, remove the START command from your Batch file:

set dbPasswordFilePath=%1
for /f "delims=" %%a in (%dbPasswordFilePath%) do set dbEncPassword=%%a
keepass.exe C:\db\abc.db -keyfile:C:\db\abc.key -pw-enc:%dbEncPassword%

Please, post the result...

Offline

#7 21 Dec 2015 15:48

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Load and escape contents of a text file to a variable

As otherwise suggested, it sounds like something is affecting the item as read from your input file.

Assuming that there is ONLY the BASE64 string and nothing else apparently in the file, I'd expect the problem to lie somewhere among:
stray white space (or other non-printable character) at start or end of line
UNIX file format
no "end of line marker" on the last line.

Suggest following foxidrive/Aacini's steps to see where that gets you.

Last edited by bluesxman (21 Dec 2015 15:49)


cmd | *sh | ruby | chef

Offline

#8 21 Dec 2015 23:23

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Load and escape contents of a text file to a variable

What I know so far:
I copied the code from

echo %dbEncPassword%

and made a search in the original file, it matches.
I compared the file from

>"file.txt" echo(%dbEncPassword%

and it matches again.
I compared again with

>> duplicatedPassword.txt echo %dbEncPassword%

and matches perfectly.

With the output from the commands above, I tried to run it manually and they all worked:

Keepass.exe C:\db\abc.db -keyfile:C:\db\abc.key -pw-enc:AQAANCMnd8BFdERjHoAwE/Cl+sBAAAAwnwgPPduaUC6ljynwOT1/gAAAAACAAAAAAZgAAwAAAABAAAAl+ycndzdLempCkfqzPs6JAAAAAASAAACgAAAAEAAAAAwVXP3nwC4eYOserrpPUYAAAAfRNoo6crchVbD+SDmoY3lDnNcxp3m4XFAAAAC5jGcxzKHkl2juJxNdqFSeN

It does not use an EOL marker as I have looked at my othe, working password text files and they are only one BSE64 line, without a new line.

I have also tried with and without the START command.

I have checked the password file and script file and they all are using windows EOLs. Hidden characters reveal CRLF.

I just don't get it. Every echo command outputs the password perfectly.

Offline

#9 24 Dec 2015 12:14

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Load and escape contents of a text file to a variable

Did you use this to compare the files?

FC /b file1.txt file2.txt

Offline

Board footer

Powered by