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-15 11:59
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » How to respectively get the drive letter and path in a batch file? Thanks! View 5,060 Replies 25
Original Poster Posted 2006-11-16 09:02 ·  中国 广东 广州 电信
初级用户
Credits 35
Posts 13
Joined 2006-07-10 15:08
20-year member
UID 58354
Gender Female
Status Offline
How to obtain this E:\Tools\pub3

And how to get E: and Tools\pub3 from it

Thank you!
Floor 2 Posted 2006-11-16 12:51 ·  中国 黑龙江 哈尔滨 中移铁通
高级用户
★★★
Credits 760
Posts 357
Joined 2005-10-10 22:33
20-year member
UID 43332
Status Offline
There is a small command, it seems that DIRT.COM can meet your requirements!
Floor 3 Posted 2006-11-16 16:16 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

  If it is a CMD batch file, you can try adding the following code in the file:

echo The full path of this file is: %cd%\
echo The drive letter where this file is located is: %cd:~0,2%
echo The path of this file is: %cd:~3%
pause
Floor 4 Posted 2006-11-16 22:10 ·  中国 广东 广州 电信
初级用户
Credits 35
Posts 13
Joined 2006-07-10 15:08
20-year member
UID 58354
Gender Female
Status Offline
Originally posted by lxmxn at 2006-11-16 16:16:

If it is a CMD batch file, you can try adding the following code in the file:

echo The full path of this file is: %cd%\
echo The drive letter where this file is located is: %cd:~0,2 ...


Thanks, brother lxmxn!

It works in the XP system,

In MS-DOS, it shows empty. I want to use it in pure DOS.

Please think of another way, thank you!
Floor 5 Posted 2006-11-16 22:21 ·  中国 广东 广州 电信
初级用户
Credits 35
Posts 13
Joined 2006-07-10 15:08
20-year member
UID 58354
Gender Female
Status Offline
Originally posted by 0451lym at 2006-11-16 12:51:
There is a small command that seems to be DIRT.COM that can meet your requirements!


Thanks, Brother 0451lym!

I didn't find DIRT.COM through searching,

Can you send one? Sororal@21cn.com

Thanks a lot!
Floor 6 Posted 2006-11-16 22:46 ·  中国 陕西 咸阳 电信
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
If the batch file is in the search path (not in the current directory), I'm afraid brother lxmxn's solution is also invalid.

In a pure DOS environment, there is a command called fpath, which is used to find the path where the current command is located. But the result still needs to be further processed to separate the drive letter and path.

http://nufans.net/clone/dostools/fpath.com
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 7 Posted 2006-11-16 23:10 ·  中国 广东 广州 电信
初级用户
Credits 35
Posts 13
Joined 2006-07-10 15:08
20-year member
UID 58354
Gender Female
Status Offline
Originally posted by Climbing at 2006-11-16 22:46:
If the batch file is in the search path (not in the current directory), I'm afraid Brother lxmxn's solution is also invalid.

In a pure DOS environment, there is a command called fpath, which is used to find the path where the current command is located...


Thank you, Brother Xie!

I still need you to teach me the specific usage. Thank you!
Floor 8 Posted 2006-11-17 00:16 ·  中国 上海 黄浦区 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
If we don't consider complex situations, your this question can be said to be just a string processing problem. You can use the @instr variable function of 4DOS.

@instr[0,2,E:\Tools\pub3]
Extract the string "E:"

@instr[3,,E:\Tools\pub3]
Extract the string "Tools\pub3"
Floor 9 Posted 2006-11-17 01:51 ·  中国 广东 广州 电信
初级用户
Credits 35
Posts 13
Joined 2006-07-10 15:08
20-year member
UID 58354
Gender Female
Status Offline
Originally posted by DOSforever at 2006-11-17 00:16:
If we don't consider complex situations, your problem can be said to be just a string processing problem. You can use the @instr variable function of 4DOS

@instr
Extract "E:"...



Thanks, Brother DOSforever!

