We help IT Professionals succeed at work.

Interactive bat file

prlit
prlit asked
on
471 Views
Last Modified: 2008-02-26
I'm wondering if it's possible to have user added fields or comments interact with my bat file. For example: if I want my bat file to create a new folder:

echo Please type the name of the new folder
USER INPUT
md c:\USER INPUT\


does that make sense? Thanks!
Comment
Watch Question

A SyscokidRetired bureaucrat/desktop support
CERTIFIED EXPERT

Commented:
This is not the most elegant solution but  you could simply have the bat file say

if _%1_==__ goto NOPARAM

md %1

:NOPARAM
@echo.
@echo.  You didn't specify the folder to create!
@echo.
@pause

:END

And when you run the bat file you would type

mybat.bat "C:\user input"

Author

Commented:
yea, there's no "parse" commad or anything like that eh?
Can't you use a script?
A SyscokidRetired bureaucrat/desktop support
CERTIFIED EXPERT

Commented:
Prlit, I suggest you post a 20 point poster question in the MS-DOS topic area.

Author

Commented:
what is a 20 point poster question?

Use a script? I'm trying to write the script.
A SyscokidRetired bureaucrat/desktop support
CERTIFIED EXPERT

Commented:
Post a question in MS-DOS

Title:  Batch File Help Needed
Text: Just a link to this question.

Assign 20 points (minimum allowed) to it.  WWhen this question here is resolved, ask Community Support to delete the pointer and refund your points.
Sorry I should have been more specific...I meant a vbscript.....
Erik BjersPrincipal Systems Administrator

Commented:
You can use system variables to do this

To get user input;

set /p variablename = [Prompt to user]

You can then refer to these variables by using %variablename% in your .bat file.  The varaibles will be cleared once the command session that the batch ran in is closed.


i.e.

test.bat
@ECHO OFF
set /p messagetxt = [What message would you like to send]
ECHO Your message is: %messagetxt%

Author

Commented:
Well, I tried this:

@ECHO OFF
echo What is your name?
set /p messagetxt =
ECHO Your name is: %messagetxt%
pause

but I don't get anything back.
Erik BjersPrincipal Systems Administrator

Commented:
first problem

set /p messagetxt =

should be
set /p messagetxt = What is your name?

I'm also niticing that echo %variable% seems not to work but if you do a set variable name it will show the input found
A SyscokidRetired bureaucrat/desktop support
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
" It should be

@ECHO OFF
set /p messagetxt = What is your name?
ECHO Your name is: %messagetxt%
pause

It returns What is your name?"

But we want it to return the name, not "What is your name?"
Erik BjersPrincipal Systems Administrator

Commented:
When it returns what is your name enter some text then press enter

ECHO Your name is: %messagetxt%
may not return results so replace it with set messagetxt
A SyscokidRetired bureaucrat/desktop support
CERTIFIED EXPERT

Commented:
Frankly, I don't see how this prompts for input.  But I'm not a DOS genius obviously.
A SyscokidRetired bureaucrat/desktop support
CERTIFIED EXPERT

Commented:
Still, I don't see how this prompts for input.
Erik BjersPrincipal Systems Administrator

Commented:
Info on set command... it works

http://www.computerhope.com/sethlp.htm
Technology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
Also, you need to be clear: are you working with DOS or an NT/2000/XP/2003 Command prompt?  The syntax is different in DOS (and depends on what version of DOS) as well as different for 9x/Me.
Erik BjersPrincipal Systems Administrator
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
@echo off
echo Please type the name of the new folder
set /p UserInput=Folder Name:
md "%UserInput%"
Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
It DOES matter -

SET /p MessageTxt = Prompt
will prompt you, but the variable will be:
%messagetxt %
NOT
%messagetxt%

That's why the Echo is failing.
Erik BjersPrincipal Systems Administrator

Commented:
ahh thank you leew that does make sence OK I stand corrected the space does matter leave it out.

GuruGary
@echo off
echo Please type the name of the new folder
set /p UserInput=Folder Name:
md "%UserInput%"

This will work but you do not need the echo statment before hand as the set /p command will prompt the user.

I would do this
@echo off
set /p UserInput=Please enter the Folder Name:
md "%UserInput%"
Erik BjersPrincipal Systems Administrator

Commented:
leew, I just tried it with out the space, and you were right, then looking back at my scripts it appears that I alredy learned that once a long time ago and just forgot it :-) thanks for the correction it helps me out as well

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.