Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an environment variable to a value with spaces in a batch file?

I don't know how to describe exactly what I'm trying to do but here's an example batch file that demonstrates what I can't figure out.:

I've got a batch file. Inside that batch file I'm trying to create a directory:

Set CopyFrom = %~dp0

if Exist "%ProgramFiles(x86)" (
  Set TargetDir = %ProgramFiles(x86)%\My Directory Name has spaces
)

md %TargetDir%\NewSubFolder
copy %CopyFrom%\SourceFile.zip %TargetDir%\NewSubFolder

My batch file is failing on line 4 Set TargetDir =... with:

\My was unexpected at this time

I'm assuming this is because I have spaces in my path name. I thought I could just wrap my variable with quotes:

Set TargetDir = "%ProgramFiles(x86)%\My Directory Name has spaces"

But then when I get to the line that creates the directory it fails because %TargetDir% is now wrapped in quotes. md "%TargetDir%"\NewSubFolder

Can this be fixed or should I just write a VBScript to sort things out?

like image 609
BobTheBuilder Avatar asked Nov 07 '09 21:11

BobTheBuilder


People also ask

How do I set an environment variable in a batch file?

To set the value TEST&1 for the environment variable testVar, type: set testVar=test^&1. To set an environment variable named INCLUDE so the string c:directory is associated with it, type: set include=c:directory. You can then use the string c:directory in batch files by enclosing the name INCLUDE with percent signs ( % ).

How do I set the path in a batch program?

To use the set command in a batch program to add a new directory to the PATH environment variable, type: @echo off rem ADDPATH.BAT adds a new directory rem to the path environment variable. set path=%1;%path% set. To display a list of all of the environment variables that begin with the letter P, type: set p.

Is it possible to set environment variables from the command line?

The thing is you can set your variables using the command line if you have a very small number of environment variables. But if you need to set multiple variables for example 10, then it will be very difficult to manage them in the command line. That's why developers most of the time prefer to use a .env file to store environment variables.

How do I use environment variables in MS-DOS?

Use environment variables to control the behavior of some batch files and programs and to control the way Windows and the MS-DOS subsystem appears and works. The set command is often used in the Autoexec.nt file to set environment variables. If you use the set command without any parameters, the current environment settings are displayed.


2 Answers

Just put your expression in quotes like this:

C:\>Set "TargetDir=%ProgramFiles%\My Directory Name has spaces"
C:\>echo %TargetDir%
C:\Program Files\My Directory Name has spaces

Note: It will expand the variable within the quotes, and if it too has spaces, it will need to be quoted.

Now you can quote it to perform your operation:

md "%TargetDir%\NewSubFolder"
like image 59
zdan Avatar answered Sep 20 '22 01:09

zdan


The problem in question here are not the spaces as others suggested, but rather the closing parenthesis in the environment variable ProgramFiles(x86) This causes the parser to think that the block ends prematurely (shameless self-promotion).

Quotes do help in this case because they make the parser jump over the whole quoted part and rightly assume the following parenthesis to be the actual closing one. but the fix might be much easier than that:

if Exist "%ProgramFiles(x86)%" Set TargetDir=%ProgramFiles(x86)%\My Directory Name has spaces

Why use a parenthesized block at all if all you do it put exactly one command into it?

set itself doesn't need any quotes, except when its arguments contain special characters like <, >, |, & which the shell itself aready handles. It isn't a panacea, though which makes handling user input or file contents correctly a pain at times.

Also, please never ever put spaces around the = in a set command. This will cause an environment variable to be created with its name ending in a space and its contents starting with a space. This was partially corrected in Windows 7 by silently creating both the variable with the space at the end and one without:

> set foo = bar
> set foo
foo=bar
foo = bar

But in previous versions of Windows this didn't happen so just never use spaces around the = unless you know this is what you want :-)

like image 26
Joey Avatar answered Sep 21 '22 01:09

Joey