中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 17:39
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Ask] How to get the last part of the file name in the absolute path? [Solved]
Printable Version  6,786 / 25
Floor1 yardian Posted 2006-10-19 01:38
中级用户 Posts 85 Credits 305
Not knowing programming and not being very familiar with DOS batch processing. It's really difficult to write a script. Now I encounter a new problem:
I now want to get the last part of the file name containing the absolute path. How to write in batch processing?
For example: a:\c\E\b.txt (the path length is not fixed, there may be many layers of directories), now I want to create a variable, let the value of this variable be b.txt, how to solve it with batch processing?

[ Last edited by yardian on 2006-10-19 at 11:24 ]
Floor2 3742668 Posted 2006-10-19 01:49
荣誉版主 Posts 718 Credits 2,013

The code is written directly in the reply, not tested...
Floor3 yardian Posted 2006-10-19 02:10
中级用户 Posts 85 Credits 305
Thanks! This is exactly the effect I want!
Floor4 namejm Posted 2006-10-19 06:01
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The code of 3742668 can be simplified:


I found a small bug and fixed it, which can adapt to the situation where the path name has spaces.

[ Last edited by namejm on 2006-10-19 at 06:24 ]
Floor5 lanlaila Posted 2006-10-19 06:04
新手上路 Posts 3 Credits 6
The learning atmosphere here is really good, I'm going to study hard.
Floor6 pengfei Posted 2006-10-19 06:10
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
That's really nice!

Moderator 3742668 converts the file path into the variable %1 using call, preparing for expanding to the file name and suffix.

Moderator namejm directly uses for to convert the file path into replaceable variables, then performs expansion.
Floor7 3742668 Posted 2006-10-19 07:46
荣誉版主 Posts 718 Credits 2,013
```
@echo off
set file=x:\xxx\xxx\xxx.xxx
set file=%file:\=\\%
wmic datafile where "caption='%file%'" get filename,extension
pause

@echo off
set file=x:\xxx\xxx\xxx.xxx
set str=%file%
:loop
set str=%str:*\=%
echo %str% | findstr /i "\\" >nul && goto loop
echo %file% ---^> %str%
pause
The above codes have not been tested...
```
Floor8 yardian Posted 2006-10-19 07:47
中级用户 Posts 85 Credits 305
Everyone is very capable, or here the atmosphere is good, thanks everyone
Floor9 namejm Posted 2006-10-19 08:02
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The second paragraph of code at 7F is probably the most general. By expanding it, you can obtain the folder names at a specified level.
Floor10 3742668 Posted 2006-10-19 08:10
荣誉版主 Posts 718 Credits 2,013
```
@echo off
set file=x:\xxx\xxx\xxx.xxx
set file=%file:\=" "%
call :Get "%file%"
pause
goto :eof

:Get
for %%i in (%*) do echo %%~i
goto :eof
```
General use should still be the above.
Floor11 namejm Posted 2006-10-19 08:40
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The 10F version cannot correctly handle paths with spaces, while the 7F version can; if you want to get the folder name of a specified level, the 10F version is a bit more concise, and the 7F version can also be implemented, but the code will be longer. Considering from the perspective of generality, I still think the 7F version is truly universal.

————————————————————————————————————
  After re-testing, it is proved that the statement in the above blue content that the 10F code cannot handle spaces is seriously inaccurate. It seems that the 10F version is truly universal.

  Attached is my test code (intention: get the second-level folder name in the path; result: cannot correctly get the folder name of the specified level):


[ Last edited by namejm on 2006-10-21 at 06:13 ]
Floor12 3742668 Posted 2006-10-20 08:15
荣誉版主 Posts 718 Credits 2,013
Floor13 namejm Posted 2006-10-21 01:30
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Hehe, it has been tested. It can indeed handle paths with spaces, but it cannot correctly obtain the folder name of the specified level in the space path.
Floor14 3742668 Posted 2006-10-21 01:42
荣誉版主 Posts 718 Credits 2,013
NO What Is Cann't...(Chinese English)
Floor15 lxmxn Posted 2006-10-21 02:14
版主 Posts 4,938 Credits 11,386
  Hehe, the moderator's performance is wonderful...

  

  Learned...
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023