1

In Windows Command Line I normally write empty line in a file with

echo; >> file

However, what I have now is a variable

$param1%

If I want echo to write it in the file I have to do

echo %param1% >> file

HERE IS WHERE THE PROBLEM START :

If I'd like an empty like I'd make

set param1=;

However since the ; is not in contact with the echo word the command is

echo ; >> file

which write the ; in the file...

I need the variable to sometime contains text, and sometime nothing. How can I do it?

1
  • if "%param1%"=="" echo;>>file else echo %param1%>>file Commented Jul 10, 2015 at 14:07

2 Answers 2

1

if "%param1%"=="" echo;>>file else echo %param1%>>file

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

Comments

1

If a param1 variable does not exist (the same as set "param1="), then %param1% results to:

  • In a .bat script: %param1% results to an empty string (a string of zero length);
  • In a CLI window: %param1% results to the %param1% string.

In a .bat script use (note no spaces surrounding %param1%)

>> file (echo;%param1%)

In a CLI window use

>>file (if not defined param1 (echo;) else echo;%param1%)

Note proper using of parentheses in if-else! For instance, check interesting result of next command:

if  ""=="" echo;"THEN branch">>file else echo;"ELSE branch">>file

Output:

==>if  ""=="" echo;"THEN branch">>file else echo;"ELSE branch">>file

==>type file
"THEN branch" else echo;"ELSE branch"

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.