However, Book.bat may not necessarily be in E:\Tools\pub3, it may also be in other directories,

so, E:\Tools\pub3 needs to be obtained first.
Floor 10 Posted 2006-11-17 02:04 ·  中国 上海 黄浦区 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
If that's the case, then I still don't understand your requirement. What exactly are you trying to do? Do you mean to let a BATCH automatically find the path of Book.bat in the E:\Tools\pub3 or other directories?
Floor 11 Posted 2006-11-17 04:48 ·  中国 黑龙江 哈尔滨 中移铁通
高级用户
★★★
Credits 760
Posts 357
Joined 2005-10-10 22:33
20-year member
UID 43332
Status Offline
Originally posted by Sororal at 2006-11-16 22:21:


Thanks to brother 0451lym!

Didn't find DIRT.COM,

Can you send one? Sororal@21cn.com

Thanks!



DIRT usage instructions are as follows:




Dirt.COM FileName.Ext (Ver 1.15)
(|)] HH:MM:SS]



Note: The function of setting environment variables of this program cannot run normally in the following systems.

winXP, win2000, and any system virtualized in Virtual PC



Whether or not a file that meets the conditions is found, Dirt.COM will set the environment variable DirtRet when exiting: Fail means no file is found, and Success means a file is found.


=====================================================
Ver 1.15
Update date: 2005-11-07


Upgrade instructions:

(1) Correct the error in the previous version where it directly searched from the root directory when "without path and file name" was searched.

(2) Add the automatic date calculation function to the original parameter YYYY-MM-DD].

Example:

Rem Find all files modified yesterday
dirt *.* /d:(-2)


In the last relatively long example, because the function of "automatic date calculation" has been added now, it can be simplified as follows:

@echo off
rem ============================================
rem The function of this batch processing is to delete all files (including files in all subdirectories) in d:\datafile that were last modified 7 days ago (excluding 7 days)
rem ============================================

attrib/s d:\datafile\*.* -h -r -s
:Loop
dirt.com /d:-(-7) d:\datafile\*.* /q
if %DirtRet%==Fail goto end
del %FPath%\%FName%
goto Loop

:end
=====================================================
Ver 1.14
Update date: 2005-10-29


Upgrade instructions:

(1) Correct the error that no file was found when searching in the root directory in the previous version.

(2) Add the function of returning code exit to the function of .
In this way, this function can be used in 2000 or xp. If the directory exists, return 1,
If it does not exist, return 0.

Example:

Judge whether d:\aa exists. If it does not exist, create it and enter

@echo off
dirt /dir:d:\aa
if errorlevel 1 goto exist
echo make d:\aa
md d:\aa

:exist
cd d:\aa

=====================================================
Ver 1.13
Update date: 2005-10-14


Upgrade instructions:

(1) Correct the wrong analysis of "*.*" in the previous version.


(2) Add parameter

Save the first 10 found files in the following environment variables
O2E_DriverX, O2E_PathX, O2E_FNameX
Save the drive letter, path, and file name respectively, where the last X is the serial number,
Replaced by 0 to 9. If the searched is a directory, O2E_FNameX will not be set

(3) Add parameter

Among the found files or directories, find the one with the earliest or latest time,
And save it in the following environment variables, FPath, FName
furthest is to find the file or directory with the earliest time
latest is the opposite

(4) Add parameter
The length of the main environment variable list of the Dos system is limited, so learn to clean up garbage.
This function is to clear all environment variables generated by this program.

(Note: The environment variables generated by this program cannot be cleared or modified by the Dos Set command
Because the Set command does not support variable names with lowercase letters)

Example:

rem Find the file with the latest modification time under D: and display it
dirt d:\*.* /time:latest
echo %FPath%\%FName%

rem Find the directory with the earliest creation time under D: and display it
dirt d:\*.*\ /time:furthest
echo %FPath%

=====================================================
Ver 1.12
Update date: 2005-09-22


Upgrade instructions:

(1) Correct some errors in "search path analysis".


(2) Add the function of folder search. Support all parameters except

But it finds the "creation time" of the directory. It is different from finding files (finding files is
According to the "modification time" of the file), so when the directory is renamed, its creation time
Will not change.

In the quick mode , the full path including the folder to be searched will be written into the environment variable FPath, and FName will not be set

The usage is: add a slash at the end of the path, and the specific method is as follows

Example:

dirt d:\ghost8\ /q
Search for a folder named "ghost8" under drive D. If found, enter it

@echo off
rem Note that there must be \ after ghost8
rem Otherwise, it will become searching for files
dirt d:\ghost8\ /q
if == goto notexist
cd %FPath%
goto end

:notexist
echo     No such directory found

:end
=====================================================
Ver 1.11
Update date: 2005-09-18


New function introduction:


Judge whether a certain folder exists, and set the environment variable Dir: Exist means exists,
NotExist means does not exist

Example:

Judge whether d:\aa exists. If it does not exist, create it and enter

@echo off
dirt /dir:d:\aa
if == goto exist
md d:\aa

:exist
cd d:\aa



=====================================================
Ver 1.1

New function introduction:

Whether or not a file that meets the conditions is found, Dirt.COM will set the environment variable DirtRet when exiting: Fail means no file is found, and Success means a file is found.

In this version, the error that the parameter position cannot be changed arbitrarily in version 1.0 is also corrected.



The function is the same as the /w of the dir command, and 5 file names are displayed per line.



Enable quick mode. As long as one file that meets the conditions is found, the search will end immediately.
And the file path and file name will be written into the environment variables FPath and FName respectively.


In addition, this program is now compiled into a.com file, and its volume is half smaller than the original dirt.exe.
=====================================================
Example:

Find the Tc.exe file in drive D. If found, run it directly and open the file try.c in the root directory of drive D. If not found, display a prompt message.

@echo off
Dirt d:\tc.exe /Q
if %DirtRet%==Fail goto NoFind

d:
cd %fpath%
%fname% d:\try.c
goto quit

:NoFind
echo.
echo.
echo Tc.exe not found in drive D

:quit



=====================================================
Function of Ver 1.0:

YYYY-MM-DD]
Specify the date, it must be of sufficient length, for example:
/d:2004-05-01 cannot be written as /d:2004-5-1
The inside is only later or earlier than this date
Such as: /d:-2004-05-01 Search for files before 2004-05-01

HH:MM:SS]
Reference date format

Time and date can be used together



The maximum number of layers of subdirectories to search, the maximum is two digits (that is, 99),
/L:0 does not search subdirectories
The default is all layers





====================================================
Another example:


@echo off

rem ============================================
rem The function of this batch processing is to delete all files (including files in all subdirectories) in d:\datafile that were last modified 7 days ago (excluding 7 days)
rem ============================================



rem ============================================
rem Get the date 7 days ago
rem If today is August 25, the date 7 days ago is August 18
rem ============================================
GetInfo.COM /d:-7
set theDay=%year%-%mon%-%day%



rem ============================================
rem This is to search for files modified 8 days ago (including 8 days) in D:\datafile
rem That is, files modified on August 17 and before
rem ============================================
attrib/s d:\datafile\*.* -h -r -s
:Loop
dirt.com /d:-%theDay% d:\datafile\*.* /q
if %DirtRet%==Fail goto end
del %FPath%\%FName%
goto Loop

:end
Attachments
Dirt.zip (15.92 KiB, Credits to download 1 pts, Downloads: 41)
Floor 12 Posted 2006-11-17 04:52 ·  中国 黑龙江 哈尔滨 中移铁通
高级用户
★★★
Credits 760
Posts 357
Joined 2005-10-10 22:33
20-year member
UID 43332
Status Offline
Another small software by the same author is very practical, but unfortunately it cannot clear variables!

GetInfo.COM ver 1.2

/T Get current date and time.
And It will modify follow environment variable.
year,mon,day,wday
hour,min,sec

