Solved

Batch file with command box

Posted on 2013-01-23
8
463 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 83

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 18

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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 18

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 83

Accepted Solution

by:
oBdA earned 500 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 51

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now