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-06-25 06:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to obtain the current path information in batch processing View 16,145 Replies 20
Original Poster Posted 2005-03-02 00:00 ·  中国 江苏 南京 联通
初级用户
Credits 119
Posts 6
Joined 2005-03-01 00:00
21-year member
UID 36629
Gender Male
Status Offline
Experts, I have a question to ask for help!

The problem is like this: for example, I first need to save the current path, then switch to drive A to execute a command, and after it finishes

I need to switch the path back to the path I saved just now. How should this batch file be written?

Please help me out, everyone, thanks!
Floor 2 Posted 2005-03-02 00:00 ·  中国 湖南 长沙 电信
高级用户
★★
Credits 772
Posts 273
Joined 2004-10-23 00:00
21-year member
UID 32861
Gender Male
Status Offline
I don't know why the forum has been so unstable lately! Just now I wrote a very detailed long reply and it didn't post successfully! Now I'm sending it to you again.
Next time when you make a Help post, first explain the runtime environment. Also the conditions—for example, if you know the source path, just directly enter the drive where the source path is located and you can return to the source path. But if you don't know the source path, that is, when executing inside a batch file, then if it is an NT or above operating system, you can use the following code: set a="%cd%"
for %%a in (%a%) do set b=%%~da
for %%b in (%a%) do set c=%%~npb
::code you run after switching drive letters
...
::code you need to return to the source path
%b%
cd\
cd %c%

If it is under pure DOS or under 98's DOS, then you can use the third-party tool strings.com, which can be downloaded from the forum's software section. Since I still have something else to do, I can't switch to DOS to verify it for you. So I'm giving you some usages of strings.com, hope they're useful to you. It's also good to try it yourself.
1.STRINGS LEFT source string number of characters to take 〈take the specified number of characters from the left side of the source string〉
2.STRINGS RIGHT source string number of characters to take 〈take the specified number of characters from the right side of the source string〉
3.STRINGS MID source string start number,end number 〈take from the middle of the source string〉
4.STRINGS LENGTH source string 〈return the character length of the source string〉
5.STRINGS FIND source string character 〈find the specified character in the source string〉
6.STRINGS FINDC source string character 〈find the specified character in the source string, case-sensitive〉
7.STRINGS LOWER source string 〈convert uppercase letters in the source string to lowercase letters〉
8.STRINGS UPPER source string 〈convert lowercase letters in the source string to uppercase letters〉
9.STRINGS CHAR character ... 〈convert characters to ASCII numbers〉
10、STRINGS VAL number ... 〈convert numbers to ASCII characters〉
11、STRINGS FILEDRIVE filename 〈return the drive letter where the filename is located〉
12、STRINGS FILEDIR filename 〈return the directory where the filename is located〉
13、STRINGS FILENAME source filename 〈return only the filename part of the source filename, without the extension〉
14、STRINGS FILEEXT source filename 〈return only the extension of the source filename〉
15、STRINGS ADDCOMMAS number 〈insert commas every three digits in the number〉
16、STRINGS REPEAT how many times,character or number 〈repeat the specified character or number the specified number of times〉
17、STRINGS READ filename,line number 〈read the content of the specified line in the file〉
18、STRINGS WRITE filename,character 〈append the character to the file〉
19、STRINGS FILESIZE filename 〈return the size of the file〉
20、STRINGS LINESIZE filename 〈return the total number of lines in the file content〉
21、STRINGS TRUENAME filename 〈return the full path of the file〉
22、STRINGS FILEDATE filename 〈return the modification date of the file〉
23、STRINGS FILETIME filename 〈return the modification time of the file〉
24、STRINGS VER 〈return the DOS version number of the current system〉
25、STRINGS ASK prompt string 〈dialog〉
26、STRINGS INWIN 〈returns 1 if running in a DOS window under WINDOWS〉
27、STRINGS ENVFREE 〈return the free bytes of environment variable space〉
28、STRINGS ENVSIZE 〈return the total size of environment variable space〉
29、STRINGS TRUEVER 〈return the true version number of the current DOS〉
30、STRINGS FILES 〈return the current number of simultaneously accessed files〉
31、STRINGS LASTDRIVE 〈return the last valid drive letter the current system can access〉
32、STRINGS CODEPAGE 〈return the active long filename code page of the current system〉
33、STRINGS COUNTRY 〈return the country code set in the current system〉
34、STRINGS BIOSDATE 〈return the BIOS manufacturing date〉
35、STRINGS GETKEY 〈return the ASCII code of the key pressed〉
36、STRINGS ADD number1,number2... 〈perform + operation on numbers〉
37、STRINGS SUB number1,number2... 〈perform − operation on numbers〉
38、STRINGS MUL number1,number2... 〈perform × operation on numbers〉
39、STRINGS DIV number1,number2... 〈perform ÷ operation on numbers〉
40、STRINGS AND number1,number2... 〈perform “logical AND” on numbers〉
41、STRINGS OR number1,number2... 〈perform “logical OR” on numbers〉
42、STRINGS NOT number1,number2... 〈perform “logical NOT” on numbers〉
43、STRINGS MEMTOTAL 〈return the total conventional memory size of the system〉
44、STRINGS MEMFREE 〈return the free conventional memory size of the system〉
45、STRINGS XMSTOTAL 〈return the total extended memory size of the system〉
46、STRINGS XMSFREE 〈return the free extended memory size of the system〉
47、STRINGS XMSVER 〈return the version number of the driver currently used for extended memory〉
48、STRINGS EMSTOTAL 〈return the total expanded memory size of the system〉
49、STRINGS EMSFREE 〈return the free expanded memory size of the system〉
50、STRINGS EMSVER 〈return the version number of the driver currently used for expanded memory〉
51、STRINGS STRINGSVER 〈return the version number of STRINGS〉
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bjsh +8 2007-05-06 10:14
http://www.msfans.net/bbs/
Floor 3 Posted 2005-03-03 00:00 ·  中国 江苏 南京 联通
初级用户
Credits 119
Posts 6
Joined 2005-03-01 00:00
21-year member
UID 36629
Gender Male
Status Offline
Thanks for the reply!
But in the functions of strings.com, there isn't one for getting the current path. How should this be handled?
Floor 4 Posted 2005-03-03 00:00 ·  中国 上海 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
If you use 4DOS / 4NT then everything is much too simple