/C Get the first CDRom.
And It will modify follow environment variable.
CDRom

/D It will modify follow environment variable.
year,mon,day,wday

/G:n,filename
Get the String from the text file.
The String must be short then 2048 Byte!
It will modify follow environment variable.
GetString,TheString

/Cmp:x,y
x=y return 0
x<y return 1
x>y return 2


=====================================================
Version 1.2 Update date:2005-10-16

Function introduction:


Compare two numbers and return a value
x=y return 0
x<y return 1
x>y return 2

Examples:


Compare 12 and 13 and display the result
@echo off
getinfo /cmp:12,13
if errorlevel 2 goto big
if errorlevel 1 goto small
if errorlevel 0 goto equ
goto end

:big
echo x bigger than y
goto end

:small
echo x smaller than y
goto end

:equ
echo x = y

:end



Judge whether today's date is the same as the set date
@echo off
getinfo /t
set today=%Year%%Mon%%Day%
getinfo /cmp:%today%,20051016
if errorlevel 2 goto big
if errorlevel 1 goto small
if errorlevel 0 goto equ
goto end

:big
echo The set date has passed
goto end

:small
echo The set date has not arrived yet
goto end

:equ
echo Today is the set date

:end




=====================================================
Version 1.1 Update date:2005-06-24

Function introduction:



Get the current system's date and time, and set the following environment variables.
year: year mon: month day: day wday: day of the week (Sunday is 7)
hour: hour min: minute sec: second



Get the drive letter of the first CD-ROM, and set the environment variable CDRom.
If there is a CD-ROM, set it to the drive letter of the CD-ROM.
If there is no CD-ROM, set it to NoCDRom



Calculate the current system's date with the specified days to get another date, and set the following environment variables.
year: year mon: month day: day wday: day of the week (Sunday is 7)

Examples:
GetInfo.com /d:-1 Get yesterday's date
GetInfo.com /d:+1 Get tomorrow's date



Get the content of the nth line from the specified text file, and save the content in TheString.
If the length of the string in this line is greater than 2048 characters, only 2048 characters will be taken.
GetString=TRUE when successful, otherwise, GetString=FALSE

Examples:
GetInfo.com /g:5,D:\A.TXT Get the content of the 5th line in D:\A.TXT



In addition, this program is now compiled into a .com file, and its size is half smaller than the original GetInfo.exe.
Attachments
Getinfo.zip (13.14 KiB, Credits to download 1 pts, Downloads: 23)
Floor 13 Posted 2006-11-17 05:59 ·  中国 广东 广州 电信
初级用户
Credits 35
Posts 13
Joined 2006-07-10 15:08
20-year member
UID 58354
Gender Female
Status Offline
Thanks, brother 0451lym!

Study earnestly.
Floor 14 Posted 2007-01-30 12:42 ·  中国 广东 广州 珠江宽频
中级用户
★★
Credits 210
Posts 85
Joined 2007-01-14 12:38
19-year member
UID 76501
Gender Male
From 广东广州
Status Offline
GetInfo 1.21 can already clear self - built variables

http://www.sendsong.cn/soft/
Floor 15 Posted 2007-02-01 01:44 ·  中国 广东 潮州 电信
中级用户
★★
Credits 240
Posts 53
Joined 2005-01-21 00:00
21-year member
UID 35493
Gender Male
Status Offline
dirt.com
Ver 1.16 (Just a little improvement, no new functions added)
Update date: 2007-01-01

Upgrade instructions:

When dirt is working, press Esc to exit immediately.

In my actual use, many times when dirt is searching for files or directories, I suddenly think of a solution to the problem and want to immediately terminate dirt's current work, but using Ctrl+C or Ctrl+Break has no effect, which is very frustrating. In the Dos command line in Windows, you can terminate it at any time with Ctrl+Break, but it's not possible in pure Dos.

So, I added the function to exit at any time to dirt.com.



Download URL
http://www.sendsong.cn/soft/
Forum Jump: