25

I want to run the following in batch script where it takes an argument (a path)

runas /user:abc "icacls %1 /grant Everyone:(F) /T"

but the argument %1 already contains a " (because it's a path, passed on by context menu's Send To - I don't have much control over this). So when the command runs in batch script it runs like this:

runas /user:abc "icacls "c:\folder" /grant Everyone:(F) /T"

So obviously I need to escape the "s created by %1. How do I perform string manipulation over %1 such that it escaped the quotes?

0

5 Answers 5

26

You should be able to use \" Here is a site with a few other escape characters that you might find useful

To perform the string replace:

set temp=%1
set temp=%temp:"=\"%
echo %temp%
Sign up to request clarification or add additional context in comments.

6 Comments

what if %1 is a path that contains spaces?
When do you use "" over \"? I find batch files truly only escape with "" and the \" version is only for non-batch programs (runas coded in C++ or some language that recognizes \ as an escape character). So do we only use \" when passing variables to non-batch programs and "" when passing to other batch scripts (or batch functions (ex. CALL :function "parameterWith""EscapedQuote")
@Mr_Moneybags, According to robvanderwoude.com/escapechars.php, either one is a valid escape. I use \" over "" simply because it's my preference since I work with c# primarily.
@nick but if you try the line SET "var1=key=\"val&ue!\"" you get the error 'ue!\""' is not recognized as an internal or external command, operable program or batch file. But "" works.
@Mr_Moneybags, Odd. I wouldn't expect either to work since & should be escaped as ^&. I'm wondering if maybe "" tells the batch file "don't treat anything in this string as a special character" similar to placing @ at the start of a string in c#. I'm not sure though, I haven't played with batch in forever
|
7

Each response covers part of the answer. Combining them using backslash & quote: \" you get:

runas /user:abc "icacls \"%~1\" /grant Everyone:(F) /T"

or you can doubled (double) quotes to escape them:

runas /user:abc "icacls ""%~1"" /grant Everyone:(F) /T"

As a side note ^ is occasionally useful for escaping special characters such as <, |, >, ||, &&, and & For example:

echo ^|

but this is pretty rare.

1 Comment

Doubled double quotes does not work for me, but backslash does.
2
SET myPath=%1
SET myPath=%myPath:"=\"%
runas /user:abc "icacls %myPath% /grant Everyone:(F) /T"

Edit - The variable name was changed from path to myPath. PATH is a reserved system variable that should not be used for anything other than what it was intended.

1 Comment

Do NOT use "path" as your variable - it is reserved for a special purpose in Windows. Modifying the value can cause problems later in the script. Change the variable name from "path" to something else, perhaps "mypath". Edit your answer and then I can up vote it.
0

A search/replace isn't even needed in such cases. Just strip the original quotes from your argument (with %~1) - then you can add again whatever you want, e.g. the escaped quotes. In your example:

runas /user:abc "icacls \"%~1\" /grant Everyone:(F) /T"

Comments

0

After some quick testing, this is the best method because it :

Upside

  • handles multiple results output from tasklist
  • handles application names with spaces if needed
  • Can easily be changed to case sensitive / insensitive

Downside

  • Calling more than one command which is technically slower

Code

@echo off    
set "EXE=authy desktop.exe"
tasklist /NH /FI "imagename eq %EXE%" | find /i "%EXE%" >nul 2>&1
If %errorlevel% EQU 0 (
    echo %EXE% Is Running
) ELSE (
    echo %EXE% is Not Running
)

Further Consideration

You can change the to && ( ) || ( ) formatting instead of checking errorlevel. Also you can use 'setlocal enabledelayedexpansion' if errorlevel is used more than once by referencing with !errorlevel!.

tasklist /NH /FI "imagename eq %EXE%" | find /i "%EXE%" >nul 2>&1 && (
    echo %EXE% Is Running
) || (
    echo %EXE% is Not Running
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.