PUSHD

…………

PUSHD

……

POPD

…………

POPD

PUSHD pushes the current path onto the stack, where is optional. If you specify PATH explicitly, then after pushing the current path onto the stack it switches to the new path. For example, if your current working directory is C:\MSDOS, executing PUSHD E:\PCTOOLS will immediately put you in the E:\PCTOOLS directory. After that you can switch paths however you like; no matter how many times you switch, as long as you use POPD once, you'll immediately return to the directory you last pushed onto the stack. PUSHD and POPD can be nested, but you should note that they work with a stack structure, so if you push multiple times, pay attention to the order when popping. If you're only doing one path switch and it's not on the same drive, then there's no need to make such a “big production” out of it. For example, if your current working directory is C:\MSDOS, then no matter which drive and directory you later switch to (except other paths on drive C), to return to the original path you only need C:. After telling you all this, I suddenly realized I find it very strange that you need this function. I've been doing BATCH up to now and I've almost never used this myself. I don't know what you're writing that needs to be used this way?

[ Last edited by DOSforever on 2008-2-29 at 01:00 AM ]
Floor 5 Posted 2005-03-03 00:00 ·  中国 湖南 长沙 电信
高级用户
★★
Credits 772
Posts 273
Joined 2004-10-23 00:00
21-year member
UID 32861
Gender Male
Status Offline
The poster above should prescribe the right remedy for the right problem—could it be you want the OP to change systems? As for the OP's question, I'll give you the answer in a bit. Right now I'm processing something and can't reboot the machine for a while. I think strings.com should be able to meet your requirement.
http://www.msfans.net/bbs/
Floor 6 Posted 2005-03-03 00:00 ·  中国 上海 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
You're absolutely right. What I mean is to suggest to the OP—and to everyone who uses BATCH—that they all switch their command interpreter to 4DOS / 4NT. I believe there isn't a single person who is good at writing BATCH who wouldn't like it, and I think you'll definitely find it hard to put down too. Besides, how can changing only the command interpreter be called changing the system? If you don't want to use it, just leave it there; there's no need to uninstall it either. This is no different from your use of third-party tools. And every time you use one of its functions, you have to call its executable file from disk once. Even though machines today are much faster than before, I still think one should have an awareness of optimization. On this forum I've seen far too many posts racking their brains over BATCH and some tiny problems. I really feel some things don't need to be done by taking a huge detour, so I've been tirelessly recommending this good thing to everyone. I've repeated similar words many times already. Of course, my suggestion is only my suggestion; whether to use it or not is someone else's business.
Floor 7 Posted 2005-03-03 00:00 ·  中国 江苏 南京 联通
初级用户
Credits 119
Posts 6
Joined 2005-03-01 00:00
21-year member
UID 36629
Gender Male
Status Offline
If DOS had a shell like bash, I'd wake up laughing even in my sleep.
Floor 8 Posted 2005-03-03 00:00 ·  中国 江苏 南京 联通
初级用户
Credits 119
Posts 6
Joined 2005-03-01 00:00
21-year member
UID 36629
Gender Male
Status Offline
By the way, a question for dosforever: what kind of thing is 4DOS / 4NT? Don't look down on me, I'm a newbie. Although I've been using computers since 1996, I haven't touched DOS for a long time, so I'm not too clear on new things. Where can I download 4DOS / 4NT, and how do I use it? Give me a link, thanks~~~~I've only become interested in DOS these past few days because I'm making a DOS boot disk that supports NTFS format and can mount ISO from an NTFS partition. The project I'm working on is very tight right now, and I'm still taking time out to play with DOS—not easy at all!!!! So please give me lots of help, everyone~~
Floor 9 Posted 2005-03-03 00:00 ·  中国 江苏 南京 联通
初级用户
Credits 119
Posts 6
Joined 2005-03-01 00:00
21-year member
UID 36629
Gender Male
Status Offline
Let me show everyone my mount.bat file. There's still a problem that needs to be solved: how do I determine whether a drive letter is already occupied? This line in the code is definitely wrong: if exist "%2" goto drvexist. How should this be changed? Thanks~~

