[英]cmd string replacement of special character
I am trying to replace ~
with %HOME%
in a batch script. 我试图在批处理脚本中用
%HOME%
替换~
。 This is what I have so far: 这是我到目前为止:
@echo off
setlocal enabledelayedexpansion
set str=%*
set replacement=%HOME%
set str=%str:~=!replacement!%
echo %str%
This does not work as I expect, presumably because I need to escape the tilde ~
in some way. 这不会像我期望的那样起作用,大概是因为我需要以某种方式逃避波浪号
~
。
When I escape with ^
, str
is unchanged. 当我用
^
转义时, str
不变。 Without escape, str
is the string str:~=<my_home_path>
. 没有转义,
str
是字符串str:~=<my_home_path>
。
How can I change "~/work/my_folder"
into "C:/Users/login/work/my_folder"
? 如何将
"~/work/my_folder"
更改为"C:/Users/login/work/my_folder"
?
If the ~
is always the first character, you can use somewhat like this: 如果
~
总是第一个字符,你可以使用如下:
@echo off &setlocal enabledelayedexpansion
set "str=|~/work/my_folder"
set "replacement=C:/Users/login"
set str=%str:|~=!replacement!%
echo %str%
Or a bit more complex: 或者更复杂一点:
@echo off &setlocal enabledelayedexpansion
set "str=~/work/my_folder -param one ~ two:~"
set "replacement=C:/Users/login"
call :strlen str len
set /a len-=1
for /l %%i in (0,1,%len%) do if "!str:~%%i,1!"=="~" (set "new=!new!%replacement%") else set "new=!new!!str:~%%i,1!"
echo %new%
goto :eof
:strlen
:: list string length up to 8189 (and reports 8189 for any string longer than 8189)
:: function from http://ss64.org/viewtopic.php?pid=6478#p6478
( setlocal enabledelayedexpansion & set /a "}=0"
if "%~1" neq "" if defined %~1 (
for %%# in (4096 2048 1024 512 256 128 64 32 16) do (
if "!%~1:~%%#,1!" neq "" set "%~1=!%~1:~%%#!" & set /a "}+=%%#"
)
set "%~1=!%~1!0FEDCBA9876543211" & set /a "}+=0x!%~1:~32,1!!%~1:~16,1!"
)
)
endlocal & set /a "%~2=%}%" & exit /b
endlocal
Output: 输出:
C:/Users/login/work/my_folder -param one C:/Users/login two:C:/Users/login
This doesn't work with exclamation marks. 这不适用于感叹号。
I think you're pretty close already except for a couple of minor problems. 除了一些小问题之外,我认为你已经非常接近了。
Firstly, there's probably no environment variable in Windows for %home%
-- at least not on my system, anyway. 首先,对于
%home%
,Windows中可能没有环境变量 - 至少在我的系统上没有。 I think the variable you're looking for is %userprofile%
. 我认为您要查找的变量是
%userprofile%
。 ss64.com has an excellent list and description of Windows environment variables . ss64.com有一个很好的Windows环境变量列表和描述。
The other problem is that you're delaying expansion in the wrong order. 另一个问题是你以错误的顺序推迟扩张。 Try changing your penultimate line to
set str=!str:~=%userprofile%!
尝试更改倒数第二行以
set str=!str:~=%userprofile%!
so that the inner variable expands before the outer. 以便内部变量在外部之前扩展。 Translating forward slashes to backward might not be a bad idea, either.
将正斜杠向后翻译可能也不是一个坏主意。
@echo off
setlocal enabledelayedexpansion
set str=%*
set "str=!str: ~= %userprofile%!"
set "str=!str:*~=%userprofile%!"
set "str=%str:\=/%"
echo %str%
Example output: 示例输出:
C:\Users\me\Desktop>test ~/.bash_profile
C:/Users/me/.bash_profile
C:\Users\me\Desktop>test arg1 arg2 ~/.bash_profile
arg1 arg2 C:/Users/me/.bash_profile
@ECHO OFF
CLS
SET OldStr=~\work\my_folder
SET NewStr=
SET Replacement=C:\Users\login\work\my_folder
CALL :ReplaceHome "%OldStr%"
ECHO %NewStr%
EXIT /b %ERRORLEVEL%
:ReplaceHome
IF "%~1"=="" EXIT /B %ERRORLEVEL%
SET TempStr=%~1
IF "%TempStr:~0,1%"=="~" SET TempStr=%Replacement%%TempStr:~1%
SET NewStr=%NewStr%%TempStr:~0,1%
CALL :ReplaceHome "%TempStr:~1%"
EXIT /B %ERRORLEVEL%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.