Help - Search - Members - Calendar
Full Version: Get absolute filename of a running .bat file?
Hydrogenaudio Forums > Misc. > Off-Topic
gasmann
Hi all,
I want to set an environment variable inside a .bat file to the absolute filename of this .bat file.

Example:

filename: c:\a_dir\mybatch.bat

inside this I want to run something to set an environment variable, let's say batname, so that I then can run:

CODE
echo %batname%


This should give me

CODE
c:\a_dir\mybatch.bat


Is there any way to do this while staying in pure dos compatible .bat?

Thanks for any tips! biggrin.gif

EDIT: It would also be okay (or maybe even easier) to just write the drive and dir in a variable.
(In this example c:\a_dir) I know the name of the batch file, anyway.
56Nomad
Try this in your batch file:
set batname="c:\mydir\myfile.bat"

Then, from a prompt, issue the SET command. You should see the variable batname and the value you gave it.
Synthetic Soul
%0 refers to the running script. Therefore:

ECHO %0

or

ECHO %~0

... if the path has spaces but you don't want quotes.

QUOTE(gasmann @ May 6 2006, 15:32) *
EDIT: It would also be okay (or maybe even easier) to just write the drive and dir in a variable.
(In this example c:\a_dir) I know the name of the batch file, anyway.
In this case, use:

ECHO %~dp0
woody_woodward
I was able to get this to work by redirecting through a temp file.

ECHO %~f0>%TEMP%\X.TXT
SET /P BATNAME=<%TEMP%\X.TXT
DEL %TEMP%\X.TXT
ECHO %BATNAME%





For some reason I could not get a simple pipe to work... Anyone know why this doesn't work????

ECHO %~f0|SET /P BATNAME=
ECHO %BATNAME%
Synthetic Soul
I don't understand.

SET BATNAME=%0
ECHO %BATNAME%

... works fine for me.
woody_woodward
QUOTE(Synthetic Soul @ May 6 2006, 12:32) *

I don't understand.

SET BATNAME=%0
ECHO %BATNAME%

... works fine for me.

Agreed, but I believe the gentleman was looking for a fully qualified pathname.

Woody
Synthetic Soul
I'm sorry Woody, I'm still confused.

Testing both scripts gives me the same result (if I use %~0 rather than %0 with a path with spaces).

I've little doubt that yours is accurate, but could you explain why please?

I assume this revolves around the definition of "fully qualified pathname", but I'm failing to see the difference.
gasmann
Thanks for your good tips, but this still didn't solve my problem. %~0, %~d0 as well as %~dp0 doesn't work in plain dos. It just gives me ~0 in DOSBox or %~0 in FreeDOS. Seems like this is an WinNT only solution sad.gif

What I want to do: I have a partition called "DOSData" formatted as 1gb fat16 containing old dos games and apps. For this, I want a menu system for starting the individual apps and games (I'm using wbat for this). This menu system should work anywhere where dos is supported: Be it Win9x, WinNT, old MS-DOS, FreeDOS, DOSBox, ...

However, I've got the problem that I can't easily find the files needed for the menu (e.g. if menu.bat is run from a different drive (like m:\menu), the menu can't find anything because it assumes everything on the current drive).

For this, I wanted to set an environment variable called %DOSDATAL% containing the drive letter the DOSData partition has. And this is my problem, I don't know how to do this that it works anywhere (%~d0 is working on NT, but not in DOS).

Any other suggestions?
Thanks so far wink.gif
woody_woodward
QUOTE(Synthetic Soul @ May 6 2006, 22:27) *

I'm sorry Woody, I'm still confused.

Testing both scripts gives me the same result (if I use %~0 rather than %0 with a path with spaces).

I've little doubt that yours is accurate, but could you explain why please?

I assume this revolves around the definition of "fully qualified pathname", but I'm failing to see the difference.

Synth, I suspect that differnet versions of DOS (or cmd.exe or command.com) yield different output. When I said pathname I meant something like "C:\BATS\BATNAME.BAT" as opposed to just the file name, e.g. "BATNAME.BAT" I'm sure the results we're both getting are 'normal.'

Mr. Gasmann, you are correct. My suggestion does require WinNT, or Win2000, or WinXP. For DOSBox or FreeDOS, I know of no other solution but to hard-code the pathname within the BAT file. Sorry

Woody
Synthetic Soul
Thanks for the clarification Woody.

Yeah, on Win2K SP4 %0 does give me a fully qualified path.
gasmann
Well, and that's the problem: I can't hardcode it because the drive letter varies. In DOSBox, it's usually C:; in FreeDOS, it's H:; while in Windows XP it's M:.

That's really a pain that there is no possibility at all to get this working. I guess I'll have to require menu.bat to be started from the drive it resides on, then sad.gif

Thanks anyway for your tips wink.gif
woody_woodward
QUOTE(gasmann @ May 8 2006, 05:46) *

Well, and that's the problem: I can't hardcode it because the drive letter varies. In DOSBox, it's usually C:; in FreeDOS, it's H:; while in Windows XP it's M:.

That's really a pain that there is no possibility at all to get this working. I guess I'll have to require menu.bat to be started from the drive it resides on, then sad.gif

Thanks anyway for your tips wink.gif

IF EXIST C:MENU.BAT SET BATDRIVE=C
IF EXIST H:MENU.BAT SET BATDRIVE=H
.
.
.
.
Shade[ST]
You might be able to find a command line program which returns path of the shell or whatever...

Or maybe you can find it in an environnement variable. Try it out : type "set"
Jndrline
I'm looking for something similar, but I *am* using an NTFS. Is there a way to return just the file name instead of the whole path.

These won't work, because they return the whole path:

%cmdcmdline%
%0

People have shortcuts to batch files on the network, and I'm trying to modify the batch files to forward the user to a file of the same name located somewhere else. It would be much easier replace the files with something that says

CODE
ECHO OFF

CLS

REM So, what should %FILENAME% really be?
SET same_filename_as_what_your_currently_processing=%FILENAME %

ECHO You are using an old shortcut.  You will be redirected.  Please update.
ECHO .

PAUSE

CALL "\\newlocation\newpath\%same_filename_as_what_your_currently_processing%"

CLS


Thanks for the help.
Synthetic Soul
SET FILENAME=%~nx0
stephanV
Perhaps this is of use too --> http://www.ss64.com/ntsyntax/parameters.html smile.gif
Synthetic Soul
Yes, I probably should have explained, but I get a little bored of it.

Is that a new page? I don't remember seeing that there before. I used that site to learn batch file scripting, but I'm sure I found out about dealing with parameters from someone on this board (he had used it in a command line and I had to investigate to find the other options).

Anyway, I digress...
stephanV
I used that site for the same purpose (learning .bat scripting), and I actually stumbled on it because I found that particular page with google. So AFAIK it has always been there. smile.gif
Egor
By the way, batch scripting reference is available in Windows Help.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.