@echo off
if "%1" == "" goto use
if "%2" == "" goto use
if not exist "%1" goto noexist
if exist "%2" goto drvexist
A:
LH Smartdrv 4088 4088 >NUL
SHSUCDHD /F:%1 >NUL
SHSUCDX /D:SHSU-CDH,%2 >NUL
cls
ECHO,
ECHO 【ISO CD image file installation wizard】
%2
DIR/W
ECHO,
ECHO The ISO CD image file has been installed...
ECHO,
goto end
:noexist
echo File %1 does not exist, please specify the image file again!
goto end
:use
echo Usage: mount image filename drive letter
goto end
:drvexist
echo Drive name %2 is already in use, please specify the drive name again!
goto end
:end
Floor 10 Posted 2005-03-03 00:00 ·  中国 上海 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
Hehe, you're overthinking it. How would I think of looking down on you? Nobody can possibly know everything. I also have many things I don't understand and need to ask others about. The purpose of coming to the forum is to ask each other, help each other, and make progress together. Let me first give you a rough introduction:
4DOS / 4NT is a command interpreter made by JP Software, that is, an OS Shell. Of course, I think under pure DOS it should only be regarded as a Shell, while under Windows it should be regarded as a command-line environment. Actually it's not some new thing at all; it's just that you didn't notice it before. The earliest 4DOS I came into contact with was NDOS included with Norton 8.0. In fact, Norton bought JP Software's 4DOS and renamed it NDOS, just like how Microsoft bought Norton's speedisk and called it defrag. The earliest 4DOS command interpreter only had 4DOS.COM for the DOS environment (although it is a .COM, in fact it is a file with an .EXE structure; I think it was probably named .COM to preserve tradition). Later, with the popularity of Windows, JP Software released 4DOS for the Windows environment—called 4NT. Don't be misled by the name into thinking it can only be used under NT. It was further divided into an ASCII version and a Unicode version: the former is for Win 98/98SE/ME, and the latter is for Win NT/2000/XP/2003. Of course, 4DOS under DOS can also be used under Win9X. I haven't used it under the NT series, so I'm not sure, though I guess it can still run, except that some commands may not work properly. Now JP Software has already given up the copyright to 4DOS under DOS, and everyone can use it FREE! For my other brief introduction to the 4DOS family and its download addresses, you can first take a look here:

http://www.cn-dos.net/forum/viewthread.php?tid=15111

and here:

http://www.cn-dos.net/forum/viewthread.php?tid=15214

As for an authoritative and comprehensive introduction, then of course it's on its official website.

Next let's discuss your BATCH problem:
I've never used what you called a “DOS boot disk that mounts ISO from an NTFS partition.” Not afraid of everyone laughing at me, I've never even used ISO files. From the original file you provided and from my understanding, you're executing SHSUCDHD from drive A to map the ISO file as a CD-ROM drive, then using SHSUCDX to map the CD-ROM drive as a logical drive letter, then switching to the newly generated logical drive, and showing everyone, “Look, I've already listed files on the new logical drive.” Is that what you mean? If so, I still have a few questions: why do you have to go to drive A to execute Smartdrv, SHSUCDHD, and SHSUCDX? Could it be that these three files, together with the ISO file, cannot be executed from the hard disk? For example:
c:\windows\command\Smartdrv 32768
c:\windows\command\SHSUCDHD /F:%1 >NUL
c:\windows\command\SHSUCDX /D:SHSU-CDH,%2 >NUL

