中国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-03 10:52
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » How to get the substring after the last "\" in the path [Perfect Solution]
Printable Version  2,613 / 25
Floor16 HUNRYBECKY Posted 2007-02-13 22:35
银牌会员 Posts 442 Credits 1,179
Originally posted by HUNRYBECKY at 2007-2-13 04:20:

─────────────────── Moderation Record ────────────────────
Performed by: namejm
Description: {tid=27658} and the owner ...



Thank you for the punishment. At that time, I was in a hurry and the holiday was coming. I wanted to make the processing before the holiday. I might surf the Internet rarely after the holiday, which was not very convenient. So I posted multiple posts to speed up the response speed. I will remember it in the future.
Floor17 HUNRYBECKY Posted 2007-02-13 22:57
银牌会员 Posts 442 Credits 1,179
Let me ask the experts another question: I want to decide which input methods to delete and back up according to the user's choice of IME.SVE in the following batch processing IME.BAT, but I encountered a problem:

ime\IMJP8_1
system32\imjp81.ime
system32\imjp81k.dll
system32\kbdjpn.dll
system32\msir3jp.dll
system32\msir3jp.lex
ime\IMJP8_1 is a folder, indicating Japanese input, and the following several are several related files of Japanese input. When I back up, I need to compress this directory and other several related files together into a folder named such as IMJP8_1. How to do it? Because there are three difficulties here: First, how to name by the last \ name of the folder when the path level is uncertain. The problem has been solved by brother ZH159 on the 12th floor. Second, if to distinguish and judge whether what is listed in IME.SVE is a folder or a file. I use FINDSTR + extension judgment, but there are still some problems; Third, how to add files to the corresponding compressed package according to the judgment of the second difficulty?

Maybe my description is not very clear, and my expression ability is not good. Please forgive me. See the following code

The content of batch processing IME.BAT



The content of IME.SVE



[ Last edited by HUNRYBECKY on 2007-2-13 at 11:03 PM ]
Floor18 HUNRYBECKY Posted 2007-02-13 23:03
银牌会员 Posts 442 Credits 1,179
Another small question:
1. How to add colors and other things to the text to highlight it when posting a thread? Why don't I have these display options here?
2. It is suggested that the moderator modify the word limit of the post to 100 characters. The word limit is too low, making it difficult to express clearly.
Floor19 zh159 Posted 2007-02-13 23:10
金牌会员 Posts 1,467 Credits 3,687
Determine whether it is a folder (shortcoming: cannot distinguish folders with names containing "."):



list.txt
Floor20 HUNRYBECKY Posted 2007-02-13 23:32
银牌会员 Posts 442 Credits 1,179
Originally posted by zh159 at 2007-2-13 23:10:
Judge whether it is a folder (disadvantage: cannot distinguish folders with names containing "."):


I added FOR + FINDSTR in the code to jointly judge according to the obtained variable I, but unfortunately, ECHO %%I ^|FINDSTR seems to have a problem in the FOR statement, and the ideal result is not obtained at all. That is to say, the obtained result is that %%A is actually the same as %%I, which is equivalent to nothing. But when I test it alone, that is, without using it in the FOR statement, it is successful. I really don't understand it.
Floor21 zh159 Posted 2007-02-13 23:46
金牌会员 Posts 1,467 Credits 3,687
It seems this is a batch script content. The translation of the Chinese part "要扩展名简单啊:" is "It's simple to get the file extension:". The whole content after translation is:

It's simple to get the file extension:
@echo off
for /f "delims=" %%i in (list.txt) do if not "%%~xi" == "" echo %%~nxi
pause
Floor22 HUNRYBECKY Posted 2007-02-14 00:32
银牌会员 Posts 442 Credits 1,179
Originally posted by zh159 at 2007-2-13 23:46:
The extension should be simple:
@echo off
for /f "delims=" %%i in (list.txt) do if not "%%~xi" == "" echo %%~nxi
pause


Thank you very much for your answer. Just now I have been testing, and the problem of the extension has been solved. But the second problem I just mentioned is very difficult to solve. How to keep the obtained file name so that when appending to the previous folder directly? Because if it is not a folder, the variable will overwrite the original variable.

In order to facilitate testing, I uploaded the entire file. Please run the TEST.BAT inside to test, thank you

[ Last edited by HUNRYBECKY on 2007-2-14 at 12:52 AM ]

Attachments
test.rar (256.19 KiB)
Floor23 HUNRYBECKY Posted 2007-02-14 02:10
银牌会员 Posts 442 Credits 1,179
Bump my own post. I started having a holiday this afternoon. Hope someone can give me an idea before the holiday. Thanks.
Wish everyone a happy new year and all the best.
Floor24 HUNRYBECKY Posted 2007-02-14 03:08
银牌会员 Posts 442 Credits 1,179
After continuous conception and debugging, it was finally done. Actually, it's very simple because the variable needs to be replaced each time. Even using the SET statement is the same. Then, can it be like setting global variables in VB, VBS? After testing, writing the variable into a text file and then reading it achieves the effect of imitating global variables in VB, VBS. After testing, the following code completely achieves the effect I wanted. No effort is in vain for those who put in the effort. At the same time, I'm very grateful to ZH159, VIKILL and others for answering my questions.
Test: Please download the code on floor 22 and replace the content in TEST.BAT with the following content
Floor25 csx163 Posted 2007-02-14 05:35
初级用户 Posts 45 Credits 97
%~nI - Expand %I to a file name only
%~xI - Expand %I to a file extension only
Can %~nxI be used like this??
Floor26 zh159 Posted 2007-02-14 06:33
金牌会员 Posts 1,467 Credits 3,687
for/?
You can combine modifiers to get multiple results:

%~dpI - Expand %I to just a drive letter and path
%~nxI - Expand %I to just a file name and extension
%~fsI - Expand %I to a full path name with a short name
%~dp$PATH:i - Search the directories listed in the PATH environment variable and expand %I to the first found drive letter and path.
%~ftzaI - Expand %I to a DIR-like output line


[ Last edited by zh159 on 2007-2-13 at 05:34 PM ]
Prev  1 2
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023