Enable Pipelines in Windows Batch Programming


Previously, I have programmed some scripts in Windows Batch, and these can be found in the github. For example, the rot13.bat rotates given input string (command line parameters) to 13 places. The reverse.bat reverses the given text. If you want to connect the commands using pipelines i.e. a pipe in command connects the output of one command and make it the input of the another command. This is useful if you want to combine several commands. In this case, for example, if you want to rotate a text first and reverse it next. You probably want to have something like this.

rotate Some Text | reverse

However, the previous versions of scripts do not support this, and it will complain ‘The process tried to write to a nonexistent pipe‘ because the pipe is not existent. The workaround is to add the support of reading from stdin in the batch script. In this case, the script is capable of handling the command-line parameters and the stdin. In the latter case, where the command-line parameters are not available, the script will wait for the readin until a empty string is entered.

These scripts have been added a pre-processing header in the front of the scripts that handle the stdin as well as the command-line parameters. For example, the reverse.bat is improved to the following.

@echo off
:: reverse.bat
:: [email protected]
:: https://helloacm.com

setlocal enableextensions enabledelayedexpansion 

:begin
	if "%1"=="-h" goto help
	if "%1"=="" goto readin
	goto start

:readin
	(set /p _s=) && (
		call :start !_s!
		goto readin	
	) || (
		goto end
	)

:help
	echo Usage: %0 text
	shift
	goto begin

goto end

:start
	set _len=0
	set _str=%*

	:: Get the length of the sentence
	set _subs=%_str%

:loop
	if not defined _subs goto result

	::remove the first char
	set _subs=%_subs:~1%
	set /a _len+=1
	goto loop	
      
:result
	set /a _len-=1
	for /l %%g in (0,1,%_len%) do (
		call :build %%g
	)

	echo %s%
	
	goto end

:build
	:: get the next character
	call set _digit=%%_str:~%1,1%%%
	set s=%_digit%%s%

:end
endlocal

The script will first check if a help switch -h is turned on, if yes, it prints the message and shift parameters one position to the left and goto the begining. If there are no command-line parameters, it jumps to the section that waits for the input using set /p that further will end the script if the user directly puts a return. Otherwise, the parameters will be processed separately line by line (as entered by user), by calling the :start label.

The support of the stdin input will enable the pipelines and thus will allow the following command to work.

mode con cols=80 & cls & echo C:\^>cls ^& echo. ^& color 0E ^& rot13 .gfrO ruG fv tanS vhU ^| reverse ^| print & echo. & color 0E & rot13 .gfrO ruG fv tanS vhU | reverse | print

and the output is like this.

hf Enable Pipelines in Windows Batch Programming batch script beginner DOS I/O File implementation programming languages string tricks windows windows command shell

(PS: I promise my wife Hui Fang this image).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
590 words
Last Post: Rot13 and Rot5 implementation using Windows Batch
Next Post: Remainder Computation (Modulo) on Floating Numbers, Delphi and Python

The Permanent URL is: Enable Pipelines in Windows Batch Programming

Leave a Reply