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-08-03 10:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to get the substring after the last "\" in the path [Perfect Solution] View 2,609 Replies 25
Original Poster Posted 2007-02-13 02:45 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Solved, see http://www.cn-dos.net/forum/viewthread.php?tid=27658&page=1&sid=FB2nu4#pid174202 Post 2 building ZH159 brother's answer.

Below is my code, please modify

@ECHO OFF
SET PATH=%CD%;%CD%\TOOLS\;%PATH%

FOR /F "EOL=;DELIMS=" %%I in (.\Purge\PurgeS.SVE) DO (
ECHO Compressing and backing up files under %windir%\%%I\, please wait...
PING 127.0.0.1 -n %TIMEOUT% >NUL
RAR.EXE A -EP3 -IDQ ".\BACKUP\%%~nI" "%windir%\%%I"
)
PAUSE


The content in PurgeS.SVE is as follows:

; Here you can set the system-level garbage files to be backed up and cleaned. They must be represented by folders and must be represented with %windir% as the root.
; If font cleaning is set, you need to set the font files to be retained in FONTS.SVE.
; Input method cleaning should be carried out separately in IME.SVE.
;
;
; Folders that can be backed up and cleaned. If you want to retain them, use ; to comment out the items to be cleaned; you can also add the contents of other folders that need to be backed up and cleaned, such as the wallpapers under Web\Wallpaper
; Fonts
FONTS
; Help
HELP
; Help support center
PCHealth
; Driver cache
Driver Cache
; DLL cache
SYSTEM32\dllcache
; System repair backup
REPAIR
; Prefetch
prefetch
; Search assistant
srchasst
; Microsoft Agent
msagent
; System activation (only for VOL version)
; system32\oobe


Everyone should have understood the meaning of my code. I am using the RAR program to compress and back up all the files in the folder specified in PurgeS.SVE to the current BACKUP directory. But when running to SYSTEM32\dllcache with subdirectories, an error occurs, prompting that the path of the compressed file to be backed up is not found. Because I want to back up directly to the BACKUP directory regardless of whether there are subdirectories, and determine the name of the compressed file with the name of the last subdirectory of the directory. For example, the backup of SYSTEM32\dllcache should be dllcache.rar and must be in the BACKUP directory.

I hope everyone can give me a solution idea or code. It is required to use batch processing, not use VBS, and the running environment is WINXP. Thank you.

Regarding the RAR command, I just sent my study notes to http://www.cn-dos.net/forum/viewthread.php?tid=27657&sid=FB2nu4, which can be referred to.


There is a new problem in building 17, waiting online for the masters' answer again

[ Last edited by HUNRYBECKY on 2007-2-14 at 03:09 AM ]
Floor 2 Posted 2007-02-13 02:49 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
### Step 1: Analyze the code
- `Str1 = "C:\WINDOWS\IME\CINTLGNT"`: This is a string variable assigned a path.
- `Pos = InstrRev(Str1, "\")`: `InstrRev` is a function to find the last occurrence of a character in a string. Here, it finds the last backslash in `Str1`.
- `mid(Str1, Pos + 1, len(Str1) - Pos)`: `Mid` function is used to extract a substring. It starts from the position after the last backslash and takes the remaining characters.

### Step 2: Translate the code's functionality description (since the code itself is in English-like structure but we need to ensure correct translation of the logic description)
The code first defines a string `Str1` with a path, then finds the position of the last backslash in `Str1`, and then extracts the substring starting from the character after the last backslash.

In terms of the final result, the code will extract the last part of the path after the last backslash. The translated code execution result is that the message box will display "CINTLGNT".

But according to the requirements, we just translate the original text as per the rules. The original code is in a programming language (VBScript like), and when translating the content:

The code lines are:
```vb
dim Str1,Pos
Str1="C:\WINDOWS\IME\CINTLGNT"
Pos=InstrRev(Str1,"\")
msgbox mid(Str1,Pos+1,len(Str1)-Pos)
```

Translated:
```vb
dim Str1, Pos
Str1 = "C:\WINDOWS\IME\CINTLGNT"
Pos = InstrRev(Str1, "\")
msgbox Mid(Str1, Pos + 1, Len(Str1) - Pos)
```

The result of running this code is to display "CINTLGNT" in the message box. But following the instruction to just provide the translated text as per the input, the translated code is as above, and the result of the code is "CINTLGNT". So the final output related to the translation of the code content is:

```vb
dim Str1, Pos
Str1 = "C:\WINDOWS\IME\CINTLGNT"
Pos = InstrRev(Str1, "\")
msgbox Mid(Str1, Pos + 1, Len(Str1) - Pos)
```

