In batch scripting, is it possible to use percent variables (e.g. %PATH%
) from user inputted commands? I am creating a batch script that uses a user's input to run certain commands, and I want users to be able to use variables in the commands, but it doesn't work. I can use exclamation mark variables when I use SETLOCAL EnableDelayedExpansion
, but with it enabled or disabled, the percent variables won't work. For users who are unfamiliar with using exclamation mark variables (e.g. !PATH!
), I want it to be possible for the percent variables to be used. Any ideas why it doesn't work?
As per the comments, an example was requested. Here is a very basic version of what I have:
ECHO OFF
TITLE Batch Prompt
CLS
ECHO Test this code using "ECHO <command here>"
CD %USERPROFILE%
:CMD
set /P "COMMAND=>"
%COMMAND%
GOTO CMD
You can test this by copying it to Notepad and saving it as a Batch file, and using ECHO to see how it treats variables. Don't try any other command, since in this very basic version it may not work.
As can be seen upon testing, any variable using percent symbols (e.g. %HOMEDRIVE%) is not being displayed. But enable delayed expansion, and use exclamation marks (e.g. !HOMEDRIVE!) and it's value is displayed.
So how can I get this to work with the percent variables? Any idea what is going on that is causing this issue?