1

I am running a batch file that prompts the user to "enter name". If the user entered nothing and pressed enter, then I want to show the same window over and over again that says:

Enter Name:

The below code is not working:

@echo off
:myDosFunc
set /p name=Enter Name:
IF "%name%"=="" (
call:myDosFunc
) ELSE (
echo %path%
)
pause
GOTO:EOF

call:myDosFunc

I am new to batch, please help.

Lundin
  • 195,001
  • 40
  • 254
  • 396
par181
  • 401
  • 1
  • 11
  • 29

4 Answers4

2

This is similar to the w0051977 answer, but I belive it more closely matches your original intent.

@echo off
set "name="
:myDosFunc
set /p "name=Enter Name: "
if not defined name goto :myDosFunc
echo %path%
pause
exit /b

If you only want the prompt to appear once, no matter how many times the user presses Enter without entering anything, then you can use:

@echo off
set "name="
<nul set /p "=Enter Name: "
:myDosFunc
set /p "name="
if not defined name goto :myDosFunc
echo %path%
pause
exit /b
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

I think this is what you are after:

@ECHO OFF
:start
SET /P uname=Please enter your name: 
IF "%uname%"=="" GOTO Error
ECHO Hello %uname%, Welcome to DOS inputs!
GOTO End
:Error
goto start
:End

Use the GOTO keyword. Go here for more information: http://www.codeproject.com/Tips/123810/Get-user-input-from-DOS-prompt

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • 1
    More information (including the code) can be found o9n this website: http://www.codeproject.com/Tips/123810/Get-user-input-from-DOS-prompt – w0051977 Nov 25 '12 at 20:41
  • Hi w0051977, Please enter your name: should be shown once for any number of time Enter pressed.It should not be prompted below the enter pressed, repeatedly.Hope you are getting me...Thanks – par181 Nov 25 '12 at 21:01
  • @pkawar, if I am understanding you correctly then I believe that you want nothing to happen if the user presses enter without specifying a name? – w0051977 Nov 25 '12 at 21:07
  • @pkawar, you could replace goto Start on line 8 with: start cmd /c Test.bat. Test.bat is the name of your batch file and /c indicates it is in the C Drive root. This would force MSDOS to reopen a new window each time. – w0051977 Nov 25 '12 at 21:44
1

The problem is that you are trying to show the PATH without enclosing it with double quotes, and PATH can contain conflictive operators like ) then the batfile closes by an error.

PS: I've made other minor corrections in the code.

@echo off

:myDosFunc
Set "NAME="
set /p "name=Enter Name:"

IF NOT DEFINED NAME (
    call :myDosFunc
) ELSE (
    echo "%path%"
)
pause
GOTO:EOF
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • echo PATH may fail with quotes, without quotes, or both with and without. It depends on the contents of PATH. On most machines I work with, it works without quotes. The only guaranteed method is to use delayed expansion. – dbenham Nov 25 '12 at 20:56
  • I can assure you PATH can't fail using quotes, can't fail because you can't use conflictive characters when creating foldernames ( only ( and ) ), so any character is conflictive if you enclose the path like in my answer. – ElektroStudios Nov 25 '12 at 21:01
  • Not true. Some paths within PATH may already be quoted, others not. The following valid PATH definition cannot be expanded safely either with or without quotes: `PATH="c:\part1&part2";c:\this&that`. See [How to check if directory exists in %PATH%?](http://stackoverflow.com/a/8046515/1012053) for more info. In particular, look at point 7. – dbenham Nov 25 '12 at 21:13
0

A simple solution would be to add cls before you prompt for the input

:myDosFunc
cls
set /p name=Enter Name:
Bali C
  • 30,582
  • 35
  • 123
  • 152