?
Solved

Batch file with command box

Posted on 2013-01-23
8
Medium Priority
?
479 Views
Last Modified: 2013-05-30
Good day

Does anyone know how to create a batch file that brings up a a command box with "yes" or "no"  answer, and depending on your answer must perform specific command, i.e you have a batch file and when it runs brings up a box asking do you want to copy or cut, if you select copy it must run the "copy" batch file, if you select cut it must run the "cut" batch file.

Urgent help needed please
0
Comment
Question by:iamdieter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 38809338
Try this:
@echo off
setlocal
:InputLoop
cls
echo.
echo Do you want to copy or cut?
set /p Input=Please enter 'copy', 'cut', or 'cancel': 
set Choice=
for %%a in ("copy" "cut" "cancel") do (
	if /i "%Input%"=="%%~a" set Choice=%Input%
)
if "%Choice%"=="" goto :InputLoop
if /i "%Choice%"=="cancel" (
	echo Operation cancelled.
	goto :eof
)
if /i "%Choice%"=="cut" (
	echo Cutting ...
	call cut.bat
	goto :eof
)
if /i "%Choice%"=="copy" (
	echo Copying ...
	call copy.bat
	goto :eof
)

Open in new window

0
 

Author Comment

by:iamdieter
ID: 38809378
Thank you, it works. any posibility of making it a box where users can click instead of type?
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 38809411
Examples: Create batch files in dos  eg. helloworld.bat and GoodBye.bat

We can do this by using powershell:

Save the following in  cutCopy.ps1


$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to Copy or Cut Operation?", `
0,"Copy or Cut Files",4)
If ($intAnswer -eq 6) {
  cmd /c c:\test\helloworld.bat
} else {
  cmd /c c:\test\GoodBye.bat
}



The above code calls the batch files depending on selection
Try to run it
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 2

Expert Comment

by:sarvjita
ID: 38809415
@ECHO OFF
REM - LABEL INDICATING THE BEGINNING OF THE DOCUMENT.
:BEGIN
CLS
REM - THE BELOW LINE GIVES THE USER 2 CHOICES (DEFINED AFTER /C:)
CHOICE /N /C YN /M "TYPE 'Y' IF 'COPY' AND 'N' FOR 'MOVE' (Y or N)"%1
REM - THE NEXT TWO LINES ARE DIRECTING USER DEPENDING UPON INPUT

IF ERRORLEVEL == 2 GOTO TWO
IF ERRORLEVEL == 1 GOTO ONE

GOTO END
:TWO
MOVE /Y [PATH]<SOURCE FOLDERNAME> <DESTINATION FOLDERNAME>

GOTO END
:ONE
COPY <SOURCE> <DESTINATION>
:END


(AND SAVE AS "SOME_NAME.BAT")
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 38809428
To run the given code,

Start-> Run -> Powershell  (Enter)

Copy paste the following code:

$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to Copy or Cut Operation?", `
0,"Copy or Cut Files",4)
If ($intAnswer -eq 6) {
  cmd /c c:\test\helloworld.bat
} else {
  cmd /c c:\test\GoodBye.bat
}
0
 
LVL 85

Accepted Solution

by:
oBdA earned 1500 total points
ID: 38809470
A batch file is a console application and has no way to interact with the mouse.
You'll have to resort to a VB script or something else that supports GUI elements.
Try this VB script below (save as Whatever.vbs):
strCommandCopy = "C:\Temp\vopy.bat"
strCommandCut = "C:\Temp\cut.bat"
strPrompt = "Default operation is 'Copy'; select 'Yes' to choose this." & vbCRLF & "Choose 'No' to 'Cut'."
intInput = MsgBox(strPrompt, vbYesNoCancel, "Select operation")
If intInput = 2 Then
	Wscript.Quit
End If
Set objShell = CreateObject("Wscript.Shell")
If intInput = 6 Then
	objShell.Run strCommandCopy, 1, False
End If
If intInput = 7 Then
	objShell.Run strCommandCut, 1, False
End If

Open in new window

Note that you can hide the console windows of the batch script by replacing the 1 in the "objShell.Run" line with a 0, but do so only after you're sure that the scripts do what you want.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 38809649
A few techniques discussed here you might want to look at:

http://www.robvanderwoude.com/usermessages.php

In addition, there's a nice little utility called WBAT at the link below that can do this, and more:

http://www.horstmuc.de/ui.htm

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38811332
Another way I use for things is a dynamically created VBScript within the batch file:

Steve

@echo off


call :askYesNo "Please choose Yes or No" "Title for box"
echo %answer%
if %answer%==6 echo You chose Yes
if %answer%==7 echo You chose No
if %answer%==2 echo You pressed cancel or the X


exit /b
REM call :AskYesNo "Message" "title"
:AskYesNo
  set answer=
  set vbs="%temp%\getanswer.vbs"
  echo wscript.echo MsgBox^("%~1",vbYesNoCancel,"%~2"^)>%vbs%
  for /f %%a in ('cscript //nologo %vbs%') do set answer=%%a
exit /b

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question