ECHO %CHOICE% to existing batch file

May 13, 2010 at 08:42:40
Specs: Windows 7 Ultimate, 4GB DDR2
Hi, I'm struggling with a command at my batch that was meant to set user's name ONCE, then set %name% to the rest of the batch file by using another batch, but this seems kinda impossible because %name% is already set as % % at the original batch [I'm pretty sure there's a very easier way to do this, but this is how far I could think of in 1 day =P] Here's the code:

@ECHO OFF
color 02

IF NOT 0%name% EQU 0 goto task

ECHO @ECHO OFF > name.bat
ECHO SET /P name=Enter your name: >> name.bat
ECHO pause >> name.bat
ECHO copy name.bat %%name%%.txt >> name.bat
ECHO exit >> name.bat

start name.bat
pause
del /Q name.bat
SET name=%name%.txt
::yes I know the last command is effed up

:task
COMMAND


See More: ECHO %CHOICE% to existing batch file

Report •

#1
May 13, 2010 at 09:10:15
I cut it down to this and it seems to build the expected bat:

=============================
@echo off & setLocal enableDELAYedeXpansion

set name=AAA

ECHO @ECHO OFF > name.bat
ECHO SET /P name=Enter your name: >> name.bat
ECHO pause >> name.bat
ECHO copy name.bat %%name%%.txt >> name.bat
ECHO exit >> name.bat


=====================================
Helping others achieve escape felicity

M2


Report •

#2
May 13, 2010 at 09:11:12
I admit I do not understand what your batch is performing, but I point out you CAN'T set the name variable by issuing

start name.bat

as the name.bat is executed in a different space than the calling one and its variables are unaccesible from the launching one.

A batch can't set up persistent environment variables, these die when the batch ends.

Replace the above statement with the plain

call name.bat

and so the variables are accessible from the calling one.


Report •

#3
May 13, 2010 at 09:22:37
Hi IVO,

Yeah, I have no idea why anybody wants to START a bat.


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
May 13, 2010 at 15:11:05
Hi people thanks for the replies!

IVO, I didn't know what was the difference between CALL and START, so that was the only thing I needed thanks!

Mechanix2Go, Hey go easy on me, I'm trying to learn DOS language by myself and I started just a month ago =P

Here's the working code I ended up with =D:


@ECHO OFF & setLocal enableDELAYedeXpansion
color 02

IF EXIST user.bat call user.bat
IF NOT 0%name% EQU 0 goto task

SET name=USER

ECHO @ECHO OFF > name.bat
ECHO SET /P name=Enter your name: >> name.bat
ECHO ECHO Welcome, %%name%% >> name.bat
ECHO pause >> name.bat

call name.bat
ECHO SET name=%name% > user.bat
del /Q name.bat

ECHO You are %name%.
pause
exit

:task
ECHO GRATZ!! This batch works!
pause


BTW: Is there a way of forbidding user of setting %name% with spaces, or to allow spaces? Like this:


@ECHO OFF
:a
SET /p name=Enter your name [no spaces allowed]
IF NOT %name% "has spaces" goto b
cls
ECHO "%name%" is invalid - Enter a name without spaces.
goto a
:b
ECHO Hi %name%.
COMMAND


Report •

#5
May 14, 2010 at 02:25:50
Replace

@ECHO OFF
:a
SET /p name=Enter your name [no spaces allowed]
IF NOT %name% "has spaces" goto b
cls
ECHO "%name%" is invalid - Enter a name without spaces.
goto a
:b
ECHO Hi %name%.
COMMAND

with

@ECHO OFF
:A
SET /p name=Enter your name [no spaces allowed]
IF "%name: =%"=="%name%"  goto :B
cls
ECHO "%name%" is invalid - Enter a name without spaces.
goto :A
:B
ECHO Hi %name%.
COMMAND

I don't understand why you code COMMAND at the end. Anyway if you want to load the command interpreter, be aware that under Windows NT family (from Windows NT to Windows 7) that is CMD.exe NOT COMMAND.COM.

You said you are learning DOS language, actually what you believe to be the DOS prompt is the System Textual Console as there is no DOS in Windows XP/Vista/7.

You are coding NT batch scripts, more powerful than their ancestors in plain DOS.


Report •

#6
May 14, 2010 at 16:41:26
Thank you both it works like a charm =]!

IVO, "COMMAND" was supposed to mean any commands I'd add to the batch [I didn't even know there was a command called "COMMAND" lol]. I'll probably use that code for many batches so it's pointless to use any specific commands as an example.

Also IVO, could you please explain how fixed the last one because I looked at it and I'm still confused =P?


Report •

#7
May 15, 2010 at 02:16:27
If you refer to

if "%name: =%"=="%name%" goto :B

%name: =% erases the blanks from the name string (without modifying the variable) and so the "modified" string is compared to the original one. If they differ that means the erased characters are present.

The above is a trick allowed by substring manipulation modifiers you can take a look at by typing SET /?.

If however your question is related to something else, please explain what has to be clarified.


Report •

#8
May 15, 2010 at 13:06:27
Ooh now I get it!

Ok will do.

Nop, everything is clear now thanks =D!


Report •

Ask Question