Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

The Executing Application's Path: How do I determine it  In Winbatch?

Posted on 2006-05-23
32
Medium Priority
?
957 Views
Last Modified: 2008-01-09
I'm using Winbatch to execute other programs all in the same directory. We are allowing the user to choose the path. When the user executes one of these utilities from the chosen subdirectory, we would like to be able to find out what the path of the currently executing program is. All the other programs will be found in the same place, so once we have that information we are home free. We can imagine several other ways to accomplish this, but since we know how to do this in faciliteis other than Winbatch, we are hoping that we can do it directly in Winbatch. If we use another programming language and call such a program, if we want to put this auxillary program in the same path as the Winbatch modules, then we have exactly the same problem. Putting such a program in a fixed location such as the root directory of the C: drive solves the problem, but inelegantly as we see it.
0
Comment
Question by:BraveLad
  • 24
  • 5
  • 2
  • +1
32 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 16743623
In a normal batch file

echo %~f0

prints out the path of the calling batch file...I don't know if this helps you or not but:

     http://labmice.techtarget.com/articles/batchcmds.htm

has a lot on the subject

0
 

Author Comment

by:BraveLad
ID: 16743693
Actually I found a way to discover the path of an initiating process. It is the path of the CURRENTLY EXECUTING PROCESS that I am interested in.
0
 
LVL 25

Expert Comment

by:SStory
ID: 16745516
I guess I am confused, if you execute a process from your batch file, how would you not know which one you executed?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BraveLad
ID: 16746627
If an operator executes a program, an .exe file by the way, not a ‘.wbt’ script interpreted by the Winbatch Interpreter, then we could ask the operator from whence he executed the program all right, but I think the operators of programs expect us to be more omniscient than that. I did this so many years ago in Turbo Pascal that I can't remember. I just want to do it in Winbatch!

There was a system variable ProgDir that was available if you had a 'Uses DOS, or 'Uses System' statement I faintly remember. Let me give you a link that answers the question for c# and the .NET environment. I just want the answer for Winbatch. It is like having an implicit parameter.

The operator manages to execute a program, wherever its executable is in the secondary storage world, and the program just orients itself by finding out from where it was executed. If you anthropomorphize the matter, just like the basic 'Hello World!" function this is the basic, "Where in the World did I come from” question and of course there is the name of the currently executing program, which would answer the burning question, "Who in the World am I?"

Here is the link for the answer for ‘.NET’ and C#,

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncfhowto/html/HOWTOExecutingAppPath.asp 

We were NOT executed by a batch file. We were executed by an operator of Windows, using whatever means they chose. The puzzle is, where was the executable (an instance of which is running now) to be found?
0
 

Author Comment

by:BraveLad
ID: 16746642
The puzzle is, where WAS the executable (an instance of which is running now) to be found? = The puzzle is, where IS the executable (an instance of which is running now) to be found?

0
 

Author Comment

by:BraveLad
ID: 16746685
Winbatch has a compiler. We use it to produce .exe's for those unfamiliar with Winbatch.
0
 

Author Comment

by:BraveLad
ID: 16746779
For test purposes, it is very handy to just execute one of an interrelated bunch of programs and expect to find the rest in the same nest. We could of course do this in a number of ways. We could always do it from one place and hard code it. We could put the information in an 'initialization file' or 'configuration file' or the registry, but we chose to exploit the simple fact of WHERE THE CURRENTLY EXECUTING PROGRAM WAS EXECUTED FROM that should be easily knowable by the program currently executing. The Operating System knows this information. If it wishes, it can tell any program.

In the Microsoft MSDN article, the title was, "HOW TO: Determine the Executing Application's Path". That’s all we want to know, the Path of the Executing Application’s Path. We are content with a hard-coded or configured name for the ‘Executing Application,’ we just want to dynamically determine its Path!
0
 

Author Comment

by:BraveLad
ID: 16746845
In the Delphi context, here is another article that answers my question:

http://www.chami.com/tips/delphi/111496D.html
Find out where your program is running from
Sometimes it's necessary to find out the path to the executable file of your program, in order to find out where to get the rest of the files from. In most cases, the current directory would be the directory where your programs are executed from, but this method would not work if your program path is listed in the PATH or if an user executes your program from another directory.
 
