8

I have a powershell file that replaces an IP in a config file. The powershell takes in three parameters:

  1. file name ('C:\Program Files (x86)\app\app.conf')
  2. IP to replace
  3. new IP

Since we are dependent on using a batch file for our service to process the task, I wrote a batch script that simply takes three parameters and posts that to the powershell script.

We use batch file for calling and parsing the parameter: ($ just used to represent variables)

changeip.bat '$filepath' $oldip $newip

This fails, but I don't understand why. I've tried using double quotes instead of single quotes around $filepath and it worked.

How can the whitespace in $filepath be understood by double quotes and not single quotes?

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Rushabh Dudhe
  • 101
  • 2
  • 8
  • 1
    Without having code of your `changeip.bat` it's a bit difficult to say why it doesn't work. – Robert Dyjas Jun 28 '18 at 10:30
  • Possible duplicate of [What does single quote do in windows batch files?](https://stackoverflow.com/questions/24173825/what-does-single-quote-do-in-windows-batch-files) – phuclv Jun 28 '18 at 11:49

2 Answers2

15

The short answer is that CMD doesn't treat single quotes as anything but a regular character. It's not a pair/group like double quotes.

The only place where the single quote has any special meaning is in a FOR /F loop, where you are specifying a command to run and iterate over the output.

FOR /F %%d IN ('DIR /S /B C:\Windows') DO @ECHO File: %%d

FOR /F has an option to use backticks instead, in case you need to pass single quotes to the called process, but this is unrelated to your question.

mojo
  • 4,050
  • 17
  • 24
2

I assume you are calling the batch file and its variables in powershell. The single quotation pretty much denotes an as is state for the text between them and doesn't expand it to a variable, where as the double quotes expand the variable and wrap the text into a single string.

EG.

$Hostname = "Computer Name"
Get-WmiObject Win32_computersystem -ComputerName '$Hostname'

This will try and search for a computer called $Hostname

$Hostname = "Computer Name"
Get-WmiObject Win32_computersystem -ComputerName "$Hostname"

This will try and search for a computer called "Computer Name"

This is a poor example as you don't need the quotations for either of these, but you get the idea.

Drew
  • 3,814
  • 2
  • 9
  • 28