Batch file 批处理文件中的字符串替换(星号)?

Batch file 批处理文件中的字符串替换(星号)?,batch-file,Batch File,如何在批处理文件中用其他内容替换字符串中的星号 如果From是*,则%MyVar:From=到%的正常扩展似乎不起作用 i、 例如,如果我有: Set MyVar=From 那么这些就不起作用了: Echo %MyVar:*=To% Echo %MyVar:^*=To% 做这件事的正确方法是什么?总有一种方法(不管多么令人不快) 这是一个类似Sorpigal的解决方案,但它也处理以星星开头的文本和要替换的多个星星,并且不会因引号而失败 @echo off setlocal EnableDel

如何在批处理文件中用其他内容替换字符串中的星号

如果
From
*
,则
%MyVar:From=到%
的正常扩展似乎不起作用

i、 例如,如果我有:

Set MyVar=From
那么这些就不起作用了:

Echo %MyVar:*=To%
Echo %MyVar:^*=To%
做这件事的正确方法是什么?

总有一种方法(不管多么令人不快)


这是一个类似Sorpigal的解决方案,但它也处理以星星开头的文本和要替换的多个星星,并且不会因引号而失败

@echo off
setlocal EnableDelayedExpansion
rem replace all "*" with "."
set "replace=."
set "var=*One**two***three"

rem Add dummy char to accept also a star in front
set "var=#!var!"
:replaceLoop
for /F "tokens=1 delims=*" %%A in ("!var!") do (
  set "prefix=%%A"
  set "rest=!var:*%%A=!"
  if defined rest (
    set "rest=!REPLACE!!rest:~1!"
    set Again=1
  ) else set "Again="
  set "var=%%A!rest!"
)
if defined again goto :replaceLoop
set "var=!var:~1!"
echo !var!
exit /b

PS+批量

@echo off

set string=*hello***lop*
set oldchar=*
set newchar=$

for /f "tokens=*" %%* in ('
   "powershell -c ""$env:string"".replace('%oldchar%','%newchar%')"
 ') do set result=%%*

echo %result%
pause
goto :eof
@echo关闭
setlocal EnableDelayedExpansion
set old_str=**你好*世界**
设置oldchar=*
设置新字符=$
设置新的=

>$1($2(对于/l%%a,在(1%cnt%)中)在Windows XP上测试的变体:

@echo off  1>nul 2>nul 3>nul 4>nul

set string=**hello*World**
set old_char=*
set new_char=#

pushd %Temp%.\

set string>f1.t

copy nul+nul f0.t 

for /f %%a in (f0.t) do (
  (
    echo 1r%old_char%%%a%new_char%
    echo w
    echo e
  ) | edlin /b f1.t
)
for /f "tokens=*" %%i in (f1.t) do set %%i

echo %string% >con

del f0.t f1.t f1.bak

popd
pause

@Mehrdad:edlin在xp中可用:

hh.exe ntcmds.chm::/edlin.htm
hh.exe ntcmds.chm::/edlin_subcmds.htm
另一种变体:

@echo off
setlocal EnableDelayedExpansion

set string=**hello * World**
set old_char=*
set new_char=#
set new_str=

for /l %%a in (0 1 0xFF) do (
  if !string:~%%a^,1!. neq %old_char%. (
     set "new_str=!new_str!!string:~%%a,1!"
  ) else if !string:~%%a^,1!. neq . (
     Set new_str=!new_str!%new_char%))

echo !new_str!
pause
另一个:

@echo off

set str=**hello ** world**
set old=\*
set new=#

for /f "useback delims=" %%_ in (`@mshta "about:<script>var string='%str%';new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(string.replace(/%Old%/g,'%New%'));close();</script>" ^|more`) do set res=%%_

echo %str% 
echo %res%

pause
@echo关闭
set str=**你好**世界**
变老=\*
焕然一新=#
对于/f“useback delims=“%\”in(`@mshta”about:var string='%str%';new ActiveXObject('Scripting.FileSystemObject'))。GetStandardStream(1)。Write(string.replace(/%Old%/g,'%new%');close();“^ more`)do set res=%_
回显%str%
回声%res%
暂停

谢谢你的回答!不幸的是,如果字符串中有引号,这似乎不起作用……或者我做错了什么。有什么想法吗?你从来没有停止过,也不会不感到惊讶。非常好!谢谢,但几个月前我在一个更具实验性的论坛上问了同样的问题。好吧,我能说什么呢……我很高兴你有一个好的源代码这有助于你让像我这样的人感到惊讶!:)我过去非常喜欢批处理脚本,但我缺乏持久性。另外,那是DOS时代,批处理脚本的功能对我来说非常差,所以我很快就冷静下来。简而言之,我仍然喜欢并使用批处理脚本,因为相对简单的脚本可以节省大量时间。但是你在答案和c中展示的内容omments是一个完全不同的专业水平,我相信,仅仅参加相关论坛是不够的。@jeb:这太不可思议了(而且很难理解:P)。非常感谢您的解决方案!!:)+1,有趣的方法,但稍作解释就好了。顺便说一句,它会在
old_str
的开头去掉空格。在我的文本中总是
10µA
100µs
,因此
fc
也会失败:-)
@echo off
setlocal EnableDelayedExpansion

set string=**hello * World**
set old_char=*
set new_char=#
set new_str=

for /l %%a in (0 1 0xFF) do (
  if !string:~%%a^,1!. neq %old_char%. (
     set "new_str=!new_str!!string:~%%a,1!"
  ) else if !string:~%%a^,1!. neq . (
     Set new_str=!new_str!%new_char%))

echo !new_str!
pause
@echo off

set str=**hello ** world**
set old=\*
set new=#

for /f "useback delims=" %%_ in (`@mshta "about:<script>var string='%str%';new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(string.replace(/%Old%/g,'%New%'));close();</script>" ^|more`) do set res=%%_

echo %str% 
echo %res%

pause