Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a batch file that creates a text file with its output like the following:

RESULT=0&SECURETOKEN=SmH3kciXiVkiwD0i70CwKYgAR&SECURETOKENID=2013031414280240&RESPMSG=Approved

The length of the variables is constant, so it is easy to parse the value from the string. Delimits for all name-value pairs are ampersands.

Then, from within the first batch file, I need to call another batch file and use two variable values from the text file content:

SECURETOKEN

and

SECURETOKENID

Using the following part in the second batch does not help, where Temp_result.txt is the file that contains the output.

set /p out=<{full path to the file}\Temp_result.txt
set SECURETOKEN=%out~21,25%
set SECURETOKENID=%out~61,16%

Can anybody help me out, please?

share|improve this question

2 Answers 2

You are missing the : after your variable name. So your commands would be:

set /p out=<{full path to the file}\Temp_result.txt
set SECURETOKEN=%out:~21,25%
set SECURETOKENID=%out:~61,16%
share|improve this answer
    
Thank you very much! I just overlooked the colon :-) –  IllyaIll Mar 15 '13 at 0:42
    
these are working perfectly. Thank you very much. I overlooked the colon set /p out=<{full path to the file}\Temp_result.txt set SECURETOKEN=%out:~21,25% set SECURETOKENID=%out:~61,16% –  IllyaIll Mar 15 '13 at 0:42

It looks like you have a perfectly fine solution with the user1936123 answer. Here is a completely different approach that will work even if the value lengths are not constant.

@echo off
setlocal
for /f "usebackq tokens=2,3 delims=&" %%A in ("{fullFilePath}\Temp_result.txt") do (
  set "%%A"
  set "%%B"
)

If you are willing to set a variable for all of the name value pairs, not just the 2nd and 3rd ones, then:

set /p "ln=" <"{fullFilePath}\Temp_result.txt"
set "%ln:&="&set "%"

It is not obvious how the above works. It uses search and replace to transform the line into the following series of SET statements, that are then executed:

set "RESULT=0"&set "SECURETOKEN=SmH3kciXiVkiwD0i70CwKYgAR"&set "SECURETOKENID=2013031414280240"&set "RESPMSG=Approved"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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