Simple xargs Batch Implementation for Windows


xargs is a Linux utility that allows you to build command from the standard input.

xargs –help
Usage: xargs [OPTION]… COMMAND [INITIAL-ARGS]…
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.

Mandatory and optional arguments to long options are also
mandatory or optional for the corresponding short option.
-0, –null items are separated by a null, not whitespace;
disables quote and backslash processing and
logical EOF processing
-a, –arg-file=FILE read arguments from FILE, not standard input
-d, –delimiter=CHARACTER items in input stream are separated by CHARACTER,
not by whitespace; disables quote and backslash
processing and logical EOF processing
-E END set logical EOF string; if END occurs as a line
of input, the rest of the input is ignored
(ignored if -0 or -d was specified)
-e, –eof[=END] equivalent to -E END if END is specified;
otherwise, there is no end-of-file string
-I R same as –replace=R
-i, –replace[=R] replace R in INITIAL-ARGS with names read
from standard input; if R is unspecified,
assume {}
-L, –max-lines=MAX-LINES use at most MAX-LINES non-blank input lines per
command line
-l[MAX-LINES] similar to -L but defaults to at most one non-
blank input line if MAX-LINES is not specified
-n, –max-args=MAX-ARGS use at most MAX-ARGS arguments per command line
-o, –open-tty Reopen stdin as /dev/tty in the child process
before executing the command; useful to run an
interactive application.
-P, –max-procs=MAX-PROCS run at most MAX-PROCS processes at a time
-p, –interactive prompt before running commands
–process-slot-var=VAR set environment variable VAR in child processes
-r, –no-run-if-empty if there are no arguments, then do not run COMMAND;
if this option is not given, COMMAND will be
run at least once
-s, –max-chars=MAX-CHARS limit length of command line to MAX-CHARS
–show-limits show limits on command-line length
-t, –verbose print commands before executing them
-x, –exit exit if the size (see -s) is exceeded
–help display this help and exit
–version output version information and exit

I wanted to delete all local branches except the develop branch and I typed in:

$ git branch | grep -v "develop" | xargs git branch -D
'xargs' is not recognized as an internal or external command,
operable program or batch file.

Windows xargs reading from STDIN

Apparently, the xargs is not found on windows command shell, and we can quickly implement a xargs-like batch utility like this:

@echo off
:: example:   git branch | grep -v "develop" | xargs git branch -D
:: https://helloacm.com/simple-xargs-batch-implementation-for-windows/
setlocal

set args=%1
shift
:start
    if [%1] == [] goto start1
    set args=%args% %1
    shift
    goto start

:start1
    for /F "tokens=*" %%a in ('more') do (
        %args% %%a
    )

And here we go… except there is a tiny error which can be safely dis-regarded. The idea is to first shift all command line parameters and concatenate them into a single variable %args%, then using a for utility, we can simulate and obtain each line from the STDIN, then finally build and execute command in the form of %args %%a

Please note that the set /p does not work with pipeline and that is why we use the more together with for /F “tokens=*” to achieve the similar tasks.

git-branch-delete-using-xargs Simple xargs Batch Implementation for Windows batch script tools / utilities windows batch windows command shell windows scripting host

git-branch-delete-using-xargs

Windows xargs reading from File

The xargs supports reading from the file instead of the Standard Input (STDIN). We can improve the above xargs batch script by checking if the first argument is -a followed by the text file as input. Example usage:

xargs -a input.txt echo

The input.txt contains three lines:

1
2
3

So the above xargs will be equivalent to the below after building the commands:

echo 1
echo 2
echo 3

We use the setlocal enabledelayedexpansion to set the correct source for the for /f – either the ‘more’ command or the text file:

@echo off
:: example:   git branch | grep -v "develop" | xargs git branch -D
:: example    xargs -a input.txt echo
:: https://helloacm.com/simple-xargs-batch-implementation-for-windows/
setlocal enabledelayedexpansion

set args=
set file='more'

:: read from file
if "%1" == "-a" (
    if "%2" == "" (
        echo Correct Usage: %0 -a Input.txt command
        goto end
    )
    set file=%2
    shift
    shift
    goto start
)

:: read from stdin
set args=%1
shift

:start
    if [%1] == [] goto start1
    set args=%args% %1
    shift
    goto start

:start1
    for /F "tokens=*" %%a in (!file!) do (
        %args% %%a
    )

:end

xargs is now open source: https://github.com/DoctorLai/BatchUtils/blob/master/xargs.cmd

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
907 words
Last Post: How to Show/Execute History Command Lines in Windows Command Line Prompt?
Next Post: Binary Search to Guess Number Game (C++ Coding Exercise)

The Permanent URL is: Simple xargs Batch Implementation for Windows

2 Comments

  1. Captain
  2. ck

Leave a Reply