Skip path with spaces as parameter in bat file

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




In my code I have

: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 just 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> 
+44
command parameters batch-file
Jan 23 '09 at 14:42
source share
7 answers

Interesting. I like to collect quotes about handling quotes in the cmd / command.

Your specific scripts are fixed with% 1 instead of "% 1" !!!

By adding "echo on" (or getting rid of the echo), you could easily find this.

+15
Jan 23 '09 at 16:10
source share

Use "%~1" . %~1 only removes surrounding quotes. However, since you cannot know if the input parameter %1 quotes or not, you must make sure that "%~1" sure that they are added. This is especially useful when combining variables, for example. convert.exe "%~1.input" "%~1.output"

Literature:

+85
Dec 08 2018-10-12
source share

I think the problem with the OP was that he wants to make BOTH of the following:

  • Pass a parameter that may contain spaces
  • Check if parameter is missing

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

To check if a parameter is missing, the method I always studied was:

 if "%1" == "" 

However, if the actual parameter is specified (as it should be, if the value contains spaces), it becomes

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

which causes an "unexpected" error. If you use

 if %1 == "" 

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

 if == "" 

To fix this, use any other characters (except for characters with a special value for DOS) instead of quotation marks in the test:

 if [%1] == [] if .%1. == .. if abc%1xyz == abcxyz 
+13
May 05, '09 at 16:29
source share

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

Not sure what exactly you are asking?

+5
Jan 23 '09 at 14:44
source share

"% ~ 1" will work most of the time. However, there are a few things you need to note for this:

  • It is not supported on Windows NT 4.0. You need Windows 2000 or later. (If you are encoding a script compatible with the old OS, be careful.)
  • It does not make the parameters safe and disinfected. My observation is that in CMD scripts there is no way to deactivate command line arguments incorrectly .

To demonstrate the second point, let me give an example:

 REM example.cmd ECHO %~1 

Run with example.cmd dummy^&DIR . Ampersand hides here ( ^& ) to interpret the shell interpreter as a command delimiter so that it becomes part of the argument passed to the script. DIR is interpreted as a command inside a sub-shell that runs a script where it should not.

Quote: this may work for a while, but is still unsafe:

 REM example2.cmd SETLOCAL EnableExtensions EnableDelayedExpansion SET "arg1=%~1" ECHO "%~1" ECHO !arg1:"=! 

example2.cmd foo^"^&DIR^&^"bar will break it. The DIR command will be run twice, one immediately after SET, and the other immediately after the first ECHO. You see that "%~1" , which in your opinion has a quote, is not well sorted by the argument itself.

Thus, it is not possible to provide a secure analysis of arguments.

(EDIT: EnableDelayedExpansion also does not work on Windows NT 4. Thanks to the information here: http://www.robvanderwoude.com/local.php )

+5
Mar 06 '15 at 7:37
source share
 @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 enough.

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

  • 'empty string' ( "" ),
  • and then 'c: \ Program',
  • and then "unexpectedly here" "Files \ xxx",
  • followed by an empty string ( "" )

Note that if you need to use PWA_PATH in your IF clause, you need to specify if as !PWA_PATH! (hence enabledelayedexpansion as the start of the script)

+2
Jan 23 '09 at 16:15
source share

If your path contains a space, try using %~s1 . This will remove the space and add ~1 to your path, and, more importantly, this refers to the absolute path of your file. Try using it.

+2
Aug 31 2018-12-12T00:
source share



All Articles