1

I'm trying to remove quotes in some text using parameter expansion in Batch. Can anyone tell me why this:

@echo off
setlocal

set args=%*
echo %args:"=%

prints "= instead of nothing? As far as I can see %args:"=% should replace all quotes with nothing, so I don't get why this is happening.

Any help would be appreciated, thanks!

edit: To clarify, I'm not passing any parameters to the batch script.

James Ko
  • 25,479
  • 23
  • 92
  • 196

2 Answers2

3

That is the result you get when you do not pass any arguments to your script.

If args is not defined, then %args:"=% is expanded as follows:

  • %args: is treated as a non existent variable expansion, which becomes nothing
  • "= is treated as itself
  • % (a lone percent) is stripped

It is not intuitive, but that happens to be how cmd.exe works. See https://stackoverflow.com/a/7970912/1012053 for more info.

You can prevent the problem by using if defined

@echo off
setlocal

set args=%*
if defined args echo %args:"=%
Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353
1

It works here

C:\Windows\system32>"C:\Users\User\Desktop\Test2.bat" dog
dog

Removing echo off shows this

C:\Windows\system32>setlocal

C:\Windows\system32>set args=dog

C:\Windows\system32>echo dog
dog

You are not passing any command line arguments.

C:\Windows\system32>"C:\Users\User\Desktop\Test2.bat"

C:\Windows\system32>setlocal

C:\Windows\system32>set args=

C:\Windows\system32>echo "=
"=