|
yardian
中级用户
  
积分 305
发帖 85
注册 2005-5-23
状态 离线
|
『楼 主』:
[请教]如何获取绝对路径中文件名的最后一部分?[已解决]
使用 LLM 解释/回答一下
不会编程,对dos的批处理也不是很熟.想写个脚本真难.又遇到一个新问题:
我现在想获取包含绝对路径的文件名的最后一部分,请问批处理如何写?
比如:a:\c\E\b.txt(路径长度不固定,可能有很多层目录),现在我想建一个变量,让这个变量的值为b.txt,请问如何用批处理解决?
Last edited by yardian on 2006-10-19 at 11:24 ]
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 ]
|
|
2006-10-19 01:38 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
@echo off
set file=c:\abc\efg\xxx.xxx
call :Print "%file%"
pause
goto :eof
:Print
echo %~nx1
goto :eof
代码直接在回贴里面写的,未测试...
@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...
|
|
2006-10-19 01:49 |
|
|
yardian
中级用户
  
积分 305
发帖 85
注册 2005-5-23
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
谢谢!要的就是这种效果!
Thanks! This is exactly the effect I want!
|
|
2006-10-19 02:10 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
3742668的代码可以简化一下:
@echo off
set file=c:\abc\efg\xxx.xxx
for /f "delimes=" %%i in ("%file%") do echo %%~nxi
pause
发现了一个小bug,修正了一下,可以适应路径名为空格的情形。
Last edited by namejm on 2006-10-19 at 06:24 ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-19 06:01 |
|
|
lanlaila
新手上路

积分 6
发帖 3
注册 2006-10-18
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
这里学习气氛真好,我要好好学习了
The learning atmosphere here is really good, I'm going to study hard.
|
|
2006-10-19 06:04 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
真不错!
3742668版主是把文件路径用call转化成%1的变量, 为扩充到文件名和后缀名做准备.
而namejm版主直接使用for将文件路径转化成可替换变量, 再执行扩充.
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.
|
|
2006-10-19 06:10 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
@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
以上代码均未测试...
```
@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...
```
|
|
2006-10-19 07:46 |
|
|
yardian
中级用户
  
积分 305
发帖 85
注册 2005-5-23
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
大家都厉害,还是这里气氛好啊,多谢各位了
Everyone is very capable, or here the atmosphere is good, thanks everyone
|
|
2006-10-19 07:47 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
7F的第二段代码可能是最通用的。把它扩展一下,就可以获取指定层次的文件夹名了。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-19 08:02 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
@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
通用还是要用上面的.
```
@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.
|
|
2006-10-19 08:10 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
10F的不能正确处理带空格的路径,而7F的可以处理;如果要获取指定层次的文件夹名的话,10F的更简洁一点,而7F的也可以实现,只是代码会长一点。如果从通用性上考虑的话,我还是觉得7F的才是真正的通用。
————————————————————————————————————
经过再次测试证明,以上蓝色内容中关于10F的代码不能处理空格的言论严重失实,看来10F的才是真正的通用的。
附上我的测试代码(意图:获取路径中第二层文件夹名;结果:不能正确获取指定层次的文件夹名):
@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 ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-19 08:40 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
|
2006-10-20 08:15 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
呵呵,测试过了,确实能处理带空格的路径,但是不能正确获取空格路径中指定层次的文件夹名。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-21 01:30 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
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
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
|
|
2006-10-21 01:42 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
呵呵,版主表演得精彩……
学习了。。。
Hehe, the moderator's performance is wonderful...
Learned...
|
|
2006-10-21 02:14 |
|