Most viewed Batch recipes http://code.activestate.com/recipes/langs/batch/views/2017-04-28T08:22:08-07:00ActiveState Code RecipesCreating array into batch (Batch)
2012-11-01T18:35:05-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578314-creating-array-into-batch/
<p style="color: grey">
Batch
recipe 578314
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/array/">array</a>).
Revision 2.
</p>
<p>Command language supports creation array. For example:</p>
Batch Local Network Messaging System (Batch)
2012-02-03T01:41:41-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/578028-batch-local-network-messaging-system/
<p style="color: grey">
Batch
recipe 578028
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/alex/">alex</a>, <a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/batch_file/">batch_file</a>, <a href="/recipes/tags/dos/">dos</a>, <a href="/recipes/tags/messaging/">messaging</a>, <a href="/recipes/tags/messenger/">messenger</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/wallar/">wallar</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/wired/">wired</a>, <a href="/recipes/tags/wired_network/">wired_network</a>).
Revision 3.
</p>
<p>This is a Batch Local Messaging System that can be used to send messages between computers that share the same file system.</p>
<p>There are three files in the code below. They are separated with dashed lines. There is a comment on the top of each segment of code indicating the file name that the code needs to be saved as. They need to be saved as three separate batch files in the same folder. </p>
<p>The first file, MessengerMain.bat will ask you for your name and then an address. The name should be whatever you wish your screen name to be. The address needs to be the path of the FOLDER that is shared between you and the other computer that you are communicating with. Once these pieces of information are entered, two screens will appear. The first is the Sender screen and the other is the Receiver screen. </p>
<p>How does it work?
The MessengerMain.bat file will create two text files, Name.txt and Address.txt, that will hold your user name and the common folder address. It will then use the START command to run Sender.bat and Receiver.bat. Sender.bat will create a text file named msgtext.txt in the address folder you specified in MessengerMain.bat. If there is already a file of this sort in existence in the address folder, it will overwrite it. Once this file is created, any message you enter will be saved in the msgtext.txt file after the name you entered (%name%: %msg%). Sender.bat the CLS (clears) the screen and asks you for another message continuously. Receiver.bat is very similar. Receiver.bat, first off, reads the Address.txt file and the Name.txt file to find your information given in MessengerMain.bat just like Sender.bat does. It will count the number of lines in the msgtext.txt file and will clear the whole screen and print out the whole contents of the file if the number of lines in msgtext.txt increases. So basically you are writing to a shared file and reading from a shared file. </p>
<p>Example:</p>
<p>I have a folder called MSGFolder located in the folder as I have my MessengerMain.bat, Receiver.bat, and Sender.bat files. I then run MessengerMain.bat by double clicking on the icon. I enter Alex for my Name and MSGFolder as the Address. I then double click MessengerMain.bat again and enter the name Bob for Name and MSGFolder for Address. Once you do this you can type in either of the Sender.bat interfaces and it will show up as if you are talking. </p>
File selector dialog in batch (Batch)
2017-03-27T13:20:41-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580665-file-selector-dialog-in-batch/
<p style="color: grey">
Batch
recipe 580665
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/directory/">directory</a>).
Revision 2.
</p>
<p>Runs mshta hidden to open a file dialog and get the path of the file selected. No parameters can be passed to the dialog.
EDITED: I did a subroutine with the snippet</p>
Publish a Windows Process List to PDF with xtopdf (Batch)
2015-12-27T20:45:32-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579142-publish-a-windows-process-list-to-pdf-with-xtopdf/
<p style="color: grey">
Batch
recipe 579142
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/processes/">processes</a>, <a href="/recipes/tags/process_management/">process_management</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how you can generate a Windows process list or task list (basically, a list of running processes, with some information about each of them), to a PDF file, using the Windows TASKLIST command along with the xtopdf toolkit. The list is sorted in ascending order of memory usage of the processes, before writing it to PDF.</p>
<p>It differs somewhat from other xtopdf recipes, in that no additional code needs to be written, over and above what is already in the xtopdf package. We just have to use the needed commands there, in a series of commands or a pipeline.</p>
<p>However, one can still write additional code, by modifying the program used (StdinToPDF.py), if needed, to customize the PDF output.</p>
Formating strings (print a table) (Batch)
2013-06-18T07:52:03-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578562-formating-strings-print-a-table/
<p style="color: grey">
Batch
recipe 578562
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/table/">table</a>).
</p>
<p>As you know there are no escape characters such as "\t" in the windows command language but it does not mean that we can not format text. Command prompt has its own tricks. At firstly, you need declare enabledelayedexpansion after setlocal command in your batch file to get access for some interesting things; secondly, use <code><nul set /p "str=[string]"</code> construction which is equal print function in C language. OK, next batch file print multiplication table.</p>
Fibonacci numbers (Batch)
2012-11-14T19:03:11-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578326-fibonacci-numbers/
<p style="color: grey">
Batch
recipe 578326
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/fibonacci/">fibonacci</a>).
Revision 2.
</p>
<p>Using example: fibonacci.cmd 9 (returns 55).</p>
Count lines and words in a text file (Batch)
2017-04-28T08:22:08-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580791-count-lines-and-words-in-a-text-file/
<p style="color: grey">
Batch
recipe 580791
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/word_count/">word_count</a>).
Revision 2.
</p>
<p>Returns number of lines and words (separed by spaces) of a text file dragged to the script. Does'nt count blank lines. </p>
Send and retrieve text to/from clipboard (Batch)
2016-05-27T09:56:56-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580669-send-and-retrieve-text-tofrom-clipboard/
<p style="color: grey">
Batch
recipe 580669
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/clipboard/">clipboard</a>).
</p>
<p>Sending uses the command line tool clip.exe that may not be available in Windows XP. There is no standard tool to retrieve from clipboard so I use javascript via mshta.</p>
Add days to a calendar date- Julian date (Batch)
2016-08-17T10:32:03-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580683-add-days-to-a-calendar-date-julian-date/
<p style="color: grey">
Batch
recipe 580683
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/calendar/">calendar</a>).
</p>
<p>The trick to add days to a date is to convert the date to a serial, do a normal addition then convert the result back to a date. The serial I use here is the Julian day number used by the astronomers because its conversion formulas arewell documented. If you check the julian day number on internet the result can vary by a day, because julian days start at noon and i'm not making this distinction. </p>
A SOX 1KHz Sinewave Generator Using A Windows, (TM), Batch File... (Batch)
2013-05-02T17:48:18-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578516-a-sox-1khz-sinewave-generator-using-a-windows-tm-b/
<p style="color: grey">
Batch
recipe 578516
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/sound_exchange/">sound_exchange</a>, <a href="/recipes/tags/sox/">sox</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>32 bit Windows to Windows 7...</p>
<p>This batch file generates a 65536 byte binary file to give 8 seconds of pure sinewave at the earphone/speaker output(s)...</p>
<p>It uses ONLY a default Windows 32 bit installation, to Windows 7, except for the installation of SOX...</p>
<p>Obtain SOX from here:-</p>
<p><a href="http://sox.sourceforge.net/" rel="nofollow">http://sox.sourceforge.net/</a></p>
<p>Notepad was the _main_ editor... ;o)</p>
<p>Hopefully the code section does NOT corrupt the binary part which is 8 bytes in size.
If it does then let me know on here and I will post a pointer from where to grab it...</p>
<p>Enjoy...</p>
Invoking PowerShell script from batch file (Batch)
2012-11-01T18:39:55-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578316-invoking-powershell-script-from-batch-file/
<p style="color: grey">
Batch
recipe 578316
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/powershell/">powershell</a>).
</p>
<p>I'm not sure that there is a way do it without temporary files and why it need at last, maybe for tuning PowerShell host at start? This sample demonstrates how to launch PowerShell host inside CommandPrompt session and change it caption on clock. (Note: be sure that you have enough rights to execute PowerShell scripts - Get-ExecutionPolicy).</p>
String length (Batch)
2013-10-10T16:27:48-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578664-string-length/
<p style="color: grey">
Batch
recipe 578664
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/length/">length</a>, <a href="/recipes/tags/string/">string</a>).
Revision 2.
</p>
<p>A way for checking string length.</p>
Hex2Dec (Batch)
2013-02-21T13:25:23-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578309-hex2dec/
<p style="color: grey">
Batch
recipe 578309
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/hex2dec/">hex2dec</a>).
Revision 7.
</p>
<p>Many must be familiar with the excellent utility by Mark Russinovich called hex2dec. It is good to all, but I would like to add interactivity to it. So I wrote a "clone" on command language.</p>
Executing VBScript without creation of it (Batch)
2013-10-10T16:20:45-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578329-executing-vbscript-without-creation-of-it/
<p style="color: grey">
Batch
recipe 578329
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/cscript/">cscript</a>).
Revision 2.
</p>
<p>Have you a wish to launch VBScript without temporary files and right from batch? Maybe you think that it's not possible, but you think wrong. Do you remember my <a href="http://code.activestate.com/recipes/578315-create-cab-or-zip-with-batch/?in=lang-batch">post</a> for executing JScript from batch? So, maybe you are familiar with 'MSScriptControl' object which helps to execute JScript code in VBScripts or conversely - VBScript code inside JScripts. Let's take a look.</p>
Current month calendar (Batch)
2013-10-11T16:11:41-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578687-current-month-calendar/
<p style="color: grey">
Batch
recipe 578687
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/calendar/">calendar</a>).
</p>
<p>Prints calendar at current month.</p>
Split string sample (Batch)
2013-08-13T11:29:44-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578638-split-string-sample/
<p style="color: grey">
Batch
recipe 578638
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Target: we have a task to split the string ("this is a string") on the characters. Possible solution:</p>
Get text length - Column left center and right alignement (Batch)
2016-06-15T06:42:00-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580682-get-text-length-column-left-center-and-right-align/
<p style="color: grey">
Batch
recipe 580682
by <a href="/recipes/users/4182514/">Antoni Gual</a>
.
</p>
<p>The recipe parses some lines of text (separator=$), calculates the width of each column and prints the text in columns with lefr, right and center alignment. I have put the strlen and the alignment routines in separate procedures, to ease the reuse. The alignement procedures work in place, the result is returned in the same variable as the original string.</p>
ASCII art Sphere with integer square root (Batch)
2016-06-06T10:09:38-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580673-ascii-art-sphere-with-integer-square-root/
<p style="color: grey">
Batch
recipe 580673
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/graphics/">graphics</a>).
Revision 2.
</p>
<p>The code draws a sphere in ASCII Art. Square root routine bt Aacini</p>
Batch prime generator (Batch)
2017-04-25T11:44:27-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580789-batch-prime-generator/
<p style="color: grey">
Batch
recipe 580789
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/primes/">primes</a>).
Revision 3.
</p>
<p>Here is a radically differnt approach to generating primes in pure batch that overperforms everything else I have found . The idea comes from an exercise in Knuth's TAOCP Vol 3 page 617.</p>
Bash style commands (Batch)
2013-08-22T13:06:33-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578649-bash-style-commands/
<p style="color: grey">
Batch
recipe 578649
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/cmd/">cmd</a>).
</p>
<p>This is just a concept. To import bash style commands use "script.cmd /bash", to get the list of imported commands use "script.cmd /map"</p>