字符串替换(星号)的批处理文件? [英] String substitution (asterisks) in Batch files?

查看:628
本文介绍了字符串替换(星号)的批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何更换的星号的与别的东西,在一个批处理文件中的字符串?

How do I replace an asterisk in a string with something else, in a batch file?

%myvar的正常扩张:=为%似乎并不如工作从 *

即,如果我有:

Set MyVar=From

那么这些不工作:

then these don't work:

Echo %MyVar:*=To%
Echo %MyVar:^*=To%

什么是做正确的方法是什么?

What's the correct way to do this?

推荐答案

像一个Sorpigal,但这种手柄文字与星和聚星更换开端,它并不失败,报价一个解决方案。

A solution like the one of Sorpigal, but this handles also text beginning with stars and multiple stars to replace, and it doesn't fail with quotes.

@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

这篇关于字符串替换(星号)的批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