0

To chcek if file is loaded or not, I load its contents using:

set /p filevar=file.txt

and check if var is empty:

if "%filevar%"="" exit

When script chceks file with multiple lines, execution of script stops, so i suppose that chcecking fails. Why script fails? How to perform such check properely?

1
  • set /p filevar=file.txt does not load the contents of file.txt into variable filevar. It writes the string "file.txt" to the screen and waits for a single line of user input. Commented Mar 24, 2015 at 12:12

1 Answer 1

2

Firstly, you've got the syntax of your set /p command messed up. As it is, it will prompt the user with the text "file.txt". I think what you mean is

set /p "filevar=" <file.txt

You should also use the /b switch with exit to prevent the console window from closing if your script is run from the command line.

But yeah, as jeb states, to check whether left equals right, either use == or equ. Or, as dbenham reminds me, if you're checking for an empty value, you can also use if not defined.

if "%filevar%"=="" exit /b
if "%filevar%" equ "" exit /b
if not defined filevar exit /b

All three statements will have the same result1.

In a console window, enter help if for more information.


1 It's worth mentioning that, while those three if statements have the same result outside of a parenthetical code block, only the third one will work reliably within parentheses (such as in a for loop). The first two would need delayed expansion to work properly if used within the same code block as %filevar% is set.

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

1 Comment

Or simply check if variable is defined using if not defined filevar exit /b

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.