Variable able to run various apps from same location

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aldistuck
Posts: 4
Joined: 23 Dec 2010 01:23

Variable able to run various apps from same location

#1 Post by aldistuck » 23 Dec 2010 01:37

Hello all,
Im a first time poster in need of help.
I have been searching this site and other but am unable to find anything that will trigger what I need to know.

Heres what I am trying to do.

I want to start a batch file that prompts me to enter a command then run the associated application. The command needs to be a variable so when I am prompted I can type any executable I want such as winword.exe or powerpnt.exe. Then I need that variable to plug into the executable path.

Example:

set /p %1=Enter command:
if "%1"=="%1" call :run

:run
"c:\program files\microsoft office\office14\%1"

So after command I want to be able to type winword.exe and have it replace %1 in the executable path and start Word.

I have tried many variations but with no sucess.

Thank you for any help you can give.

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Variable able to run various apps from same location

#2 Post by ChickenSoup » 23 Dec 2010 07:34

You basically have it

Code: Select all

set /p theprogram=Enter command:
REM check that it is an executable.
echo %theprogram%:~-4%
if "%theprogram:~-4%"==".exe" (
   Echo.Running Program %theprogram%...
   %theprogram%
) else (
   Echo.Not a valid executable.
)

The real problem is that you need to know the full path to the executable unless you put it in the PATH statement. So, in this example, notepad.exe and write.exe will work but, winword.exe and excel.exe will not.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Variable able to run various apps from same location

#3 Post by aGerman » 23 Dec 2010 09:42

Because c:\program files\microsoft office\office14 is not part of the PATH variable you would need the full name. The START command could work without path and it's probably a better way (because it makes no sense to keep the batch window open).

Code: Select all

  start "" /max "%theprogram%"


And (@ChickenSoup) what happens if the user hits the Enter button without inputting something? And what happens if the extension is .EXE instead of .exe? :wink:

Regards
aGerman

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Variable able to run various apps from same location

#4 Post by ChickenSoup » 23 Dec 2010 10:09

Good points. I always miss some of the obvious. If they enter nothing, it will return "Not a valid executable."
So how does this look?

Code: Select all

set /p theprogram=Enter command:
REM check that it is an executable.
if /i "%theprogram:~-4%"==".exe" (
   Echo.Running Program %theprogram%...
   start "" /max "%theprogram%"
) else (
   Echo.Not a valid executable.
)


By the way, I just found this forum the other day and love it! I have had a love for batch scripting for years and have already learned a ton more from this site! :D

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Variable able to run various apps from same location

#5 Post by aGerman » 23 Dec 2010 10:33

Yeah, looks much better.
My proposal:

Code: Select all

@echo off
:loop
set "theprogram="
set /p "theprogram=Enter command: "
if not defined theprogram goto loop
start "" /max "%theprogram%" ||goto loop


IMO no need to check if the file is an executable because START will do it for you. BTW try to enter e.g. excel (without .exe). Works for me :wink:

Regards
aGerman

aldistuck
Posts: 4
Joined: 23 Dec 2010 01:23

Re: Variable able to run various apps from same location

#6 Post by aldistuck » 23 Dec 2010 13:14

That worked out nice even though I dont understand how it works just yet.
Thank you ChickenSoup.

I should explain though and maybe ask if there is someway to make the batch file more versitile? What I mean is that I was hoping with this batch file to use it in other situations with some slight modifications.

An example of a different command were I am still prompted for input but the input is different and the variable in a different place in the command string.

Example:

set /p %1%2%3=Enter Operation:

ECHO "CN=%1,OU=%2 %3,DC=ACMEDOM1,DC=COM" -pwd * -samid %1 -upn %1@ACMEDOM1.COM

If I execute this batch file without the "set" function It will copy the input I want into the percent variables. Lets say the batch file is named "test.bat"
If I type test Username Domain Users the output will echo replacing all %1's with Username, %2 with Domain and %3 with Users.

I don't know how to plug the "set" function into the command string.
This is my main goal and I cant find my way. I get many errors such as goto cannot run or no executable found.

Thank you for any assistance you can lend, and thank you again for the first batch file resolve.

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Variable able to run various apps from same location

#7 Post by ChickenSoup » 23 Dec 2010 15:51

I am confused to what you are getting at.

If you have a batch file named test.bat with the following code:

Code: Select all

echo.Variable1 %1
echo.Variable2 %2
echo.Variable3 %3

and you type "test.bat one two three", your output will be

Code: Select all

Variable1 one
Variable2 two
Variable3 three

That is where you use %1, %2, etc.

Or are you looking at something conditional? That is, if you enter "winword.exe" for the "operation", it then asks a specific set of questions on that?

aldistuck
Posts: 4
Joined: 23 Dec 2010 01:23

Re: Variable able to run various apps from same location

#8 Post by aldistuck » 23 Dec 2010 17:03

Originally I was and am still working on two seemingly different batch files.
However the way I was trying to make them work seemed similar so I just posted about the one for my office apps, which you made work great but in doing so it made me see that my other batch file could not be made the same way. My other bacth file is only similar I guess in that I want it to ask for information that will will then plug into the command string.

my second batch file is using the dsadd user command.

dsadd user "cn=%1,cn=%2,dc=acmedom1,dc=com" -pwd * -samid %1 -upn %1@ACMEDOM1.COM

If I just type in a prompt "test.bat Username Users" , then Username replaces %1 and Users replaces %2.

I would like to be prompted to enter a username then enter a group, username being %1 and group being %2, and then have those names plug into the above command string.

To my eyes my initial office batch file seemed similar but now I dont know.
Also, I dont know how to deal with the %2 if I have a group name that is made up of two words such as group name.

I hope that makes more sense, Im sorry for the confusion.
Thank you,

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Variable able to run various apps from same location

#9 Post by ChickenSoup » 27 Dec 2010 08:21

This makes much more sense.
Well, if you want it to prompt for Enter user name and Enter group name, it will look something like this:

Code: Select all

set /p strusername=Enter the user name: 
set /p strgroup=Enter the group name:
dsadd user "cn=%strusername%,cn=%strgroup%,dc=acmedom1,dc=com" -pwd * -samid %strusername% -upn %strusername%@ACMEDOM1.COM


if you want it to run like test.bat %1 %2, you would have something like this:

Code: Select all

for /f "useback tokens=*" %%a in ('%1') do set strusername=%%~a
for /f "useback tokens=*" %%a in ('%2) do set strgroup=%%~a
dsadd user "cn=%strusername%,cn=%strgroup%,dc=acmedom1,dc=com" -pwd * -samid %strusername% -upn %strusername%@ACMEDOM1.COM

If your group has a space in it you would run the command like this:
test.bat user "my group"
the 2 FOR loops will strip the quotes from the strings.

aldistuck
Posts: 4
Joined: 23 Dec 2010 01:23

Re: Variable able to run various apps from same location

#10 Post by aldistuck » 02 Jan 2011 13:08

Thats excellent!

Thank you very much for your help.
This will give me much to edit with.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Variable able to run various apps from same location

#11 Post by avery_larry » 11 Jan 2011 12:06

If you have a definite set of programs that you care about:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set word=c:\program files\microsoft office\office12\winword.exe
set excel=c:\program files . . . \excel.exe
set notepad=c:\windows\notepad.exe
rem you can add others here.

set program=%1
if not defined program set /p program=Enter the program:
!%program%!

Post Reply