Home > Windows Server Tips > Windows Systems and Network Administration > Using 'functions' in batch files
Windows Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

WINDOWS SYSTEMS AND NETWORK ADMINISTRATION

Using 'functions' in batch files


Jim Ruby
03.13.2007
Rating: -2.40- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.

Rate this Tip
To rate tips, you must be a member of SearchWindowsServer.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Windows Systems and Network Administration
Troubleshooting Windows application crashes or hangs
Converting VMware ESX machines to Hyper-V format
Using DFSR for SYSVOL replication in Windows Server 2008
Top 25 Windows PowerShell commands for administrators
Key DFS improvements in Windows Server 2008 R2
Free Windows security tools every admin must have
Group Policy makes strides in Windows Server 2008 R2
Quick tips for troubleshooting NTFS permissions
Common causes of Windows server security vulnerabilities
Cutting the cost of Windows identity and access management

Windows File Management
Using DFSR for SYSVOL replication in Windows Server 2008
Key DFS improvements in Windows Server 2008 R2
Quick tips for troubleshooting NTFS permissions
Using NTFS on a non-Windows OS with NTFS-3G
File classification the automated way with Windows Server 2008 R2
Using DFS to create file system virtualization in Windows Server 2008
File server migration tips for Windows Server 2008
Windows Distributed File System (DFS) Tutorial
Planning a file server migration to Windows 2008
Windows Distributed File System (DFS) Namespace primer
Windows File Management Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
NTFS  (SearchWindowsServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



Server Room Design - Planning, Cooling, Maintenance
HomeTopicsBlogsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2004 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts