xargs start

  • Comments 3

Like many programmers, I've messed around in a lot of development environments - including the UNIX/Linux family of operating systems.

One of the UNIX commands I like is xargs... this is very handy when writing one-off command lines or scripts.

I like it so much that I miss it terribly when I'm in a Windows OS.  So I wrote an xargs.bat and stuck it in my PATH.  Now instead of writing complicated for loops, I can just pipe to xargs.

The script is "powered by" the highlighted line:

C:\Users\MatEer\Desktop\custom-path>type xargs.bat
@echo off

setlocal

if /i (%1)==(/?) goto USAGE

if /i (%1)==() goto USAGE

if /i (%1)==(/addquotes) goto ADDQUOTES

goto NOQUOTES

:USAGE
echo usage: something-that-produces-output ^| %0 [/?] [/addquotes] thing-to-run
echo   xargs.bat by Matthew van Eerde 10/3/2005
echo.
echo   something-that-produces-output should write to its STDOUT
echo   thing-to-run will have each line of the output appended to it,
echo   then will be run successively
echo.
echo   If /addquotes is set, quotes will be added around the line
echo   before appending the line to thing-to-run
echo.
echo   If you call xargs without piping output to it, xargs will wait
echo   for you to type something in on STDIN.
echo   Ctrl-Z on its own line to finish.
goto END


:ADDQUOTES

rem eat /addquotes parameter
shift

rem Alas, shift doesn't affect %*
if (%1)==() goto USAGE
set basecommand=%1
shift

:BUILDBASECOMMAND
if (%1)==() goto DONEBASECOMMAND
set basecommand=%basecommand% %1
shift
goto BUILDBASECOMMAND
:DONEBASECOMMAND

rem run the program specified by %*
rem as many times as there are lines in STDIN
rem with one extra argument -- defined by each line of STDIN -- in quotes
rem
rem all that the find command does is intercept STDIN
rem
for /F "usebackq delims=" %%a in (`find /v ""`) do %basecommand% "%%a"

goto END


:NOQUOTES

rem run the program specified by %*
rem as many times as there are lines in STDIN
rem with extra arguments defined by each line of STDIN
rem
rem all that the find command does is intercept STDIN
rem
for /F "usebackq delims=" %%a in (`find /v ""`) do call %* %%a

goto END


:END

endlocal

This allows wonderfully anaOStic things like findstr /m IFoo * | xargs start

Leave a Comment
  • Please add 6 and 6 and type the answer here:
  • Post
  • Why don't you just install the Cygwin utilities and get a lot of other Unix utilities to boot, including "less" which is SO much more than the DOS "more" command.

  • This was posted to channel9 ages ago and I got it from there, thanks for writing this!

  • ever heard of forfiles?

    in windows/system32

    easy to use and (perhaps*) easier to  monitor than a loop.

    *e.g. if you attempt a large job with xargs and something goes wrong

Page 1 of 1 (3 items)