set global variable in batch file?

set variable February 3, 2009 at 01:13:14
Specs: Windows XP
I've write a simple file to get input from cmd.
Ex: test.bat
if I call C:\>test
The program should ask "Please enter your name:"
Ater that it show the name out
If I call C:\>test myname
It show the name out

Here is what I've done:
@echo off

if .%1 == . goto getname

echo %1

goto exit

SET name=
SET /P pass= Please enter your name:
set %1 = %pass%


It's ok if I call C:\>test myname
But error happens when I call C:\>test.
Can SO help me on this??

See More: set global variable in batch file?

Report •

February 3, 2009 at 01:22:32
You can't set the command line arguments from within the batch file. The best you can do is shift 'em down.

The suggested solution would be to just use %pass% instead of %1. The crazy/stupid solution would be to call yourself with the desired arguments:
"%~f0" %pass%

Report •

February 3, 2009 at 16:59:00
Thanks for your help but I do not clearly understand what you explained. Is there any other way to do to follow the requirement mentioned above?

Report •

February 4, 2009 at 17:34:20
You can't set %0 - %9 directly; only CMD can do that. I'm not sure how else to phrase that.

Report •

Related Solutions

February 4, 2009 at 17:51:34
@echo off & setLocal EnableDelayedExpansion

if %1'==' (
set /p var=name?
) else (
set var=%1
echo !var!

If at first you don't succeed, you're about average.


Report •

February 4, 2009 at 20:37:44
Great! It works as what I want.
Thanks for all :)

Report •

Ask Question