|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
   『楼 主』:
[挑战1] 目录最后一个名称的提取[ 难度:☆]
使用 LLM 解释/回答一下
我们这一期的题目 :目录最后一个名称的提取
因为任意目录的名称都不一样。。但都满足于
c:\aaa\bbb\ccc
的格式,那么我们的要求就是 提出 ccc。
这个格式可以通过 dir /b /s /ad 来获取。
可以使用 vbs 来提取
但不得通过第三方工具。。。。。。。。。。。。。。。。。。。。
挑战人的关键字: set /a ,变量延迟 , if not defined
不得使用:for 或 call 时 的特殊参数。。例如 %~nxi 之类的。
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 在%aaa%目录中
echo 提取的文件夹名为:"!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 ]
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 ]
|

知,不觉多。不知,乃求知 |
|
2007-4-30 09:42 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
其实这个问题在论坛里也讨论过了。
测试代码如下:
@echo off
set "FolderPath=测 试\te st\tes t123\OK"
for /f "delims=" %%i in ("%FolderPath%") do echo %%~ni
pause>nul
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
|
|
2007-4-30 09:58 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
 『第 3 楼』:
使用 LLM 解释/回答一下
当然以上的代码利用了 %%~ni的特殊性。
其实,还有一种思路,就是利用递归方法来获得 文件夹 名,测试代码如下:
@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"
)
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"
)
|
|
2007-4-30 10:11 |
|
|
baomaboy
银牌会员
    
积分 1513
发帖 554
注册 2005-12-30
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
哦 原来这样取,刚才我还以为要取得最后的有效路径呢,就是判断磁盘上此路径是否真实存在。
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.
|

好多菩提树,好多明镜台。本来好多物,好多的尘埃。 |
|
2007-4-30 10:13 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
如果要获得 文件夹 的层数的话,可能用第二种方法比较好。
::code by youxi01@cn-dos.net
::获取文件夹的名称及层数
@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 文件夹名:%%i & call echo 处于第%%num%%层 & goto :eof
call :GetFdName "%%j"
)
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"
)
|
|
2007-4-30 10:16 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
对三楼的代码的讲解:
@echo off
set "FolderPath=测 试\te st\tes t123\OK"
call :GetFdName "%FolderPath%" || rem 使用子程序。。
pause>nul
:GetFdName
for /f "delims=\ tokens=1,*" %%i in ("%~1") do (
if "%%j"=="" echo %%i & goto :eof
call :GetFdName "%%j"
)
上面的是这个代码的精髓所在。。不停的把
a\b\c\d\e\f 的形式转换成
a 和 b\c\d\e\f
b 和 c\d\e\f
c 和 d\e\f
d 和 e\f
e 和 f
f 和 空。。
若后面一个数为空,则 %%i 就是所需要的目录名。
代码实在比我高百倍:)呵呵。。。
下一期难度会提高。。敬请各位高手关注。。
Last edited by flyinspace on 2007-4-30 at 12:14 AM ]
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 ]
|

知,不觉多。不知,乃求知 |
|
2007-4-30 13:00 |
|
|
baomaboy
银牌会员
    
积分 1513
发帖 554
注册 2005-12-30
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by flyinspace at 2007-4-30 13:00:
寒! 不好意思,我出题目的时候,忘记申明一些东西了。。
我原来的意思是对不确定的层数进行提取。。
例如: asdf\aasdf\adlj\als\adfa\adf
我们总 ...
不一定是目录,那是甚么意思?
不一定是目录,那是甚么意思?
|

好多菩提树,好多明镜台。本来好多物,好多的尘埃。 |
|
2007-4-30 13:07 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
也不是很明白他的意思,他的意思可能是还有些是文件吧。
或者那个目录有很多层,但是他想提取指定的层?
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?
|
|
2007-4-30 13:15 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
举个例子:
现在我们有一些东西要出售。。
前面是对这个东西的描叙。而最后一项是这个东西的价格。
前面的描叙项不定,而我们只提取最后一项。
这个是我出题的疏忽。。
下次再出题的时候,我会尽量注意的。。
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.
|

知,不觉多。不知,乃求知 |
|
2007-4-30 13:20 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by flyinspace at 2007-4-30 01:20 PM:
举个例子:
前面的描叙项不定,而我们只提取最后 ...
越来越不明白,现在的代码不就是提取最后一项吗?
如果要提取指定层数,也很简单。
在第二种办法的基础上,启用延迟变量,然后加个if 检测 :
if !num! gtr ... &... & goto :eof
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
|
|
2007-4-30 13:24 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
呵呵,是我表达出了问题:)
不好意思。。
因为第一次组织这样的活动。。难免有考虑不周的地方。。请原谅。。
还有youxi01 你的代码已经写得很好了。。
真不好意思。。
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..
|

知,不觉多。不知,乃求知 |
|
2007-4-30 13:28 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
其实,你才最值得奖励。
我们应该为你的 努力 加油!!
说实话,提一些有意义的问题,然后大家讨论解决,确实有助于提高论坛人气,也有助于一些新手快速成长起来!当然我自己也是新手,努力学习中...
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...
|
|
2007-4-30 13:34 |
|
|
baomaboy
银牌会员
    
积分 1513
发帖 554
注册 2005-12-30
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
唉 第一次经历 难免,
就算路径最后取得的是“我是程序.exe”也不说明它不是文件夹啊,"我是程序.exe"怎么不能做文件夹名吗!
其实从一开始 我都一相情愿的认为 该贴的要求是:
取得(验证)aaa=c:\windows\system32\playuo\cndos\asd adf中的最后有效路径,也就是说验证aaa这个路径中的最后有效路径,如果cndos和asd adf这两个文件夹都不存在的话,最后返回的结果应该是playuo
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
|

好多菩提树,好多明镜台。本来好多物,好多的尘埃。 |
|
2007-4-30 13:40 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
Originally posted by baomaboy at 2007-4-30 12:40 AM:
唉 第一次经历 难免,
就算路径最后取得的是“我是程序.exe”也不说明它不是文件夹啊,"我是程序.exe"怎么不能做文件夹名吗!
其实从一开始 ...
呵呵。你的这个办法很好啊。。
完全可以按照这个来嘛:)
这个完全可以做下一期的挑战题目嘛:)
Last edited by flyinspace on 2007-4-30 at 12:59 AM ]
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 ]
|

知,不觉多。不知,乃求知 |
|
2007-4-30 13:54 |
|
|
notepad
新手上路

积分 10
发帖 4
注册 2007-4-20
状态 离线
|
|
2007-5-1 02:01 |
|