Escape "double quotes" inside batch's input parameters

Viewed 56630

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?

5 Answers

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
)
Related