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 08:24
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Ask] How to get the last part of the file name in the absolute path? [Solved] View 6,634 Replies 25
Floor 16 Posted 2006-10-21 02:54 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

Brother namajm, is the code in F11 a test error code or a correct code?

I tested here and it didn't pass. It shows "ab" instead of the "ab c" we want. I don't know why.
Floor 17 Posted 2006-10-21 06:15 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
I'm sorry, the example I gave was to show that the code couldn't correctly get the folder name of the specified level in the path with spaces. I didn't make it clear at that time, but now it's commented properly.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 18 Posted 2006-10-21 06:17 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The code in 14F is really remarkable. It uses a relatively obscure shift, which is quite skillful. It might be the most general-purpose one.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 19 Posted 2006-10-21 07:03 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Hehe...

  I've been thinking about this question all the time:

  Why does the code on floor 11 not work, but the code on floor 14 does? Is it just the role of this shift? I still don't understand why we need to loop 4 times.
Floor 20 Posted 2006-10-21 07:35 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Take a look at <Help and Support> more. Different combinations have different flavors, which is also one of the reasons for the existence of scripts:


@echo off
setlocal ENABLEDELAYEDEXPANSION
set str=c:\ab c\def\gh .exe\
set num=1
for /l %%i in (1,1,%num%) do set "str=!str:*\=!"
echo %str:\= && 2>nul cd %
pause

Changing the final cd to echo allows displaying level by level. The several methods are used in different situations, and as for which is better or worse, that is a matter of personal opinion.
Floor 21 Posted 2006-10-21 08:39 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
I found the sentence "echo %str:\= && 2>nul cd %". I can't figure out how to write to remove the last few content separated by \. I hope 3742668 can explain it.

In addition, when extracting the folder name from "echo %str:\= && 2>nul cd %", the last character is a space. There is no this problem when extracting the file name. It can be changed to "echo %str:\=&& 2>nul cd %" (note: there is a space before && in the two statements and no space in one).
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 22 Posted 2006-10-21 08:43 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Originally posted by 3742668 at 2006-10-21 07:35:
Take a look at the <Help and Support> more.
Different combinations have different flavors, which is also one of the reasons why scripts exist:
Several methods are used in different situations, and as for which is better or worse, that is a matter of personal opinion.


Agree, there are various ways to solve problems, just see how you use them.
Floor 23 Posted 2006-10-21 09:00 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Originally posted by namejm at 2006-10-21 08:39:
  I found that in the sentence "echo %str:\= && 2>nul cd %", replacing "cd" with any command can execute correctly. I can't figure out the writing method of removing the last few \-separated contents. I still need to ask 37426...

I remember that a similar method was used in that post discussing encryption and decryption.
The principle is very simple, for example:

set "command=set i=3"
echo i的值:%i% & pause
%command%
echo i的值:%i% & pause

Reference: Non-compiled Batch Processing Encryption Scheme and Code of 11F scheme.
In addition, in fact, there is still a small problem with the last method. When getting the final file name, a prompt like "ECHO is on" might be displayed. To save code quantity, it was not processed.
Floor 24 Posted 2006-10-24 03:32 ·  中国 上海 虹口区 电信
初级用户
★★
Credits 153
Posts 68
Joined 2006-10-12 22:25
19-year member
UID 65507
From 上海
Status Offline
Originally posted by 3742668 at 2006-10-21 09:00:

I remember that a similar method was used in that post discussing encryption and decryption.
The principle is very simple, for example:

set "command=set i=3"
echo i的值:%i% & pause
%command%
ech ...



After testing, it does appear: "The prompt that ECHO is on/off", I don't know how to solve this problem, I often encounter this problem...
个人主页-找餐厅:http://www.canting.cc
Floor 25 Posted 2006-10-24 06:39 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
May I ask under what circumstances the prompt "ECHO is in the on/off state" appears? Please send out the test path for me to take a look. It is estimated that changing echo %str:\= && 2>nul cd % to echo. %str:\=&& 2>nul cd % will work (note that there is a dot after echo).
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 26 Posted 2006-10-24 21:58 ·  中国 湖南 娄底 电信
初级用户
Credits 46
Posts 22
Joined 2006-10-24 09:03
19-year member
UID 68024
Gender Male
Status Offline
Well, strong! Worth learning~!
Forum Jump: