Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch file with command box

Posted on 2013-01-23
8
Medium Priority
?
483 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
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 59

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

885 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