2

I have a long test.properties file that contains the following at the very top:

property1=cheese
property2=apple
property3=bread

# many more properties

I can read those properties from the command line by changing the working directory to the one containing test.properties and running the following command:

> FOR /F "tokens=1,2 delims==" %A IN (test.properties) DO
    IF "%A"=="property1" SET firstitem=%B
> FOR /F "tokens=1,2 delims==" %A IN (test.properties) DO
    IF "%A"=="property2" SET seconditem=%B

> echo %firstitem%
cheese
> echo %seconditem%
apple

However, when I try to put this code in a batch file stored in the same directory, it fails and I cannot work out why:

FOR /F "tokens=1,2 delims==" %A IN ("%~dp0\test.properties") DO 
    (IF "%A"=="property1" SET firstitem=%B)
FOR /F "tokens=1,2 delims==" %A IN ("%~dp0\test.properties") DO
    (IF "%A"=="property2" SET seconditem=%B)

Running the batch file from the command line gives me this:

> "C:\folder\testbatch.bat"
~dp0\test.properties") DO IF "B was unexpected at this time.

What can I do to read the properties using the batch file, so that they are stored in variables that can be used in the rest of the script?

EDIT: Problem solved; working code below.

FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%~dp0\test.properties") DO 
    (IF "%%A"=="property1" SET firstitem=%%B)
FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%~dp0\test.properties") DO
    (IF "%%A"=="property2" SET seconditem=%%B)

2 Answers 2

1

There's two problems here:

  1. In the batch file version you need two percent signs for the iterator variables.
  2. In the batch file version you erroneously put quotes around the file name, causing the string itself to be tokenized, not the contents of the file specified by the string. Update: @Stephan correctly noted the use of the usebackq modifier for a more general solution. But since you're talking about "batch file stored in the same directory", you might as well drop the path prefix %~dp0 altogether.

Corrected version:

@ECHO OFF
FOR /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
    IF "%%A"=="property1" SET firstitem=%%B 
)
FOR /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
    IF "%%A"=="property2" SET seconditem=%%B
)
ECHO %firstitem%
ECHO %seconditem%

Returns:

cheese
apple
Sign up to request clarification or add additional context in comments.

5 Comments

Accpeted this answer as it very nearly solved my problem. I still had to use usebackq to get it to work correctly; what is written above gives me no output for some reason.
why reading the file over and over again? (" # many more properties "). Put all the if's into one for-statement.
@Stephan: You're absolutely right, of course, but I wanted to stick to the structure OP had given.
@Richard: Are you running this from Explorer via "Run as Administrator"? If so, the current directory is changed to %SystemRoot%\system32 and it's very likely that test.properties does not reside there. That'd explain why you need the %~dp0 bit. See here for an alternative solution.
It would indeed be more efficient to read the file just once, rather than once for each property you want to read. I just couldn't get the syntax for that to work (reasonably sure logic was correct), and this method still only takes fractions of a second.
1

"What can I do to read the properties using the batch file, so that they are stored in variables that can be used in the rest of the script?"

That is surprisingly easy in your case if you are willing to use the strings from the file:

FOR /F "usebackq tokens=*" %%A IN ("%~dp0\test.properties") DO set %%A
echo property2 is %property2%

Note: in case, your path or filename contains spaces, you need the quotes. Tell for not to treat it as a string with usebackq

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.