Popular Batch recipes http://code.activestate.com/recipes/langs/batch/popular/2017-04-28T08:22:08-07:00ActiveState Code RecipesCount 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> 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> 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> 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> 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> 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> 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> Permutations by recursive Heap's method (Batch) 2016-06-11T08:42:41-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580678-permutations-by-recursive-heaps-method/ <p style="color: grey"> Batch recipe 580678 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/permutation/">permutation</a>, <a href="/recipes/tags/recursion/">recursion</a>). </p> <p>Setlocal / endlocal allow for recursion in batch. Values can be passed by value and by reference, the later can return a value to the caller.</p> Last sunday of each month for a given year (Batch) 2016-06-07T20:25:00-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580677-last-sunday-of-each-month-for-a-given-year/ <p style="color: grey"> Batch recipe 580677 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/calendar/">calendar</a>). Revision 2. </p> <p>Includes batch routines for day of week, is leap year and last dat of the month</p> Timing a batch program (Batch) 2016-05-29T10:50:33-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580670-timing-a-batch-program/ <p style="color: grey"> Batch recipe 580670 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/timing/">timing</a>). </p> <p>It uses %time% to time a function to the hundredth of second.</p> Batch priority queue (Batch) 2016-05-26T11:00:21-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580666-batch-priority-queue/ <p style="color: grey"> Batch recipe 580666 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/batch/">batch</a>). </p> <p>The fact the environment sorts itself by key allows us to implement a PQ using the prefix "queu" added to the priority value as a key</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> Unix head -n analog (Batch) 2013-10-12T17:40:06-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578688-unix-head-n-analog/ <p style="color: grey"> Batch recipe 578688 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/head/">head</a>). </p> <p>Reads first N strings in text file. It looks like <a href="http://code.activestate.com/recipes/578311-unix-tail-n-analog/">tail -n</a></p> Download file (Batch) 2013-10-10T16:31:50-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578680-download-file/ <p style="color: grey"> Batch recipe 578680 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/download/">download</a>). </p> <p>Download file from internet without third party tools.</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> 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 &lt;code&gt;&lt;nul set /p "str=[string]"&lt;/code&gt; construction which is equal print function in C language. OK, next batch file print multiplication table.</p> Search engine (Batch) 2013-10-10T16:18:09-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578646-search-engine/ <p style="color: grey"> Batch recipe 578646 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/search/">search</a>). Revision 2. </p> <p>Quick search on files index embedded directly in the command script. For example: "search.cmd exe$" - looks for all EXE files in index or just "search.cmd blahblahblah" - looks for all matches with "blahblahblah" in paths. To remove index from script use "search.cmd /fix:me" command (without quotes).</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> 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>