Solved

Batch file with command box

Posted on 2013-01-23
8
468 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 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 53

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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