Solved

Batch file with command box

Posted on 2013-01-23
8
470 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 84

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 84

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 54

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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