Here's how to get the directory where your programs is located:
 
  MessageDlg(
    'program path =  ' +
    ExtractFilePath( Application.ExeName )
    , mtInformation, [mbOk], 0 );

Here is yet another article for another context that answers this question:

http://www.blitzbasic.com/Community/posts.php?topic=47755
0
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 16747201
Hi,

An example for a WinBatch script file (WBT) would be:

run ("d:\path\tcmd.exe","")
channel = DDEInitiate("TCMD32", "Execute")
if channel != 0
result = DDEExecute(channel, '*dir /w')
else
Message("Error", "DDEInitiate failed!")
endif
exit

The main information I'd like to pass is the point were you determine the "CHANNEL" (if channel != 0 ... else) ... you might abstract your measures from there ...


Best regards,
Raisor
0
 

Author Comment

by:BraveLad
ID: 16747279
This is not a server application. Try something more generic.
0
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 16747315
Hi,

... thanks for your quick response to my suggestion ... and sorry for not being as generic as you've expected!

May I ask you to place the pieces you've already developed (the inelegant ones you were writing about) and a structured description about OS and PL?


Best regards,
Raisor
0
 

Author Comment

by:BraveLad
ID: 16747439
WIL (Windows Interactive Language) is the PL. Any current Windows OS that Winbatch WIL runs on or which its compiled output runs on. For a very non-technical question, how technical does the description need to be?
0
 

Author Comment

by:BraveLad
ID: 16747454
P.S. If I had the single line that no doubt answers this question, I wouldn't be asking it. We sill not be producing any code for 'inelegant solutions.' Am convinced the facility exists. If this was Borland Pascal the answer would be,  "It's in the ProgDir system variable, Dummy!" 'Dummy' is optional, but it makes me feel at home.
0
 

Author Comment

by:BraveLad
ID: 16747456
We sill = We will
0
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 16747473
Hi,

... BraveLad, having had a look at SStory's intention ... and the way your treating my need of getting to know "some basics" ... your question seems to me like a question that finally can only be answered by yourself ... so please accept my excuse that I've interupted you solving your own question!


Good luck!
Best regards,
Raisor
0
 

Author Comment

by:BraveLad
ID: 16747481
Raisor. The basics were Winbatch and Windows. anything more is just education on Winbatch. I'm not the expert.
0
 

Author Comment

by:BraveLad
ID: 16747491
I see to ask a question here you not only need to be stupid and ignorant, but patient too! I have no problem with the first two!
0
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 16747515
BraveLad,

... these were the first 68 minutes I've spent at EE since a month or so ... and you kind of guy make me feel sick for another month or two ...

... you might feel good with this ... for people who are in real need of help or assistance this might be a mess!


Best regards,
Raisor
0
 

Author Comment

by:BraveLad
ID: 16747614
Raisor, shave with this. I entered college at age 16 two years before you were born. You hold yourself out as a consultant a form of businessman. Did you ever hear the old saying, "The Customer is always right?" Completely untrue of course, just like, "All men are created equal," but things work more smoothly if we ACT as if they were true.

You don't have to get huffy if you don't know beans about Winbatch and you suffer from the inclination to make things more complex than they have to be!
0
 

Author Comment

by:BraveLad
ID: 16747643
You know now that Raisor has got me riled up, there is a great defect in this Forum. What if you find the answer to your own question? Wouldn't it be being a good citizen to give out the answer and not award all the points or even no points at all? But NOOO! If you do that, the question goes away and no one benefits from the answer unless it is delivered in a way that flatters "Experts" Exchange or its putative experts! So I will persist in asking and clarifying the question. Raisor appears to have understood this simple question at least, but wants to put it off in an obscure little corner of Winbatch, when it is truly elementary and can be answered by scanning the quick reference, function list of the Winbatch language WIL
0
 

Author Comment

by:BraveLad
ID: 16747655
I heard once that a consultant is paid for what they know, not what they don't know. Of course contract workers masquerading as consultants is the norm. Everyone it seems likes an inflated title.
0
 

Author Comment

by:BraveLad
ID: 16747658
And oh yes. I pay good coin of the realm to good consultants. Just think of answering this question as a tryout.
0
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 16748443
Hi,

