Joseph
Joseph

Reputation: 607

How to include spaces in batch variable?

I run the following batch script which asks the user for information and assigns this to the variables.

set var_type=1
set var_name=1
set var_technology=1

set /p var_type="Enter type (E.g. "Equipment" or "Technology"): "
IF /I %var_type%==Technology set /p var_technology="Enter technology name: "
set /p var_name="Enter name: "

cmd /k python "script.py" %var_type% %var_technology% %var_name%

The script works fine when the user types in a single string. E.g:

Enter type (E.g. "Equipment" or "Technology"): Technology
Enter technology name: Solar
Enter name: Philips

The above variables are passed to a python script which creates folders using the names above:

C:/Users/ME/Desktop/Technology/Solar/Philips

But if the user types in a second string for the technology name, the second string is treated as the third variable. E.g:

Enter type (E.g. "Equipment" or "Technology"): Technology
Enter technology name: Solar PV
Enter name: Philips

Now the folder structure is:

C:/Users/ME/Desktop/Technology/Solar/PV

Where it should be:

C:/Users/ME/Desktop/Technology/Solar PV/Philips

Instead of forcing users to use an underscore between strings, is there a method to include spaces in the variables?

Upvotes: 0

Views: 2655

Answers (1)

Joey
Joey

Reputation: 354516

Quote your arguments:

cmd /k python "script.py" "%var_type%" "%var_technology%" "%var_name%"

As an aside: Any reason why you're starting a new shell there? And keep it open afterwards (/k)? And why the echo on at the end?

Upvotes: 3

Related Questions