Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive even the strangest command line parameters?

as discussed in an other thread How to avoid cmd.exe interpreting shell special characters like < > ^ it is not easy to get all parameters from the command line.

A simple

set var=%1
set "var=%~1"

are not enough, if you have a request like

myBatch.bat abc"&"^&def

I have one solution, but it needs a temporary file, and it is also not bullet proof.

@echo off
setlocal DisableDelayedExpansion
set "prompt=X"
(
    @echo on
    for %%a in (4) do (
        rem #%1#
    ) 
) > XY.txt
@echo off
for /F "delims=" %%a in (xy.txt) DO (
  set "param=%%a"
)
setlocal EnableDelayedExpansion
set param=!param:~7,-4!
echo param='!param!'

It fails with something like myBatch.bat %a, it display 4 not the %a

in this situation a simple echo %1 would work.
It's obviously the for-loop but I don't know how to change this.
Perhaps there exists another simple solution.

I don't need this to solve an actual problem, but I like solutions that are bullet proof in each situation, not only in the most cases.

like image 547
jeb Avatar asked Nov 17 '10 00:11

jeb


People also ask

How do I get to command line arguments?

Go to the Shortcut tab, then locate the Target field, which lists the exact location of the file within quotation marks. In the Target text box, place the cursor after the last quotation mark, then add a blank space followed by the command line parameters. All command line parameters are preceded with a hyphen (-).

How do I pass a command line argument in command prompt?

option. You can test command line arguments by running an executable from the "Command Prompt" in XP, Vista or later, or from the "DOS prompt" in older versions of Windows. You can also use command line arguments in program shortcuts, or when running an application by using Start -> Run.

What is command line syntax?

In the computer world, the syntax of a command refers to the rules in which the command must be run in order for a piece of software to understand it. For example, a command's syntax may dictate case-sensitivity and what kinds of options are available that make the command operate in different ways.

What are parameters in commands?

The Parameter (PARM) command definition statement defines a parameter of a command being created. A parameter is the means by which a value is passed to the command processing program. One PARM statement must be used for each parameter that appears in the command being defined.


Video Answer


1 Answers

I don't think anyone found any holes in this, except for the inability to read newlines in the parameters:

@echo off
setlocal enableDelayedExpansion
set argCnt=1
:getArgs
>"%temp%\getArg.txt" <"%temp%\getArg.txt" (
  setlocal disableExtensions
  set prompt=#
  echo on
  for %%a in (%%a) do rem . %1.
  echo off
  endlocal
  set /p "arg%argCnt%="
  set /p "arg%argCnt%="
  set "arg%argCnt%=!arg%argCnt%:~7,-2!"
  if defined arg%argCnt% (
    set /a argCnt+=1
    shift /1
    goto :getArgs
  ) else set /a argCnt-=1
)
del "%temp%\getArg.txt"
set arg

The above comes from a lively DosTips discussion - http://www.dostips.com/forum/viewtopic.php?p=13002#p13002. DosTips user Liviu came up with the critical SETLOCAL DisableExtensions piece.

like image 91
dbenham Avatar answered Nov 13 '22 10:11

dbenham