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   


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
-


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


RELATED CONTENT
Windows Systems and Network Administration
Cutting the cost of Windows identity and access management
Using NTFS on a non-Windows OS with NTFS-3G
Group Policy Object modeling simplifies network security
Implementing simple Network Access Protection for Windows Server 2008
Immediate steps for Windows disaster recovery
Tips for Windows domain controller optimization
Quick hits: Troubleshooting service account failure, batch job execution
Case Study: Troubleshooting Windows service dependency failures
Troubleshooting common Windows service failures
Reducing the size of network backups in Windows

Windows File Management
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
Self-healing NTFS keeps admins one step ahead of data corruption
Windows NTFS Tutorial
Windows registry hack improves offline file access for mobile users
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


----------

@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




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