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-26 16:55
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » A batch script that discards a specified level at the end of the path View 2,596 Replies 4
Original Poster Posted 2006-12-30 08:52 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Just for fun, post one:

@echo off
:: Purpose:
:: Discard the string of the specified level at the end of the path
:: Idea:
:: First read the path string in reverse order, then cut off the specified level from the beginning
:: Finally read the remaining string in reverse order

set num=0
set times=2
set "route=c:\a&bc\!dx\test dos\123.txt"
call :antitone "%route%"
call :intercept "%str%"
call :antitone "%route%"
echo "%str%"
pause
goto :eof

:antitone
set str=
set "var=%~1"
:loop1
if not "%var:~0,1%"=="" set "str=%str%%var:~-1%"
set "var=%var:~0,-1%"
if not "%var%"=="" goto loop1
goto :eof

:intercept
set "route=%~1"
:loop2
set "route=%route:*\=%"
set /a num+=1
if %num% lss %times% goto loop2
goto :eof

  Add a more efficient code:

@echo off
:: Idea:
:: Through for statement extension, intercept the directory path where the file is located
::   When the number of layers to be discarded exceeds the actual number of layers, keep the root directory level
set "route=c:\a&bc\!dx\test dos\123.txt\"
set layers_discard=3
if "%route:~-1%"=="\" set "route=%route:~0,-1%"
for /l %%i in (1,1,%layers_discard%) do call :intercept "%%route%%"
:end
echo "%route%"
pause
goto :eof

:intercept
for /f "delims=" %%i in (%1) do set "route=%%~dpi"
if "%route:~-2,1%"==":" (goto end) else set "route=%route:~0,-1%"
goto :eof


[ Last edited by namejm on 2007-1-19 at 03:04 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 2 Posted 2006-12-30 09:34 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
A very charming piece of code~~~Thumbs up + cherish and study~ :)))
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2006-12-30 10:56
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
The code is worthy of diligent study, give it a thumbs up~
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 4 Posted 2006-12-30 13:14 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Learning
Floor 5 Posted 2007-01-06 05:35 ·  中国 山西 阳泉 联通
初级用户
Credits 125
Posts 49
Joined 2006-12-24 08:33
19-year member
UID 74424
Gender Male
Status Offline
Great. Exactly what I needed. Thanks
Forum Jump: