Tip

Using 'functions' in batch files

    Requires Free Membership to View

More on Windows systems management

Learn how to trace open files on a shared network.

Have a Windows systems management tip? Submit it. You could receive an Amazon gift certificate.
Did you know that there are ways to use 'functions' in batch files? SearchWincomputing.com member Jim Ruby submitted this Windows systems management tip explaining how to do so.

It is possible to create function-like structures in WIN32 cmd files to break apart and simplify long batch files. One batch file can call another and use the returned value as a variable for further steps.

Granted, there are times when this method is overkill, but there are plenty of opportunities to use this technique. Believe it or not, you can actually create some structured programming in cmd files using this concept.

The three cmd files code examples below demonstrate this idea. The main batch file that will call and display the results of Function1.cmd and Function2.cmd is called Calc.cmd. Both "functions" return values to The Calc cmd file; Function1 returns one value while Function2 return two values.

TheCalc.cmd
-----------

@Echo off
FOR /F %%f in ('Function1') do SET TheAnswer=%%f ECHO.
ECHO.
ECHO.
ECHO The Answer is: %TheAnswer%
ECHO.
ECHO.
ECHO.


FOR /F "Tokens=1-2 delims= " %%g in ('Function2') DO (SET TheAnswer1=%%g & SET TheAnswer2=%%h)
ECHO.
ECHO.
ECHO.
ECHO The 1st Answer is: %TheAnswer1%
ECHO The 2nd Answer is: %TheAnswer2%M
ECHO.
ECHO.
ECHO.


Function1.cmd

-------------
SET /A Function1=2+4
ECHO %Function1%


Function2.cmd

-------------
SET /A Function1=2+4
SET /A FirstAnswer=1+1
SET /A SecondAnswer=2+2
ECHO %FirstAnswer% %SecondAnswer%

Rating this tip below will let us know how useful it was to you. And if you have a Windows tip, timesaver or workaround to share, submit it to our tip contest. You could win the prize for that contest.

This was first published in March 2007

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.