Do you feel better now BraveLad?

Behaving like you were King Arthur might help you in this (your) life ... to others it might not be understandable ... like to me!

As for your question, my day starts right know (being a poor consultant at 6:30 AM CEST) after some three hours of sleep I can not spend time riht now ... if no other expert solves this during the day I'll have another look at it in the evening!

Just a last word on your "service-dream" you seem to be dreaming ... I'm independend so I can't share your vision! I only help where it makes sense -> no sense, no help ... no troubles ... yep, no money too ... but it's always my who decides!


Best regards,
Raisor
0
 

Author Comment

by:BraveLad
ID: 16748798
Yes, actually I do! Thanks for asking. I find a bout of invective quite cathartic, purgative, or whatever. I use them in lieu of cholesterol reducing drugs to clear out my arteries.

Your odds are good the question will still be around by the time you get to it.

I think it was the "structured description about OS and PL" that put me off. You wouldn't have a form handy for that would you?
0
 

Author Comment

by:BraveLad
ID: 16749676
Well, Raisor, kind of you to think of me as not merely a Knight of the Table Round, but its founder. However, I am only a humble member of a tabular organization, an association of folk who are defined as "Those who want to have their heads examined" by their founder.

However, I am more of Hotspur's temper than King Arthur’s.

"I am not yet of Percy's mind, the Hotspur of the North; he that kills me some six or seven dozen of Scots at a breakfast washes his hands and says to his wife - `Fie upon this quiet life! I want work' `O my sweet Harry,' says she `how many has thy killed today?'.... `some fourteen,' an hour after; `a trifle a trifle." From HENRY IV, PART1, ACT 2, SCENE 4

Those Caledonians are a tough breed! Not only difficult to eliminate, but tolerant to a fault. After all, they will admit any to their number who have the good sense to want to be a Caledonian!
0
 

Author Comment

by:BraveLad
ID: 16758991
Raisor,

One other thing.

In the below code snippet I understood assignment, variables, constants, if, else, endif, exit, message, run and punctuation.

"run ("d:\path\tcmd.exe","")
channel = DDEInitiate("TCMD32", "Execute")
if channel != 0
result = DDEExecute(channel, '*dir /w')
else
Message("Error", "DDEInitiate failed!")
endif
exit"

I didn’t understand channel, DDEInitiate, and DDEExecute. I couldn’t find anything defining DDE in the Winbatch WIL Reference, so I guessed that it meant Dynamic Data Exchange, a facility similar to OLE (Object Linking and Embedding). I couldn’t find a definition of ‘channel’ for this context either.

So, I skimmed the DDE section very quickly and concluded that this was like a handle, except it was for the link between processes that exchange data dynamically via DDE facilities. I saw in some error messages that ‘channel’ also was relevant to OLE. This made sense, since I knew DDE and OLE to be first cousins.

I also did not understand the significance of “TCMD32”. Would it be 32 bit Terminal Command or some such?

So I looked it up on the web. It said.

“You can also use TC as a DDE server and send commands to it from another application. To do so, use an application or server name of TCMD32, and a topic name of Execute.”

So, TC or TCMD32 is a DDE server instance for some context. The rest of the article read.

“The message can be any valid command that you could enter on the Command line: any alias, internal command, batch file, or external command. To send more than one line in a single DDE string, separate the lines with carriage return and line feed characters.
 
When using TC as a DDE Server you should start TC, execute the desired command, and exit TC, using the DDE commands of the "client" application. TC's DDE server feature is not intended to be used with a copy of TC running at the prompt; attempting to do so may result in unusual displays or display of the prompt in an incorrect location.
 
For example, if you use Microsoft Word you could use the following Visual Basic for Applications (VBA) fragment to send a DIR /W command to TC from within Word:
 
Dim TCMDChannel as Long
Shell ("d:\path\tcmd.exe")
TCMDChannel = DDEInitiate("TCMD32", "Execute")
DDEExecute TCMDChannel, "dir /w"
DDETerminate TCMDChannel
 
A similar sequence for a WinBatch script file (WBT) would be:
 
run ("d:\path\tcmd.exe","")
channel = DDEInitiate("TCMD32", "Execute")
if channel != 0
result = DDEExecute(channel, '*dir /w')
else
Message("Error", "DDEInitiate failed!")
endif
exit
 