I think I need to first clarify my question before going on with further discussion. Also, Smartdrv doesn't need LH. As long as you have enough UMB, it can LH itself. Of course, if you LH it, it won't report an error either. When starting Smartdrv for the first time, if you don't want to see any messages, you also don't need to use >NUL, because by default Smartdrv shows no prompt information at all on first startup. If you want it to show prompt information, you have to add the /V parameter. If your memory is large enough, say 64M or more, you might as well make Smartdrv's cache area larger. If you don't use it, it's just wasted anyway.



Link fixed

[ Last edited by DOSforever on 2008-6-4 at 03:49 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bjsh +8 2007-05-06 10:15
Floor 11 Posted 2005-03-04 00:00 ·  中国 湖南 长沙 电信
高级用户
★★
Credits 772
Posts 273
Joined 2004-10-23 00:00
21-year member
UID 32861
Gender Male
Status Offline
This is really maddening, it didn't go out again, I'm furious. What on earth has been going on with the forum lately?
http://www.msfans.net/bbs/
Floor 12 Posted 2005-03-04 00:00 ·  中国 湖南 长沙 电信
高级用户
★★
Credits 772
Posts 273
Joined 2004-10-23 00:00
21-year member
UID 32861
Gender Male
Status Offline
Looks like the OP still hasn't found the answer, so I think I'd better give you the answer. strings a=filedrive pp.bat
strings b=filedir pp.bat
::strings x=filename pp.bat If you need to know the full path of the original file, this is necessary; you just need to remove the leading "::"
::strings c=fileext pp.bat If you need to know the full path of the original file, this is necessary; you just need to remove the leading "::"
set ypath=%a%%b%
::set d=%a%%b%\%x%.%c% This is the full path
::code you run after switching drive letters
...
::code you need to return to the source path
%a%
cd\
cd %ypath%
http://www.msfans.net/bbs/
Floor 13 Posted 2005-03-04 00:00 ·  中国 重庆 渝中区 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
string is a good thing!
Floor 14 Posted 2005-03-04 00:00 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re All:

Everyone has discussed so much about the choice of tools, but in fact this is only a difference caused by personal habits and different usage scenarios. Personally, when writing batch files I rarely use third-party tools. There's no other reason; it's mainly because “turning seemingly impossible things into possible ones” can produce an incomparable kind of joy. Below is a batch file example that does not depend on the environment. It has been tested successfully on WinXP/DOS7.


@echo off
echo Current path is:
cd
pause
echo exit | %comspec% /k prompt $N:$_cd $P$_ | find "exit" /v > %temp%.\~retpath.bat
c:
cd\windows
echo Current path is:
cd
pause
for %%c in (call del) do %%c %temp%.\~retpath.bat
echo Current path is:
cd


[ Last edited by willsort on 2005-10-30 at 13:55 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 15 Posted 2005-03-07 00:00 ·  中国 上海 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
Hehe, “Xiaotian” is formidable—being able to achieve the same function under limited conditions really makes you an expert. Truly worthy of the title Batchinger. I have benefited greatly! But one principle of mine is to use as few external commands as possible and involve as little disk I/O as possible when writing BATCH. Of course, with today's machine speeds this factor can be completely ignored, but I still keep this habit. As for what you said about “using fewer third-party tools,” my view is that this “third party” is really only a very clear concept from the vendor's point of view; for us users, that boundary is very blurry. For us, as long as something works well, it doesn't matter whether it's third-party or not. For example, originally MS-DOS did not have tools like antivirus, disk defragmentation, or full-screen interface backup. Microsoft simply brought in CPAV and turned it into MSAV and VSAFE, brought in SPEEDISK and turned it into DEFRAG, and brought in NORTON BACKUP and turned it into MSBACKUP. Could it be that before microsoft included these in its DOS, we couldn't use these “third-party” tools? Although you only used the tools that come with DOS, after all you still “took a big detour.” Look, if you use 4DOS it's much cleaner—two sentences solve the problem, and all of it operates in memory. When you can get the same result with less effort and save more resources, what's bad about that? I believe that for you, using 4DOS would definitely let you handle things with greater ease. If you can use it for BATCHING, it can be called “a treasured sword matched with a hero”! In addition, I don't understand why you add a “ . ” after %temp%; wouldn't that become a syntax error?
Forum Jump: