views:

7219

answers:

5

I have a simple bat script that copies files from an known directory to a directory given by the user. How can I pass the path (it might contain spaces) to my script and use it with the xcopy command?

+3  A: 

If you have a path with spaces you must surround it with quotation marks (").

Not sure if that's exactly what you're asking though?

Phill Sacre
A: 

In my code i have the following

:READ_PWA_PATH
    if "%1" == "" ( 
     rem Set default path
     set PWA_PATH="C:\Program Files\PWA"
     rem
     echo You have not specified your PWA url.
     echo Default will be assumed: C:\Program Files\PWA. 
     choice /C:YN /M:"Do you wish to continue [Y] or cancel the script [N]?"
      IF ERRORLEVEL ==2 GOTO CANCEL
      IF ERRORLEVEL ==1 GOTO READ_WSS_SERVER_EXTENSIONS_PATH
     GOTO END
    ) else (
     set PWA_PATH=%1
    )

If I simply call the script I get the following error:

C:\Projects\Setup>install.cmd "C:\program files (x86)"

-----------------
SETUP SCRIPT
-----------------

files was unexpected at this time.
C:\Projects\Setup>
iulianchira
If you have @echo off at the top of your script, try commenting that out and see what it is actually running.
Phill Sacre
You should make this an edit to your initial question then delete this answer.
EBGreen
Please also show where you are referencing PWA_PATH, as I believe the code shown is not where the error is occurring.
Instantsoup
I simply do echo PWA_PATH: %PWA_PATH%
iulianchira
**@iulianchira:** Your error comes from the top **if** statement. use **"%~1"==""** instead and if should be ok.
Jay
+1  A: 

Interesting one. I love collecting quotes about quotes handling in cmd/command.

Your particular scripts gets fixed by using %1 instead of "%1" !!!

By adding an 'echo on' ( or getting rid of an echo off ), you could have easily found that out.

Vardhan Varma
+1  A: 
@echo off
setlocal enableextensions enabledelayedexpansion

if %1=="" (     
        rem Set default path
        set PWA_PATH="C:\Program Files\PWA"
        rem
        echo You have not specified your PWA url.
        echo Default will be assumed: C:\Program Files\PWA.     
        choice /C:YN /M:"Do you wish to continue [Y] or cancel the script [N]?"
                IF ERRORLEVEL ==2 GOTO CANCEL
                IF ERRORLEVEL ==1 GOTO READ_WSS_SERVER_EXTENSIONS_PATH
        GOTO END
    ) else (
        set PWA_PATH=%1
     @echo !PWA_PATH! vs. %1
     goto end
    )
:READ_WSS_SERVER_EXTENSIONS_PATH
echo ok
goto end
:CANCEL
echo cancelled
:end
echo. final %PWA_PATH% vs. %1

As VardhanDotNet mentions, %1 is enough.

"%1%" would add quotes around quotes: ""c:\Program Files\xxx"" which means:

  • 'empty string' (""),
  • followed by 'c:\Program',
  • followed by the "unexpected here" 'Files\xxx',
  • followed by an empty string ("")

Note however that if you need to use PWA_PATH within your IF clause, you need to refer if as !PWA_PATH! (hence the enabledelayedexpansion as the beginning of the script)

VonC
+1  A: 

I think the OP's problem was that he wants to do BOTH of the following:

  • Pass a parameter which may contain spaces
  • Test whether the parameter is missing

As several posters have mentioned, to pass a parameter containing spaces, you must surround the actual parameter value with double quotes.

To test whether a parameter is missing, the method I always learned was:

if "%1" == ""

However, if the actual parameter is quoted (as it must be if the value contains spaces), this becomes

if ""actual parameter value"" == ""

which causes the "unexpected" error. If you instead use

if %1 == ""

then the error no longer occurs for quoted values. But in that case, the test no longer works when the value is missing -- it becomes

if  == ""

To fix this, use any other characters (except ones with special meaning to DOS) instead of quotes in the test:

if [%1] == []
if .%1. == ..
if abc%1xyz == abcxyz
Alternatively, using **"%~1"** would remove exterior quotations and allow for a real testing, because you never know what the user will type, like **this** or **"this"**.
Jay