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:50
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Challenge 1] Extract the last name in the directory [Difficulty: ☆] View 3,757 Replies 31
Original Poster Posted 2007-04-30 09:42 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
The topic of this issue: Extraction of the last name in the directory

Because the names of any directories are different. But they all meet the format of

c:\aaa\bbb\ccc

So our requirement is to extract ccc.

This format can be obtained through dir /b /s /ad.

VBS can be used for extraction
But third-party tools are not allowed...............
Challenge keywords: set /a, variable delay, if not defined

Not allowed: special parameters when using for or call, such as %~nxi and so on.


set "aaa=c:\windows\system32\playuo\cndos\asd adf"
set /a "count=0","count1=0"
set "FileName="

for /f "tokens=* " %%i in ("%aaa%") do (
set str=%%~i
for /l %%a in (0,1,255) do if "!str:~%%a,1!"=="" if not defined len set len=%%a
for /l %%a in (0,1,255) do if "!str:~%%a,1!"=="\" set /a count+=1
for /l %%a in (0,1,255) do (
set abc=!str:~%%a,1!
if "!count!"=="!count1!" (
set /a "lenflag=!len!-%%a"
if not defined FileName call :GetName "!lenflag!" "%%a"
)
if "!abc!"=="\" set /a count1=!count1!+1
)
echo In the %aaa% directory
echo The extracted folder name is: "!FileName!"
)
goto :END
:GetName _len_ _len_
set "parm1=%~1"
set "parm2=%~2"
set "FileName=!str:~%parm2%,%parm1%!
goto :EOF
:END

pause



[ Last edited by flyinspace on 2007-4-30 at 05:03 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
youxi01 +8 2007-04-30 13:31
知,不觉多。不知,乃求知
Floor 2 Posted 2007-04-30 09:58 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Actually, this question has also been discussed in the forum.

The test code is as follows:

@echo off

set "FolderPath=测 试\te st\tes t123\OK"

for /f "delims=" %%i in ("%FolderPath%") do echo %%~ni

pause>nul
Floor 3 Posted 2007-04-30 10:11 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Of course, the above code makes use of the particularity of %%~ni.
In fact, there is another idea, which is to use the recursive method to obtain the folder name. The test code is as follows:

@echo off
set "FolderPath=测 试\te st\tes t123\OK"
call :GetFdName "%FolderPath%"

pause>nul

:GetFdName
for /f "delims=\ tokens=1,*" %%i in ("%~1") do (
if "%%j"=="" echo %%i & goto :eof
call :GetFdName "%%j"
)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +3 2007-04-30 13:01
Floor 4 Posted 2007-04-30 10:13 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
Oh, so that's how to get it. Just now I thought I needed to get the final valid path, which is to judge whether this path on the disk really exists.
Floor 5 Posted 2007-04-30 10:16 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
If you want to get the number of layers of a folder, the second method may be better.


::code by youxi01@cn-dos.net
::Get folder name and layers

@echo off
set "FolderPath=测 试\te st\tes t123\OK"
call :GetFdName "%FolderPath%"

pause>nul

:GetFdName
for /f "delims=\ tokens=1,*" %%i in ("%~1") do (
set/a num+=1
if "%%j"=="" echo Folder name: %%i & call echo At layer %%num%% & goto :eof
call :GetFdName "%%j"
)
Floor 6 Posted 2007-04-30 13:00 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Explanation of the code on the third floor:


@echo off
set "FolderPath=测试\te st\tes t123\OK"
call :GetFdName "%FolderPath%" || rem Use subroutine..

pause>nul

:GetFdName
for /f "delims=\ tokens=1,*" %%i in ("%~1") do (
if "%%j"=="" echo %%i & goto :eof
call :GetFdName "%%j"
)

The above is the essence of this code. It keeps converting
the form of a\b\c\d\e\f into
a and b\c\d\e\f
b and c\d\e\f
c and d\e\f
d and e\f
e and f
f and empty..

If the latter number is empty, then %%i is the required directory name.

The code is really hundreds of times better than mine : ) Hehe...




The difficulty of the next issue will increase.. Please pay attention, all experts..

[ Last edited by flyinspace on 2007-4-30 at 12:14 AM ]
知,不觉多。不知,乃求知
Floor 7 Posted 2007-04-30 13:07 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
Floor 8 Posted 2007-04-30 13:15 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
I don't quite understand what he means. Maybe he means there are still some files. Or there are many layers in that directory, but he wants to extract the specified layer?
Floor 9 Posted 2007-04-30 13:20 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
For example:

Now we have some items for sale..

The front is the description of this item. And the last item is the price of this item.

The number of front description items is not fixed, and we only extract the last item.

This is a negligence in my question setting.

Next time when setting questions, I will try to pay attention to it.
知,不觉多。不知,乃求知
Floor 10 Posted 2007-04-30 13:24 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Originally posted by flyinspace at 2007-4-30 01:20 PM:
For example:
The preceding description items are indefinite, and we only extract the last...

More and more confused. Isn't the current code just extracting the last item?
If you want to extract a specified layer, it's also very simple.
On the basis of the second method, enable delayed variables, and then add an if check:
if !num! gtr... &... & goto :eof
Floor 11 Posted 2007-04-30 13:28 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Hehe, it's my expression that was problematic : )

I'm sorry..

Because it's the first time to organize such an activity.. There are inevitably places that haven't been considered properly.. Please forgive me..

Also, your code youxi01 is already very good..

I'm really sorry..
知,不觉多。不知,乃求知
Floor 12 Posted 2007-04-30 13:34 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Actually, you deserve the most reward.
We should cheer for your efforts!!
To be honest, raising some meaningful questions and then having everyone discuss and solve them really helps increase forum popularity and also helps some newbies grow up quickly! Of course, I am also a newbie, working hard to learn...
Floor 13 Posted 2007-04-30 13:40 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
Hey, it's the first time experiencing this, so it's inevitable.
Even if the path finally obtained is "I am program.exe", it doesn't mean it's not a folder. Why can't "I am program.exe" be a folder name!
Actually from the very beginning, I've been wishfully thinking that the requirement of this post is:
To obtain (verify) aaa=c:\windows\system32\playuo\cndos\asd adf, and get the last valid path, that is, verify the last valid path in the aaa path. If both the cndos and asd adf folders don't exist, the final returned result should be playuo
Floor 14 Posted 2007-04-30 13:54 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by baomaboy at 2007-4-30 12:40 AM:
Oh, it's inevitable for the first time.
Even if the final path gets "I am program.exe", it doesn't mean it's not a folder. Why can't "I am program.exe" be a folder name!
Actually from the very beginning...


Hehe. Your this method is very good ah. ..

Completely may act according to this to come:)

This may completely make the next issue the challenge topic:)

[ Last edited by flyinspace on 2007-4-30 at 12:59 AM ]
知,不觉多。不知,乃求知
Floor 15 Posted 2007-05-01 02:01 ·  中国 广东 佛山 禅城区 电信
新手上路
Credits 10
Posts 4
Joined 2007-04-20 06:50
19-year member
UID 85818
Gender Male
Status Offline
Forum Jump: