1

I am writing a batch file that will generate/write to a property file based on multiple user input values. However, it is not recording the values of input. The result looks like

prop1=
prop2=

I wonder if there's something I need to know with set that is preventing this from working.

Weird part is that if I run this particular script multiple times, the value output from echo seems to be always be the user input from last time.

Code:

@echo off

IF NOT EXIST data_file (
set /p prop1=Enter value: 
set /p prop2=Enter value: 
(echo prop1=%prop1%) > data_file 
(echo prop2=%prop2%) >> data_file 
) 
1
  • why are you using agrupations with the two echo commands? isn't necessary, see my answer, thankyou for read. Commented Dec 3, 2012 at 23:32

2 Answers 2

3

Classic problem for inexperienced batchers :)

%prop1% is expanded when the line is parsed. Your problem is that everthing within parentheses is parsed in one pass. So the value you see is the value that existed before you entered the IF statement.

You have two simple solutions.

1) Eliminate the enclosing parens by reversing the logic and using GOTO

@echo off
IF EXIST file goto skip
set /p prop1=Enter value: 
set /p prop2=Enter value: 
(echo prop1=%prop1%) >file 
(echo prop2=%prop2%) >>file 
:skip

2) Use delayed expansion - that takes place just before each line within the parens is executed

@echo off
setlocal enableDelayedExpansion
IF NOT EXIST file (
  set /p prop1=Enter value: 
  set /p prop2=Enter value: 
  (echo prop1=!prop1!)>file
  (echo prop2=!prop2!)>>file
)
Sign up to request clarification or add additional context in comments.

5 Comments

didn't know goto makes quite a big difference here. What does setlocal do?
tried the first solution, the second solution didn't work. If i call this batch script file from another file, it won't do anything after output to the file for some reason. For example, if this is script.bat, we have a file.bat that contains "call script.bat" and a bunch of batch script, it will only call script.bat and stop there. I have to call file.bat again in order to run the rest of bat commands....why is that?
@ttback - The primary function of SETLOCAL is to localize changes to environment variables. The old values will be restored upon issuing ENDLOCAL. But in this case it is being used to gain access to delayed expansion functionality. Type HELP SETLOCAL from a command prompt to get more info. For more info about the problems of normal expansion within loops, type HELP SET and read the section that starts with "Finally, support for delayed environment variable expansion has been added..." (about 1/2 way down)
@ttback - Oops, I had a stupid bug with my redirection in my 2nd option. It has been fixed. Regarding wny your script is not continuing, I can't tell why without seeing your code. Perhaps as a new question?
will have to add a new question tomorrow. Quite worn out at office now. Heading out. Thanks a ton for the help though.
0

You need to expand the variables using SETLOCAL ENABLEDELAYEDEXPANSION or use CALL.

@echo off

IF NOT EXIST data_file (
    set /p prop1=Enter value: 
    set /p prop2=Enter value:

    (
    Call echo prop1=%%prop1%%
    Call echo prop2=%%prop2%%
    ) > data_file 

)

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.