A batch script that stores optional piped multiline string and optional arguments
NickName:Foad S. Farimani Ask DateTime:2020-10-23T03:40:46

A batch script that stores optional piped multiline string and optional arguments

Consider the cmd/batch script program.bat which accepts optional piping and command-line arguments. The script will be executed in four different ways:

  1. program.bat: without piping and arguments
  2. program.bat <arguments>: with arguments
  3. <command> | program.bat: with piped multiline string
  4. <command> | program.bat <arguments>: with both piped multiline string and arguments

It is important to note that the output of <command> is not necessarily a single line (e.g., dir). The script should work with or without any of the piped information and arguments. Case 2 can easily be achieved using the %* to reach the arguments in the form of a list/array (reference). If I were sure that there will always be a one-line string piped I could use: (reference)

SET /p "piped="

However, this will expect the user to enter something in cases 1 and 2, when nothing is piped, which is undesirable. Plus, this only stores the first line of the <command> output to the piped variable. I would appreciate if you could help me know how I can have a script that:

  • works in all 4 above scenarios
  • stores the optional piped multiline string into a variable

P.S. To test the answer by @aschipfl, I created a file program.bat with the content:

@echo %CmdCmdLine%

I ran it in 3 different ways:

  • program.bat resulting

cmd

  • <someRandomCommand> | program.bat resulting

C:\WINDOWS\system32\cmd.exe /S /D /c" program.bat"

  • program.bat | FIND /v ""

C:\WINDOWS\system32\cmd.exe /S /D /c" program.bat"

although it is a brilliant workaround to detect the piping itself, it can not distinguish if the program.bat is the one who is piping or the one which is being piped to. This is not an issue that I had thought about.

Copyright Notice:Content Author:「Foad S. Farimani」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64489414/a-batch-script-that-stores-optional-piped-multiline-string-and-optional-argument

More about “A batch script that stores optional piped multiline string and optional arguments” related questions

A batch script that stores optional piped multiline string and optional arguments

Consider the cmd/batch script program.bat which accepts optional piping and command-line arguments. The script will be executed in four different ways: program.bat: without piping and arguments pr...

Show Detail

Optional arguments parsed in batch using shift

I would like to know if it is possible to make a more elegant solution for a batch file accepting arguments from another script. Looked at the solution here: Windows Bat file optional argument pars...

Show Detail

batch file - pop up with optional arguments

First time poster. I use a program called PDW. It scans pager frequencies and displays pager messages on the screen. One of the functions is that it can run a batch or COM file when it pics up a me...

Show Detail

Passing optional arguments to a function (where they are also optional)

Background I am experiencing a problem when I attempt to pass optional arguments to another function. The function being called also has optional arguments so I'm trying to get that optionality to

Show Detail

How to obtain optional command line argument value as a string in windows batch script?

Given an optional port argument, where the port number can vary in length, how do I obtain the port number from the batch script's command line arguments? Example: foo.bat --foo bar --port 80 --b...

Show Detail

PySimpleGUI for scripts with optional arguments?

I have a script which utilizes optional arguments with the help of argparse. When the optional arguments are not in use they default to None. I'm trying to build a GUI for the script with PySimpleG...

Show Detail

Parsing optional and not optional arguments

I am new with bash and after reading and trying a lot about how to parse arguments I cannot what I really want to do I want to parse optional and not optional arguments. More specifically I want to...

Show Detail

Optional arguments in Bash script

I am trying to write a function that has optional arguments in a way such that the optional arguments are follow by something like -x, for example my_function man_arg1 man_arg2 -n opt_arg1 -o opt_a...

Show Detail

Two optional arguments in batch

How to make batch script that takes three optional parameters, but you can pass for example first and third parameter? e.g. @echo off set FIRST_PARAM=%1 set SECOND_PARAM=%2 set THIRD_PARAM=%3 echo...

Show Detail

Passing optional jvm arguments to java thru batch and shell scripts in command line

Got a java class with main method, say com.foo.Bar. But there are two optional vm(system) arguments for this class, say key1, key2. Created a batch &amp; shell shell scripts to call the above pro...

Show Detail