Solved

Need to pass a user-supplied parameter to a batch file from a desktop shortcut.

Posted on 2003-11-03
10
645 Views
Last Modified: 2010-08-05
When a user clicks on an icon that represents a batch file, I'd like to prompt them for a parameter, which is then passed in to a batch file.  

I'm able to do this in Windows 98.  I create a shortcut to the batch file, and then put a question mark after the name of the batch file in the CMD Line field, ie:

C:\GPS\GPS.bat   ?

When the user clicks on the icon for this shortcut, they are prompted to type in a value, which the batch file uses.  In this specific case, it is Great Plains Accounting, and the user is selecting which company to open.

( If they were at a DOS prompt, they could type   GPS ARNOLD  or GPS BAKER and Great Plains would open the ARNOLD or BAKER company directories. This is what I'm trying to replicate.)

I went to do this on Windows 2000, and it doesn't work.  As we're migrating the users from Win98 to Win2000, I need to bring the old functionality forward.
0
Comment
Question by:studios
  • 2
  • 2
  • 2
  • +1
10 Comments
 
LVL 2

Assisted Solution

by:CCEtech
CCEtech earned 50 total points
ID: 9672982
Have you thought about using vbscript to send the parameters to the batch file? You could create an input box so a user can type in ARNOLD or BAKER to get the directories.
0
 
LVL 7

Accepted Solution

by:
Focusyn earned 50 total points
ID: 9673324
You can do it with variable commands in the batch file using the %0-%9 commands.  This will not prompt the user, but will enable the variable to be entered at the command line or hard coded in to the shortcut.  For Example:

I"m not sure which operation you are executing on the directories in question, but we'll use xcopy for an example.  If there are multiple directories, called Baker and Arnold, and you want to copy the files contained in the specified directory on a network drive mapped as X:\ to the user's C drive,  you can use the syntax;

at the command line you would type
GPS Arnold

and the batch file would look like this

xcopy x:\%1 c:\

the result would be xcopy x:\Arnold c:\

you can use the %0-%9 variables with any operation in the batch file.  

To do actual user prompted when a batch file is run is much more difficult, and probably not worth the trouble in this case, since I"m assuming you are going to be working within the same group of a few different choices.  I might suggest even that if the directory lists are not changing, the easier way would be to create batch files with the names already in them and name them Arnold.bat, Baker.bet etc.  Create them on a network drive and copy them down, or even make shortcuts to them, on the users' desktops, so that you are creating the shortcuts only once, and skip the vriables altogether.  

The following is an example of asking for user input within a batch file from Rob van der Woude's Scripting tutorial pages that stores it in a temporary text file, then calls that text file to read the variable from it, which is the quickest way I can find of usnig variable strings from user prompts.  The REM'd lines explain what is giong on throughout the example.

@ECHO OFF
REM * Ask for USeR INPUT and store it in variable USRINPUT
REM * Written by Rob van der Woude

SET USRINPUT=

REM * Turn on ANSI key translation (translate Enter
REM * key to F6+Enter sequence) and ask for input:
ECHO Enter one word only . . .

REM * Copy entered text to temporary file:
COPY CON %TEMP%.\~USRINP.TMP

REM * Turn off ANSI key translation and clear irrelevant screen output:
ECHO 

REM * Add empty line to temporary file. The empty line
REM * will be used to stop DATE asking for new date.
ECHO.>> %TEMP%.\~USRINP.TMP
ECHO.>> %TEMP%.\~USRINP.TMP

REM * Create a temporary batch file that will store the
REM * entered text into the environment variable USRINPUT:
TYPE %TEMP%.\~USRINP.TMP | DATE | FIND "):" > %TEMP%.\~USRINP.BAT

REM * Create more temporary batch files. Add
REM * more command line parameters if necessary,
REM * as in: ECHO SET USRINPUT=%%3 %%4 %%5 %%6 %%7 %%8 %%9>CURRENT.BAT
ECHO SET USRINPUT=%%3>CURRENT.BAT

REM * VOER.BAT and TYP.BAT are replacements for CURRENT.BAT for Dutch
REM * DOS versions; add your own language versions if necessary:
ECHO SET USRINPUT=%%6>VOER.BAT
ECHO SET USRINPUT=%%4>TYP.BAT

REM * This temporary batch file now sets the variable USRINPUT:
CALL %TEMP%.\~USRINP.BAT

REM * Display the result:
ECHO You typed: %USRINPUT%
ECHO.
PAUSE

REM * Finally, clean up the mess of temporary files:
FOR %%A IN (%TEMP%.\~USRINP.BAT %TEMP%.\~USRINP.TMP VOER.BAT TYP.BAT CURRENT.BAT) DO DEL %%A
0
 

Author Comment

by:studios
ID: 9673446
The batch file all ready uses %1 to accept the company name.

I'm trying to prompt the user for the value to pass in.

Since I posted, I've learned that I have to use the 16-bit MS-DOS prompt instead of the 32-bit MS-DOS prompt.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 7

Expert Comment

by:Focusyn
ID: 9673477
unless I misunderstand what you are trying to do, you can use %2 for the next value.  Like the batch file would be
[operation] %1 %2
where the user types at the command line
GPS [company name] [value]

and it will plug the values in in order starting with 1
0
 
LVL 1

Assisted Solution

by:iaind
iaind earned 50 total points
ID: 9673658
One option would be the use of CHOICE.  I think that it comes with the 2000 Resource Kit (I know that it came with the NT4 resource Kit).  Here is a link that explains it in more detail and some links (http://www.tburke.net/info/reskittools/topics/choice.htm)

It would allow you to do something like the following.

Echo Please select one of the following
Echo A - ARNOLD
Echo B - BAKER
Choice /c:AB Push the Key of your selection
If ErrorLevel 2 goto :Arnold
If ErrorLevel 1 goto :Baker
....

A book you might want to look at is "Windows NT Shell Scripting! by Tim Hill.  ISBN - 1-57870-04707.

Or as CCEtech above mentioned look at a scripting language.  My personal preference would be for Winbatch (www.winbatch.com) from WilsonWare.  It is a really powerful scripting language that is so much easier that VBscript
0
 

Author Comment

by:studios
ID: 9676775
The 16-bit COMMAND version does take a question mark and put a window-panel up that querries the user for an input, which then goes to the %1 position in the batch file.  

The choice option looks interesting, except that there are about 50 possible choices, and it would be high-maintenance to be changing the batch file, plus the 50 names would scroll off most screens.

The big batch file is quite a piece of work, and would require some more work to work in the multi-user environment, since I've got about 12 users starting up this process all during the day.  I've looked at WinBatch before, and think it is a great product, but I think I'd have to license about 15 copies, and haven't taken that up with the client.  

So, I'll stay with the in-elegant, but function 16-bit COMMAND environment (instead of the 32-bit CMD environment).
0
 
LVL 1

Expert Comment

by:iaind
ID: 9677129
ok.  winbatch does have an option where you can 'compile' the scripts into exe's so that they can run on PC's without Winbatch.  It is more expensive but if you work with a number of clients it can be extreamly powerful. The Compiler costs ~$500 and then you can run the compliled code on as many computers as you wish.  I have been using Winbatch for over 6 years and mush say that Wilson Ware support is second to none.

Another option could be 4NT from JPSoftware (www.jpsoft.com), but it would work out more expensive for 15 copies.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 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

14 Experts available now in Live!

Get 1:1 Help Now