|
![]() |
|
|||||||
![]() |
ICT-Hotlist TopicCall :Batch_subroutineThe next example shows a batch file that uses a subroutine, creates 3 batches and starts them. Every batch file has a separate log files. Below the code is an example of the output.This example also shows how batch control characters can be escaped in a batch with the "^" character.
@Echo off
Rem =============================================================== Rem | This batchfile creates 3 batch files that do logging. Rem | The batch files run simultanous and have separate log Rem | files. Batches are created and run and log in current Rem | folder. Rem | Works for Windows XP, 2003, 2008 server and Windows 7, 8, 10 Rem | (C) Copyright 2009 - 2025 J.P.G. van Soest (www.vansoest.it) Rem =============================================================== Rem *************************************************************** Rem * Main Begin Rem *************************************************************** Call :FunctionCreateLoggedBatch Batch1 Call :FunctionCreateLoggedBatch Batch2 Call :FunctionCreateLoggedBatch Batch3 GoTo :End Rem *************************************************************** Rem * Subroutines Begin Rem *************************************************************** :FunctionCreateLoggedBatch Rem *************************************************************** Rem * FunctionCreateLoggedBatch (BatchName) Rem *************************************************************** If Exist %1.bat Del %1.bat Set LogFile=%1_LogFile.txt If Exist %LogFile% Del %LogFile% Echo @Echo Off >> %1.bat Echo Echo This is logged output from %1.bat ^>^>%LogFile% >> %1.bat Echo Echo **************************** >>%1.bat Echo Echo * This is batch %1.bat * >> %1.bat Echo Echo **************************** >>%1.bat Echo Pause >>%1.bat Echo Exit >> %1.bat Start %1.bat GoTo :Eof Rem *************************************************************** Rem * Subroutines End Rem *************************************************************** :End Rem *************************************************************** Rem * Main End Rem *************************************************************** ![]() Output of the running batches
You may vote your opinion about this article:
![]() ![]() ![]() ![]() ![]() Scripts and programming examples disclaimerUnless stated otherwise, the script sources and programming examples provided are copyrighted freeware. You may modify them, as long as a reference to the original code and hyperlink to the source page is included in the modified code and documentation. However, it is not allowed to publish (copies of) scripts and programming examples on your own site, blog, vlog, or distribute them on paper or any other medium, without prior written consent.Many of the techniques used in these scripts, including but not limited to modifying the registry or system files and settings, impose a risk of rendering the Operating System inoperable and loss of data. Make sure you have verified full backups and the associated restore software available before running any script or programming example. Use these scripts and programming examples entirely at your own risk. All liability claims against the author in relation to material or non-material losses caused by the use, misuse or non-use of the information provided, or the use of incorrect or incomplete information, are excluded. All content is subject to change and provided without obligation. |