中国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 14:58
47,811 topics / 349,897 posts / today 0 new / 48,255 members
DOS批处理 & 脚本技术(批处理室) » [Challenge 1] Extract the last name in the directory [Difficulty: ☆]
Printable Version  3,975 / 31
Floor1 flyinspace Posted 2007-04-30 09:42
银牌会员 Posts 517 Credits 1,206
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 ]
Floor2 youxi01 Posted 2007-04-30 09:58
高级用户 Posts 247 Credits 846 From 湖南==》广东
Actually, this question has also been discussed in the forum.

The test code is as follows:
Floor3 youxi01 Posted 2007-04-30 10:11
高级用户 Posts 247 Credits 846 From 湖南==》广东
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:
Floor4 baomaboy Posted 2007-04-30 10:13
银牌会员 Posts 554 Credits 1,513
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.
Floor5 youxi01 Posted 2007-04-30 10:16
高级用户 Posts 247 Credits 846 From 湖南==》广东
If you want to get the number of layers of a folder, the second method may be better.

Floor6 flyinspace Posted 2007-04-30 13:00
银牌会员 Posts 517 Credits 1,206
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 ]
Floor7 baomaboy Posted 2007-04-30 13:07
银牌会员 Posts 554 Credits 1,513
不一定是目录,那是甚么意思?
Floor8 youxi01 Posted 2007-04-30 13:15
高级用户 Posts 247 Credits 846 From 湖南==》广东
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?
Floor9 flyinspace Posted 2007-04-30 13:20
银牌会员 Posts 517 Credits 1,206
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.
Floor10 youxi01 Posted 2007-04-30 13:24
高级用户 Posts 247 Credits 846 From 湖南==》广东
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
Floor11 flyinspace Posted 2007-04-30 13:28
银牌会员 Posts 517 Credits 1,206
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..
Floor12 youxi01 Posted 2007-04-30 13:34
高级用户 Posts 247 Credits 846 From 湖南==》广东
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...
Floor13 baomaboy Posted 2007-04-30 13:40
银牌会员 Posts 554 Credits 1,513
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
Floor14 flyinspace Posted 2007-04-30 13:54
银牌会员 Posts 517 Credits 1,206
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 ]
Floor15 notepad Posted 2007-05-01 02:01
新手上路 Posts 4 Credits 10
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023