using the DOS start command when passed arguments have quotes - windows

I have a question about the DOS start command.
I have already read this topic:
Using the DOS “start” command with parameters passed to the started program
Using the "start" command with parameters passed to the started program
but my question is a little different.
I have this problem: I need to pass paths that need to be quoted.
For example, if path have no quotes this works fine:
start "" app.exe -option c:\myapp\myfile.txt
but if path have double quotes it doesn't works.
I have this line in my BATCH file:
start "" myapp.exe -option %mypath%
and when %mypath% contains double quotes (paths that have spaces or other characters in names) the start command returns very strange results.
Thanks
Sandro

Normally it's not a problem to use parameters there with quotes, but you get problems if your app-path has also quotes.
Then you need to add an extra CALL statement.
start "" app.exe -option c:\myapp\myfile.txt - Works
start "" app.exe -option "c:\myapp\myfile.txt" - Works
start "" "app.exe" -option c:\myapp\myfile.txt - Works
start "" "app.exe" -option "c:\myapp\myfile.txt" - Don't works
start "" CALL "app.exe" -option "c:\myapp\myfile.txt" - Works

This might help, but it is a bit way round about method and slight modification may required to suit your need.
The idea is to:
Dump the environment variable which has quotes to a text file with a predefined name. Like:"set mypath2 > withQt.bat"
Use windows power shell or some third party tool to find and replace quotes in that file.
Create another text file (one time step only) containing string "Set "
Use copy command to append the file mentioned in step2 with the file created in step3 and create a batch file with a predefined name. Like: copy base.bat + withQt.bat withtqt.bat
Run the batch file, which creates another/replaces the environment variable with value without quotes.
Sorry, I couldn't get something more elegant at this time.

Related

proper way to remove double quotes from string in batch

I've got a batch script (app1.bat) calling another batch script (app2.bat) which itself calls a program in windows (program.exe).
app2.bat calls program.exe with a parameter after a flag in this way:
program.exe -f Parameter with whitespaces coming into the program
What I want to do is to pass the phrase that comes to program.exe from app1.bat into app2.bat but i don't know how to properly handle the doublequotes. Currently I am passing the phrase from app1.bat to app2.bat in double quotes and inside an app2.bat (prior to executing program.exe) I get rid of the quotes like that:
inside app1.bat
call app2.bat "Parameter with whitespaces coming into the program"
inside app2.bat
set old_phrase=%1%
set new_phrase=%old_phrase:"=%
program.exe -f %new_phrase%
old_phrase is
"Parameter with whitespaces coming into the program"
and new_phrase I end up with is
Parameter with whitespaces coming into the program
Is there any standard way to handle such a situation (being passing a string to an external program which expects a tring without quotes and being ok with whitespaces, whereas batch does not allow for no-quotes-and-whitespaces strings)
When you execute call /? from cmd to launch the help you will see quite a bit around expansion of %n
The first one states:
%~1 - expands %1 removing any surrounding quotes (")
You can therefore dump all the other set commands and simply run this in your batch file:
program.exe -f %~1

Windows bat file syntax '&' for starting html

Hi I have a bat file but it is unable to read my syntax & in it.
#echo off
start http://localhost/?test=name&pass=pass
pause
this works but it kept playing 'http://localhost/?test=name' only... I'm unable to read the syntax &.
Any suggestion? THanks!
To prevent the & from being evaluated, enclose the URL in quotation marks. You'll also need an additional set of empty quotation marks as the first argument to start, as start regards the first quoted argument as a console window title.
start "" "http://localhost/?test=name&pass=pass"
You could alternatively do as Noodles commented above and escape the & with a caret, but the way I demonstrated does not require you to modify your URL query string to make it compatible with your script.

Command works on command line, but fails in batch file

When trying to setup Glassfish configuration in a batch file there is one command that works when run directly from the command line - but fails when put in a windows batch file.
The command:
call asadmin.bat create-auth-realm --classname com.sun.enterprise.security.auth.realm.ldap.LDAPRealm --property jaas-context="ldapRealm":directory="ldap\://domain.com\:389:base-dn=dc\=domain,dc\=com:group-base-dn=ou\=Groups,ou\=domain,dc\=com":search-bind-dn="CN\=username,OU\=Accounts,OU\=domain,DC\=com":search-bind-password="password":search-filter="(&(objectCategory\=user)(sAMAccountName\=%s))":group-search-filter="(&(objectCategory\=group)(member\=%d))" a-realm
When run on the command line exactly as above it completes with the response:
Command create-auth-realm executed successfully.
When run from a batch file exactly as above it fails with the response:
(member\ was unexpected at this time.
Note that the escaping of some equals characters is for Glassfish, not an attempt at escaping characters for the windows batch command.
My guess is that when run in a batch file, the batch file is treating some characters as special. I have tried escaping the parentheses with no luck.
How can this command work in a batch file!?
Your problem is in the variables %s and %d.
If they need to be interpreted by the batch file (they are environment variables), they should be %s% and %d%.
IF they are not environment variables, and need to be interpreted in (don't know what glassfish is), then they should be %%d and %%s
It seems like a problem with the percent signs.
In a batch file percent signs are removed, when no matching percent sign is found or a enclosed variable isn't defined.
On the command line they simply stay unchanged.
In a batch file a percent sign can be escaped by a second percent.
call asadmin.bat create-auth-realm --classname com.sun.enterprise.security.auth.realm.ldap.LDAPRealm --property jaas-context="ldapRealm":directory="ldap\://domain.com\:389:base-dn=dc\=domain,dc\=com:group-base-dn=ou\=Groups,ou\=domain,dc\=com":search-bind-dn="CN\=username,OU\=Accounts,OU\=domain,DC\=com":search-bind-password="password":search-filter="(&(objectCategory\=user)(sAMAccountName\=%%s))":group-search-filter="(&(objectCategory\=group)(member\=%%d))" a-realm
Windows use of carets ( ^ ) to escape special characters. Try to replace the backslashes at the carets.
call asadmin.bat create-auth-realm --classname com.sun.enterprise.security.auth.realm.ldap.LDAPRealm --property jaas-context="ldapRealm":directory="ldap\://domain.com\:389:base-dn=dc\=domain,dc\=com:group-base-dn=ou\=Groups,ou\=domain,dc\=com":search-bind-dn="CN\=username,OU\=Accounts,OU\=domain,DC\=com":search-bind-password="password":search-filter="(&(objectCategory\=user)(sAMAccountName\=%s))":group-search-filter="(&(objectCategory\=group)(member\=%d))" a-realm
^
the &'s need to be escaped with ^ I think. try it... ;)

Why does appcmd.exe behave differently when executed inside a batch file?

I have the following appcmd to add an exception to IIS7's ISAPI and CGI restrictions. The exception I am adding should look like:
c:\perl\bin\perl.exe "%s" %s
Here is the command line:
appcmd set config -section:isapiCgiRestriction "-+[path='c:\perl\bin\perl.exe \"%s\" %s', allowed='true', description='Perl CGI']"
If execute this from the command line it does this correctly, however if I execute this inside a .cmd batch file the path gets mangled and ends up looking like:
c:\perl\bin\perl.exe "s
The trouble seems arise because I have to escape the quotation marks around the first %s perl.exe parameter. But why this should behave differently in a batch file is a bit of a puzzle.
Can anyone explain why this is happening?
The problem is that the command processor reads your "%s" %s and finds two mathing % signs, so this makes a valid batch variable (namely %" %). And after expanding that into nothing, only your "s remains.
You can escape a single %-sign in your batch file by doubling it, like this:
c:\perl\bin\perl.exe "%%s" %%s

Batch file equivalent of Unix parameter expansion with quotes

There have been a lot of questions asked and answered about batch file parameters with regards to the %*, but I haven't found an answer for this.
Is there an equivalent syntax in batch files that can perform the same behavior as "$#" in Unix?
Some context:
#echo off
set MYPATH=%~dp0
set PYTHON=%MYPATH%..\python\python
set BASENAME=%~n0
set XTPY=%MYPATH%..\SGTools\bin\%BASENAME%.py
"%PYTHON%" "%XTPY%" %*
This is the .bat file that is being used a proxy to call a Python script. So I am passing all the parameters (except the script name) to the Python script. This works fine until there is a parameter in quotes and/or contains spaces.
In shell scripts you can use "$#" to take each parameter and enclose it in quotes. Is there something I can do to replicate this process?
Example calls:
xt -T sg -t "path with possible spaces" -sum "name with spaces" -p <tool_name> -o lin32 lin64 win32 <lots of other options with possibilities of spaces>
The command/file xt simply contains the code listed above, because the actual executable is Python code in a different folder. So the point is to create a self-contained package where you only add one directory (xbin directory) to your path.
I'm not sure what the cleanest solution is, but here is how I worked around the problem:
setlocal EnableDelayedExpansion
for %%i in (%*) do set _args= !_args! "%%~i"
echo %_args%
%_args% will now contain a quoted list of each individual parameter. For example, if you called the batch file as follows:
MYBATFILE "test'1'file" "test'2'file" "test 3 file"`
echo %_args%
will produce the original quoted input.
I needed this for CMD files that take unfriendly file or directory names and pass them to Cygwin Bash shell scripts which do the heavy lifting, but I couldn't afford to have the embedded single quotes or spaces lost in the transition.
Note the ~i in %%~i% which is necessary to remove quotes before we apply quotes. When a parameter containing spaces is passed (e.g., "test 3 file" above), the CMD shell will already have applied quotes to it. The tilde here makes sure that we don't double-quote parameters containing spaces.

Resources