You could use the same approach to execute a batch file or any other TC command from within Word or WinBatch, and similar approaches are available in other applications which offer DDE support. Consult your application manual for complete details.
 
In most cases, when you use TC as a DDE server you will want to redirect output from the commands you execute, because output on the TC window is not likely to be useful to the client program which invokes a command.”

Well, Howdy! There is your unexplained example! It appears to be merely a way to get a directory up so that the nincompoops who don’t know where they are can find out!  Well read the question and what I was trying to accomplish. I want an AUTOMATIC means for the currently executing PROGRAM to determine its location, NOT THE OPERATOR to do so!

So, it appears you understood the question, but didn’t read the motivation for it, which I stated.

The material which you excerpted unexplained came from http://www.jpsoft.com/help/index.htm?dde.htm .

JP Software’s Overview Page states.

“Welcome to our help! We have designed this material to accompany our current Windows products:  4NT and Take Command (usually abbreviated TC). 4NT and Take Command are designed for Windows 2000, Windows XP, and Windows 2003.  For additional information, see Supported Platforms.
 
Each of these programs is a command interpreter or shell. That means that they respond to commands you type at the prompt. They are also able to process batch files containing sequences of commands.
 
4NT is designed as a powerful alternative to CMD.EXE, the default command processor in Windows 2000 / XP / 2003.
 
Take Command is a unique graphical command processor combining the power of the command line with the convenience of a graphical user interface (GUI).”

It appears that for a limited set of Windows platforms, that Take Command is an alternative and more to Winbatch. But, my use of Winbatch is a given, unless you would like to advocate my abandoning it in the clear.
0
 

Author Comment

by:BraveLad
ID: 16759007
Let me repeat,

"If an operator executes a program, an .exe file by the way, not a ‘.wbt’ script interpreted by the Winbatch Interpreter, then we could ask the operator from whence he executed the program all right, but I think the operators of programs expect us to be more omniscient than that. I did this so many years ago in Turbo Pascal that I can't remember. I just want to do it in Winbatch!

There was a system variable ProgDir that was available if you had a 'Uses DOS, or 'Uses System' statement I faintly remember. Let me give you a link that answers the question for c# and the .NET environment. I just want the answer for Winbatch. It is like having an implicit parameter.

The operator manages to execute a program, wherever its executable is in the secondary storage world, and the program just orients itself by finding out from where it was executed. If you anthropomorphize the matter, just like the basic 'Hello World!" function this is the basic, "Where in the World did I come from” question and of course there is the name of the currently executing program, which would answer the burning question, "Who in the World am I?""
0
 

Author Comment

by:BraveLad
ID: 16759013
So it is any Windows 98 platform and above and Winbatch that is the context of my question to clear that up.
0
 
LVL 1

Accepted Solution

by:
sagarpawar earned 2000 total points
ID: 16759153
BraveLad,

Tyr to use DirScript()
I have used that.

DirScript() returns the full path of the current program.
0
 

Author Comment

by:BraveLad
ID: 16759179
Dear sagarpawar,

I tried that out and it worked. I also tried out IntControl(1004, 0, 0, 0, 0) and that returned the path AND name of the currently executing Winbatch program. Thanks!

You must have read the WIL Reference Manual! Reading the manual! What a concept. I highly recommend it--to myself as well as others for that matter. I will award you full points for this correct answer in a few moments.
0
 

Author Comment

by:BraveLad
ID: 16759583
Well, that was nice. A simple specific answer to a simple specific question. What do they call it when you cruise for points by doing a little lightweight web research and copy that material unattributed in answer to a question? Now that saves you from doing your own web research and I appreciate that, but is it good practice to excerpt material without attribution? It certainly isn't as valuable. Being able to go to the context can produce more information. When I realized that in this case that I was dealing with an example of bringing up a directory for operator perusal, that the proffered material was irrelant to my needs. But I just tripped across the source in the process of trying to understand the terms that were used in the code sample imposed on me!
0
 

Author Comment

by:BraveLad
ID: 16759591
irrelant = irrelevant
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Make the most of your online learning experience.
Introduction to Processes
Screencast - Getting to Know the Pipeline

864 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