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!
Credits 27 Posts 13 Joined 2008-06-21 09:52 18-year member UID 120670 Gender Male
Status Offline
I don't know if that's what you mean? Try this to see if it meets your requirements:
@echo off
echo Current path is: %cd%
echo Current drive letter is: %cd:~0,1%
pause
Credits 3,946 Posts 1,884 Joined 2006-01-20 13:00 20-year member UID 49283 Gender Male
Status Offline
Originally posted by Climbing at 2008-6-23 22:27:
Using strings only extracts the drive letter in the path, which is not equal to obtaining the running path of the batch file.
Can extract both the drive letter and the directory.
Using both parameters can get the complete path.
```batch
@echo off
rem 获取当前批处理文件的完整路径
for %%i in (%0) do set "batPath=%%~fi"
echo %batPath%
```
解释一下,`%0`表示当前正在执行的批处理文件的名称及路径(相对路径),`%%~fi`会将`%0`扩展为完整的绝对路径,然后将其赋值给`batPath`变量,最后输出`batPath`就可以得到`x.bat`本身所在的路径了。So the translated text is:
Assume my batch file is c:\x.bat (the current directory of drive C is c:\), and my current drive and current directory are: d:\test. Then I run with the following command line:
c:x.bat.
How should your X.bat be written to get its own path?
I'm sure this can't be done with Strings.
The following is the code to get the path of the batch file itself:
```batch
@echo off
rem Get the full path of the current batch file
for %%i in (%0) do set "batPath=%%~fi"
echo %batPath%
```
Explanation: `%0` represents the name and path (relative path) of the currently executing batch file, `%%~fi` will expand `%0` to the complete absolute path, then assign it to the `batPath` variable, and finally output `batPath` to get the path where `x.bat` itself is located.
Credits 3,946 Posts 1,884 Joined 2006-01-20 13:00 20-year member UID 49283 Gender Male
Status Offline
Originally posted by Climbing at 2008-6-24 12:30:
Suppose my batch file is c:\x.bat (the current directory of drive C is c:\), and my current drive and directory are: d:\test, then I run with the following command line:
c:x.bat.
How should your X.bat be...
Can be answered with certainty, yes.
The result is "c:\".
See the picture for the running result.
As long as you try strings yourself, you'll know. Hehe.
If it doesn't work after trying, I'll write those two commands.
Credits 3,946 Posts 1,884 Joined 2006-01-20 13:00 20-year member UID 49283 Gender Male
Status Offline
Originally posted by Climbing at 2008-6-24 12:30:
Suppose my batch file is c:\x.bat (the current directory on drive C is c:\), and my current drive and directory are: d:\test, then I run with the following command line:
c:x.bat.
How should your X.bat be ...
Quoting "Don't judge a problem by imagination, practice makes true knowledge." Hehe.
Credits 2,343 Posts 636 Joined 2004-03-06 00:00 22-year member UID 19350 Gender Male
Status Offline
It can be considered to not use strings respectively.
Respectively use
dir /s/b %0
and
for %%p in (%path%) do dir /b %%p.\%0
to detect, which should be able to get the path where the batch processing is located in most cases.
As for if the standard output is transferred and stored to the environment variable, there are many methods that can be used, so I won't discuss it much.