Trying to do:
fake-command.bat "ping -n 4 -w 1 127.0.0.1 >NUL"
and
fake-command.bat ping -n 4 -w 1 127.0.0.1
The batch file could look like:
@echo %*
It should return:
ping -n 4 -w 1 127.0.0.1 >NUL
and
ping -n 4 -w 1 127.0.0.1
Here a workaround:
@echo off
goto start
------------------------------------------------------
Usage : mystring <command>
Quotes around the command are required only when the
command involves redirection via <, >, >>, or |, etc.
Quotes ensure that the redirection is applied to the
command, rather than the bat command itself.
Examples :
mystring ping -n 4 -w 1 127.0.0.1
mystring "ping -n 4 -w 1 127.0.0.1 >NUL"
------------------------------------------------------
:start
SETLOCAL ENABLEDELAYEDEXPANSION
SET "MYSTRING=%*"
ECHO My String = !MYSTRING!
SET !MYSTRING=MYSTRING:>=^>!
CALL :BATCH_FUNCTION !MYSTRING!
GOTO :EOF
:BATCH_FUNCTION
SET "ARGS=%~1"
ECHO Arguments = !ARGS!
endlocal
GOTO :EOF
the problem is, after: mystring "ping -n 1 127.0.0.1 >NUL"
It returns:
My String =
Arguments =
and after: mystring ping -n 1 127.0.0.1
It returns:
My String = ping -n 1 127.0.0.1
Arguments = ping
UPDATE: I updated the question with the following code from Get list of passed arguments in Windows batch script (.bat)
@echo off
SETLOCAL DisableDelayedExpansion
SETLOCAL
if exist param.txt (del param.txt)
for %%a in ('%*') do (
set "prompt="
echo on
for %%b in ('%*') do rem * #%~1#
@echo off
) > param.txt
ENDLOCAL
for /F "delims=" %%L in (param.txt) do (
set "param1=%%L"
)
SETLOCAL EnableDelayedExpansion
set "param1=!param1:*#=!"
set "param1=!param1:~0,-2!"
echo My string is = !param1!
With this code, I can get escape characters, but with arguments not in quotes output is broken
What works?
mystring "# % $ ` ' ( ) < << >> > >NUL && & || | { } \ / - + = , . : ; ^ " &REM OK
mystring "ping -n 4 -w 1 127.0.0.1 >NUL" &REM OK
It returns:
My string is = # % $ ` ' ( ) < << >> > >NUL && & || | { } \ / - + = , . : ; ^
My string is = ping -n 4 -w 1 127.0.0.1 >NUL
What does not work?
mystring ping -n 4 -w 1 127.0.0.1 &REM NOK
mystring "*" &REM NOK
It returns:
My string is = ping
The system can not find the file param.txt.
My string is = *
I do not see how in this code add the trick from Mofi.