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:48
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Ask] How to get the last part of the file name in the absolute path? [Solved] View 6,631 Replies 25
Original Poster Posted 2006-10-19 01:38 ·  中国 浙江 杭州 电信
中级用户
★★
Credits 305
Posts 85
Joined 2005-05-23 00:00
21-year member
UID 39004
Gender Male
Status Offline
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 ]
Floor 2 Posted 2006-10-19 01:49 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline

@echo off
set file=c:\abc\efg\xxx.xxx
call :Print "%file%"
pause
goto :eof

:Print
echo %~nx1
goto :eof

The code is written directly in the reply, not tested...
Floor 3 Posted 2006-10-19 02:10 ·  中国 浙江 杭州 电信
中级用户
★★
Credits 305
Posts 85
Joined 2005-05-23 00:00
21-year member
UID 39004
Gender Male
Status Offline
Thanks! This is exactly the effect I want!
Floor 4 Posted 2006-10-19 06:01 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The code of 3742668 can be simplified:

@echo off
set file=c:\abc\efg\xxx.xxx
for /f "delims=" %%i in ("%file%") do echo %%~nxi
pause


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 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-10-19 06:04 ·  中国 湖北 仙桃 电信
新手上路
Credits 6
Posts 3
Joined 2006-10-18 09:22
19-year member
UID 66291
Status Offline
The learning atmosphere here is really good, I'm going to study hard.
Floor 6 Posted 2006-10-19 06:10 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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.
Floor 7 Posted 2006-10-19 07:46 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
```
@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...
```
Floor 8 Posted 2006-10-19 07:47 ·  中国 浙江 杭州 电信
中级用户
★★
Credits 305
Posts 85
Joined 2005-05-23 00:00
21-year member
UID 39004
Gender Male
Status Offline
Everyone is very capable, or here the atmosphere is good, thanks everyone
Floor 9 Posted 2006-10-19 08:02 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-10-19 08:10 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
```
@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.
Floor 11 Posted 2006-10-19 08:40 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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):

@echo off
set file=c:\ab c\def\gh .exe
set file=%file:\=" "%
call :Get "%file%"
pause
goto :eof

:Get
for /f "tokens=2" %%i in (%*) do echo %%~i
goto :eof


[ Last edited by namejm on 2006-10-21 at 06:13 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-10-20 08:15 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Floor 13 Posted 2006-10-21 01:30 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 14 Posted 2006-10-21 01:42 ·  中国 湖北 荆门沙洋区 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
NO What Is Cann't...(Chinese English)
@echo off
set file=c:\ab c\def\gh .exe
set file=%file:\=" "%
call :Get 4 "%file%"
pause
goto :eof

:Get
for /l %%i in (1,1,%1) do shift
echo %~1
goto :eof
Floor 15 Posted 2006-10-21 02:14 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
  Hehe, the moderator's performance is wonderful...

  

  Learned...
Forum Jump: