China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-03 21:30
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to get the path where the current batch file is located in batch processing? View 2,717 Replies 17
Original Poster Posted 2007-02-04 07:55 ·  中国 河南 开封 教育网
中级用户
★★
Credits 214
Posts 94
Joined 2007-01-11 10:39
19-year member
UID 76165
Gender Male
Status Offline
Some small software need to run in its own path to smoothly load necessary running files, for example, the IFSDRV modified by Boss W, which loads Chinese 936.CPI...

So I want to set the PATH path, and when running the batch file that calls IFSDRV in other drives, I need to get the path of this batch file (this batch file is in the same directory as IFSDRV), then get the path of GO.EXE, and then run it, but how to get this path??
I don't want to use absolute paths. Is there any system command to directly get this path??
Floor 2 Posted 2007-02-04 22:15 ·  中国 山东 济宁 联通
钻石会员
★★★★★
Credits 10,051
Posts 3,040
Joined 2002-11-11 00:00
23-year member
UID 223
Gender Male
Status Offline
What to add between the percent sign %0 and 0.
简单就是美
Floor 3 Posted 2007-02-05 11:06 ·  中国 河南 开封 教育网
中级用户
★★
Credits 214
Posts 94
Joined 2007-01-11 10:39
19-year member
UID 76165
Gender Male
Status Offline
Hehe, what to add??
The thing I want to implement is like this example:
After setting the PATH paths, and this batch file may exist in any of these paths, how to run this batch file in any drive letter and automatically go to the path where this batch file is located???
Floor 4 Posted 2007-02-05 11:36 ·  中国 山东 济宁 联通
钻石会员
★★★★★
Credits 10,051
Posts 3,040
Joined 2002-11-11 00:00
23-year member
UID 223
Gender Male
Status Offline
Go to the batch processing area to ask, where people tinker with this all day long. Or like me, when I don't understand, I look at the help of the command. Just now I checked, it's %~p0, and entering for /? in cmd explains it in detail.
简单就是美
Floor 5 Posted 2007-02-05 12:32 ·  中国 河南 开封 教育网
中级用户
★★
Credits 214
Posts 94
Joined 2007-01-11 10:39
19-year member
UID 76165
Gender Male
Status Offline
Hehe, I posted in the wrong place. I'll go ask in the batch processing section. But %~p0 is in CMD under WINDOWS, and it can't be used in DOS. It's a bit of a pity.. Thanks to the person above
Floor 6 Posted 2007-02-06 00:06 ·  中国 山东 济宁 联通
钻石会员
★★★★★
Credits 10,051
Posts 3,040
Joined 2002-11-11 00:00
23-year member
UID 223
Gender Male
Status Offline
Originally posted by llztt1 at 2007-2-4 11:32 PM:
Hehe, posted in the wrong place. I'll go ask in the batch processing section.
But %~p0 is in CMD under WINDOWS, not available in DOS. It's a bit of a pity..
Thanks to the above


For DOS, then it's better not to go.
简单就是美
Floor 7 Posted 2007-02-14 00:46 ·  中国 江苏 苏州 电信
初级用户
Credits 46
Posts 19
Joined 2007-02-07 01:43
19-year member
UID 78833
Gender Male
Status Offline
Been here for two days, still completely confused :(
Floor 8 Posted 2007-02-14 06:28 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Floor 9 Posted 2007-02-15 00:32 ·  中国 河南 开封 教育网
中级用户
★★
Credits 214
Posts 94
Joined 2007-01-11 10:39
19-year member
UID 76165
Gender Male
Status Offline
Floor 10 Posted 2007-02-15 03:44 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Then in the dir, by yourself. Should you be able to get the directory in the returned information?
Floor 11 Posted 2007-02-16 13:03 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Try "pushd 路径"
Floor 12 Posted 2007-02-23 09:55 ·  中国 江苏 南京 鼓楼区 电信
初级用户
Credits 31
Posts 9
Joined 2007-01-18 22:43
19-year member
UID 76924
Gender Male
Status Offline
If TRUENAME and the batch file are in the same path, you can use TRUENAME to return the path where it is located.
Floor 13 Posted 2007-02-25 22:57 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 866
Posts 415
Joined 2005-12-04 11:19
20-year member
UID 46459
Status Offline
If it's really in pure DOS, it seems there's no way. In CMD, there are plenty of methods.
Floor 14 Posted 2007-02-26 02:44 ·  中国 黑龙江 哈尔滨 电信
高级用户
★★★
Credits 760
Posts 357
Joined 2005-10-10 22:33
20-year member
UID 43332
Status Offline
APPEND

--------------------------------------------------------------------------------


Enables a program to open a data file in a specified directory as if the file were in the current directory. Do not use this command when running Windows.

The specified directory is called the append directory. For opening data files, searches for these files act as if they were appended to the current directory.



Syntax:


APPEND path] ]


To display the list of append directories, use:

APPEND

To delete the existing list of append directories, use:

APPEND ;


Parameters:


path
Specifies the directory and its drive (if different from the current drive) to append to the current directory. Multiple path can be specified, separated by semicolons (;).

;
When used directly, this symbol deletes the existing list of append directories.



Switches:

/X

Indicates whether MS-DOS searches the append directory when the program executes (/X:ON) or not (/X:OFF). /X:ON can be abbreviated as /X. If /X:ON is specified, it must be done when APPEND is first used after the system is booted. After that, /X:ON and /X:OFF can be switched back and forth, with the default being /X:OFF.

/PATH:ON|/PATH:OFF
Indicates whether the program searches the append directory for the data file even if the file name is already in the path the program is looking for. The default setting is /PATH:ON.

/E
Assigns the append directory list to an environment variable named APPEND. This switch can only be used when APPEND is first referenced after the system is booted. With /E, you can use the SET command to display the append directory list. For instructions on environment variables, see the <SET> command.

Note: The Append command cannot be used with Microsoft Windows or the Windows Setup program.



Related Commands

For setting the search path for executable files, see the <PATH> command.




--------------------------------------------------------------------------------

APPEND─Notes


APPEND and Microsoft Windows

APPEND cannot be used with Microsoft Windows or the Windows Setup program.


Running APPEND Multiple Times

You can use APPEND multiple times after the system is booted. But note the following:

★ The /E switch is only effective in the first use of APPEND after the system is booted.

★ The .EXE file extension must be omitted when APPEND is run the second time and later. If you try to enter APPEND.EXE to run APPEND, APPEND will only be loaded once.


Storing the Append Directory List in the Environment

You can use /E in APPEND to assign the list of appended directories to an environment variable named APPEND. The method is to first execute the APPEND command with only the /E switch, and then execute APPEND again, including the directories you want to append. You cannot specify /E and path in the same command line.


Specifying Multiple Append Directories

When appending multiple directories, separate the directory entries with semicolons (;). If you execute APPEND with the path parameter again, the directories specified this time will replace any directories specified in the previous APPEND command.


Append Directories and the DIR Command

The DIR command's execution result list does not include file names from the append directories.


File Name Conflicts

If there is a file with the same name in the append directory and the current directory, the file in the current directory is opened.


APPEND and Programs That Create New Files

When a program opens a file in an append directory, it is as if it were opening a file in the current directory. If the program then saves this file as a new file with the same name, the new file is created in the current directory (not the append directory). APPEND is more suitable for data files that are not modified or are modified but not backed up as new files. Database programs usually modify data files but do not back them up as new files. Text editors and word processors usually store modified data files with backups. To avoid confusion, do not use APPEND with these programs.


/X:ON Switch and PATH Command Usage

Once /X:ON is specified, you can run a program stored in an append directory by simply entering the program name at the command prompt. Normally, the PATH command is used to specify the directory where the program is located. But if your program is in an append directory, you no longer need to use the PATH command to indicate its directory. MS-DOS finds a program in an append directory in the following order: first the current directory, then the append directory, and then the search path PATH.


MS-DOS Functions That Always Use the Append Directory

Even if /X:ON is not specified, when a program calls the following interrupt 21h functions of MS-DOS, the append directory is still used.

★ Open file (0Fh)

★ Open file handle (3Dh)

★ Read file size (23h)

When /X:ON is specified, the program uses the append directory each time it calls the following interrupt 21h functions.

★ Find first entry (11h)

★ Find first file (4Eh)

★ Execute program (EXEC) (4Bh)


Using APPEND on Network Drives

The APPEND command is also applicable to appending directories on network drives.
--------------------------------------------------------------------------------

APPEND─Examples

To enable a program to open data files in the LETTERS directory on drive B and the REPORTS directory on drive A as if they were in the current directory, enter the following command:

append b:\letters;a:\reports


To append the same directories and store the list of appended directories in the MS-DOS environment variable, type the following commands:

append /e

append b:\letters;a:\reports

These commands must be used when APPEND is first executed after the system is booted.
Floor 15 Posted 2007-03-17 00:09 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
It's all overcomplicated!

Actually, just add a line at the beginning

set programme_path=%cd%

Then directly call %programme_path% later.

I just encountered such a problem yesterday;

Later I found out it can be so simple
Forum Jump: