How To Write Batch Files with Conditional Execution Statements using Notepad++

in #utopian-io8 years ago (edited)

What Will I Learn?

  • Batch File Programming IF Commands
  • Writing Batch Files with Conditional Execution Statements

Requirements

  • Notepad ++

Difficulty

  • Basic

Tutorial Contents

  • Testing for (Non) Existence of Files
  • Checking Return Codes from Applications
  • String Comparisons

How To Write Batch Files with Conditional Execution Statements using Notepad++

A guide to using IF to test for conditions in batch file programming for backup batch file scripting, rename batch file programming.

Batch file programming is a very useful way to automate small, repetitive, tasks. The Windows Command Line Programming tutorial is a good starting place to read about the basics of batch file programming, for those who are new to creating batch files.

It is also a good idea to have a debugger handy, such as the Running Steps batch file IDE with integrated debugger.

One of the batch file commands that is very useful is the IF command, which is used to provide conditional execution. In a batch file, this means that it is possible to selectively execute a set of commands based on the evaluation of a specific condition.

If that condition evaluates to true, then the commands are executed. If not, then they are not. There are 3 possible tests:

  • File existence
  • Errorlevel : return code from an application
  • String comparison

This tutorial looks at examples of each.

Testing for (Non) Existence of Files

The first variation is designed to allow the batch file script to test for the presence of a file or directory in the current tree. For example:

IF EXIST Desktop ECHO In user folder.

The reader will note from the above that there is no 'THEN' as part of the IF clause. The commands have been placed in CAPS in order to distinguish them from other parts of the batch file command.

One use for the EXIST command is in testing for the success of a compilation of some source code. For example:

echo off
bcc32 %1.c > err.txt
IF NOT EXIST %1.exe (notepad err.txt) ELSE %1



The above example first turns off screen echo, then tries to compile the application whose name is supplied on the command line. It is assumed that the name is supplied without an extension, and that there is only one source code file. If more than one source code file is required, then an appropriate MAKE tool should be used instead. The remainder of the batch file scripting can remain the same - the bcc32 command just needs to be replaced with the MAKE command used by the system.

The result of the command is then redirected to the err.txt file. The IF line checks to see if the application file exists, and if not, then assumes that the compile has failed, and opens the err.txt file with the Notepad editor. The command must be in parentheses as it contains more than one item. If the application file exists (the ELSE clause), it is executed.

Checking Return Codes from Applications

Since bcc32 returns 1 if there have been errors in the compile or link process, an alternative to the above could be just to test the return code using the errorlevel variable. This gives rise to the following batch file:

echo off
bcc32 %1.c > err.txt
IF %errorlevel% 1 (notepad err.txt) ELSE %1



In the above example, the clause evaluates to true and executes Notepad++ if the errorlevel variable is greater than or equal to the number specified. Otherwise the behavior of the clause is the same as previously.

String Comparisons

Finally, it is possible to test strings for equality. This means that batch files can be created which allow the user to specify options on the command line. For example, in the next batch file sample, the user can specify a different editor on the command line, or use the default:

echo off
bcc32 %1.c > err.txt
GOTO result%errorlevel%
:result1
IF %2==default (notepad.exe err.txt) ELSE (%2 err.txt)
GOTO end
:result0
%1
:end



To invoke an editor other than the default, the user can specify the name as the second batch file argument:

compile HelloWorld "C:\Program Files\Windows\Accessories\wordpad.exe"

The IF ... GOTO command mechanism could also be adapted to test for blank arguments in the second parameter, as an enhancement.

Testing can be conducted with the running steps batch file debugging environment.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @carver I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.13
TRX 0.33
JST 0.034
BTC 110716.57
ETH 4295.49
USDT 1.00
SBD 0.82