0

我想要一个可以删除最后一个反斜杠/斜杠的函数,现在我多次编写此代码:

IF %variable:~-1%==\ SET variable=%variable:~0,-1%

所以我会像这样

set var1=C:\Folder\
call :removeTrailingBackSlash var1

echo %var1%- 我应该在这里看到“C:\Folder”

4

1 回答 1

1
@ECHO OFF
SETLOCAL
SET var1=c:\somewhere\\\\\\\\\\\\
CALL :removeTrailingBackSlash var1
ECHO var1=%var1%
GOTO :eof
:removeTrailingBackSlash
IF NOT DEFINED %1 GOTO :eof
SETlocal
CALL SET $_=%%%1%%
IF %$_:~-1%==\ endlocal&CALL SET "%1=%%%1:~0,-1%%"&GOTO removeTrailingBackSlash
GOTO :eof

此过程应删除尾随反斜杠。它是如何工作的,我将作为练习留给读者:)

于 2013-05-03T09:17:25.937 回答