And the actual value shown in the message box is "CINTLGNT"
Floor 3 Posted 2007-02-13 02:58 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Originally posted by slore at 2007-2-13 02:49:
dim Str1,Pos
Str1="C:\WINDOWS\IME\CINTLGNT"
Pos=InstrRev(Str1,"\")
msgbox mid(Str1,Pos+1,len(Str1)-Pos)


Thanks for the answer, I want to use batch processing, not VBS
Floor 4 Posted 2007-02-13 03:00 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
for /f "tokens=*" %? in ('dir /ad/b c:\') do (echo %?|sed -r "s/.*\\([^\]*)$/\1/")
Floor 5 Posted 2007-02-13 03:01 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
### Principle of getting the substring after the last "\" in the path :)

FOR /?


%~nI - Expand %I to a file name only
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 6 Posted 2007-02-13 03:09 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Originally posted by vkill at 2007-2-13 03:00:
for /f "tokens=*" %? in ('dir /ad/b c:\') do (echo %?|sed -r "s/.*\\(*)$/\1/")


I don't want to use SED, thank you Brother VKILL for your answer. Is there any other way?
Floor 7 Posted 2007-02-13 03:15 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
### Re: 获取路径最后一个"\"后的字串原理:)

Thanks for REDTEK brother's answer, but I use it as follows, it seems wrong, can you modify it.

@ECHO OFF
SET PATH=%CD%;%CD%\TOOLS\;%PATH%

FOR /F "EOL=;DELIMS=" %%I in (.\Purge\PurgeS.SVE) DO (
ECHO Compressing and backing up files under %windir%\%%I\, please wait a moment......
PING 127.0.0.1 -n %TIMEOUT% >NUL
RAR.EXE A -EP3 -IDQ ".\BACKUP\%%I:%~nI" "%windir%\%%I"
)
PAUSE
Floor 8 Posted 2007-02-13 03:28 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
@echo off
FOR /F "EOL=;DELIMS=" %%I in (PurgeS.SVE) DO (
ECHO Compressing and backing up files under %windir%\%%I\, please wait...
PING 127.0.0.1 -n %TIMEOUT% >NUL
for /F "tokens=2 DELIMS=\" %%J IN ("%%I") do echo ".\BACKUP\%%J" "%windir%\%%I"
echo ".\BACKUP\%%I" "%windir%\%%I"
)
PAUSE
Floor 9 Posted 2007-02-13 03:50 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Originally posted by slore at 2007-2-13 03:28:
@echo off
FOR /F "EOL=;DELIMS=" %%I in (PurgeS.SVE) DO (
ECHO Compressing and backing up files under %windir%\%%I\, please wait...
PING 127.0.0.1 -n %TIMEOUT% >NUL
for /F "tok ...


Thanks brother again for the answer, but because the number of path levels is not fixed, so using your this method still doesn't work. Your this DELIMES=2 only has 2 levels. What if there are 3 levels, 4 levels?

Let me explain again the reason why I get the last name of the directory with \ is that it is convenient for me to use the RAR command to restore again. For example, if I have backed up all items, then I can let the user choose the content to restore, such as Japanese input method, Korean input method, MS Pinyin input method, etc. If it is directly compressed into one or a certain package as a whole, it is very inconvenient to use the RAR command for restoration

[ Last edited by HUNRYBECKY on 2007-2-13 at 03:53 AM ]
Floor 10 Posted 2007-02-13 04:03 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Still waiting online..................Looking forward to the answers from the experts.
Floor 11 Posted 2007-02-13 04:20 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline

─────────────────── Moderation Record ────────────────────
Performer: namejm
Description: {tid=27658}has contextual relationship with this topic, merged into this topic.
Punishment: Since the user is not a new user in the forum, they should be aware of the forum's posting regulations. Therefore, 4 points of points earned from the user's posts are deducted.
─────────────────── Moderation Record ────────────────────


C:\WINDOWS\system32
C:\WINDOWS\SHELLNEW
C:\WINDOWS\system32\dhcp
C:\WINDOWS\system32\dllcache
C:\WINDOWS\Help\Tours\mmTour
C:\WINDOWS\Help\Tours\WindowsMediaPlayer
C:\WINDOWS\ime\CHSIME
C:\WINDOWS\ime\SHARED


Among these paths above, I only want to get the content after the last \ in the path, such as the results obtained as follows
system32
SHELLNEW
dhcp
dllcache
mmTour
WindowsMediaPlayer
CHSIME
SHARED

Please ask experts for some suggestions. It is required not to use third - party tools and not to use VBS scripts

[ Last edited by namejm on 2007-2-12 at 04:46 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
namejm -4 2007-02-13 05:46
Floor 12 Posted 2007-02-13 04:42 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
list.txt
C:\WINDOWS\system32
C:\WINDOWS\SHELLNEW
C:\WINDOWS\system32\dhcp
C:\WINDOWS\system32\dllcache
C:\WINDOWS\Help\Tours\mmTour
C:\WINDOWS\Help\Tours\WindowsMediaPlayer
C:\WINDOWS\ime\CHSIME
C:\WINDOWS\ime\SHARED


@echo off 
for /f "delims=" %%i in (list.txt) do echo %%~ni
pause
Floor 13 Posted 2007-02-13 05:08 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
I'm very grateful to Brother zh159 for answering my question many times. I should learn from you a lot.

Just now, Brother redtek also answered my question. Because I'm not familiar with the FOR statement at all, I still didn't understand Brother redtek's meaning. I actually treated it as a SET variable and made it %%I:%~nI, so no matter how I did it, it didn't come out.

Thank you again for your answer.
Floor 14 Posted 2007-02-13 05:16 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Also thank you~
Still a lot to go with the for in bat..
Floor 15 Posted 2007-02-13 06:03 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Hey, I actually forgot to use %~ni
Forum Jump: