Problem passing parm to .bat with whitespaces

February 17, 2011 at 07:06:53
Specs: Windows XP
I am facing a problem while passing argument to a batch file. The
argument will be a file path. In the file path, if there are not white
spaces, it is working fine. But if any white spaces are there, it is
not recognizing that, and treating that is the end of file path.
I have tried enclosing the entire file path in double quotes. Even this
does not work. Could any one please suggest how do I pass a file path,
having white spaces.


@rem BATCH FILE
@echo on
if "%1" == "" goto error1
if "%2" == "" goto error2
@rem * Copy from source to destination including subdirs and hidden
@rem * File	
xcopy "%1" "%2" 
pause
goto endofprogram
:error1
echo You must provide source
echo Syntax:
echo %0 source destination
pause
goto endofprogram
:error2
echo You must provide destination
echo Syntax:
echo %0 source destination
pause
goto endofprogram
:endofprogram


See More: Problem passing parm to .bat with whitespaces

Report •


#1
February 17, 2011 at 07:12:52
The problem is not your program it is how you are executing it. You must put quotes around the parameters in the execution.

mybat "c:\my path\" "d:\my path"


Report •

#2
February 17, 2011 at 07:30:11
Thank you for replying.

I did try that prior to posting. Below are the different ways that I tried to execute without success.

copyfile.bat "U:\Gateway Folder\TEST\" "U:\Gateway Folder\TEST\ProcessFolder"

also tried:

copyfile.bat "\\server\uploads\Gateway Folder\TEST\" "\\server\uploads\Gateway Folder\TEST\ProcessFolder"

Neither of the above worked...

So I tried to process the xcopy command specifically as:
xcopy "\\server\uploads\Folder\TEST\" "\\server\uploads\Folder\TEST\ProcessFolder" /I

And the above goes into an infinite loop..

Any further advice that may help?


Report •

#3
February 17, 2011 at 07:50:19
When you embrace parameters with double quotes then in the script you have to use the ~ modifier to erase them if you code around double quotes, i.e.

if you call mybat "My Param with spaces"

you have to code e.g.

if "%~1"

otherwise the result is ""My param with spaces"".


Report •

Related Solutions


Ask Question