简体   繁体   English

递归搜索字符串并替换字符串

[英]search for string recursively and replace the string

I need to find a specific string in the Windows PATH recursively and replace the string with another string:我需要递归地在 Windows PATH 中找到一个特定的字符串,并将该字符串替换为另一个字符串:

Task # 1任务1

I identified(with help) on how to identify the path of available string using the below script.我确定(在帮助下)如何使用以下脚本识别可用字符串的路径。

findstr /s /i /m /c:"topaz3258" H:\MKS\build\host\dev\*.* > H:\topaz3258\result.txt

Task # 2任务#2

Now I need your assistance to update "topaz3258" with "topaz103258" in all the above path identified for the string.现在,我需要您的帮助,以在为字符串标识的所有上述路径中将“topaz3258”更新为“topaz103258”。 The file type is " .ini" and " .cmd" files.文件类型为“ .ini”和“ .cmd”文件。

Note: Powershell is restricted in the organization, so I have to use Batch script.注意:Powershell在组织中被限制,所以我必须使用批处理脚本。

With powershell scripts you can do a regex replacement within a file pretty easy.使用 powershell 脚本,您可以非常轻松地在文件中进行正则表达式替换。 I just pulled an example from something I had already, this should probably work in general.我只是从我已有的东西中举了一个例子,这应该可以正常工作。 but this is just to do one file但这只是做一个文件

(gc "input filename" -raw) -replace 'topaz3258', 'topaz103258' | (gc "输入文件名" -raw) -replace 'topaz3258', 'topaz103258' | Out-File -encoding ASCII "output filename" Out-File - 编码 ASCII “输出文件名”

You can iterate over files pretty easily though https://theitbros.com/powershell-script-for-loop-through-files-and-folders/您可以通过https://theitbros.com/powershell-script-for-loop-through-files-and-folders/轻松遍历文件

edit, im just writing this freehand but it might look something like编辑,我只是徒手写的,但它可能看起来像

$files = Get-ChildItem H:\MKS\build\host\dev\ -Recurse *.*

foreach ($f in $files){

(gc $f.FullName -raw) -replace 'topaz3258', 'topaz103258' | Out-File -encoding ASCII $f.FullName

}

if you dont want to do it in-place you can specify a different output path or else just copy the folder tree before operating on it.如果您不想就地执行此操作,您可以指定不同的 output 路径,或者在操作之前复制文件夹树。 Depends on what you need exactly.取决于你到底需要什么。 star dot star is a very wide.net to cast btw, you might want to filter by text files only or something star dot star 是一个非常宽的.net 投射 btw,您可能只想按文本文件或其他内容进行过滤


If you dont have powershell如果你没有 powershell

I quickly put this together from other answers (Also edited).我很快将其他答案(也经过编辑)放在一起。 It will no longer make a new file, it writes directly into the original file.它不会再创建新文件,而是直接写入原始文件。 This is dangerous, you cant go back.这很危险,你不能 go 回来。 There is no undo so make a temporary backup somehow.无法撤消,因此请以某种方式进行临时备份。 The script will preserve blank lines.该脚本将保留空行。 It will not respect word boundaries so for example它不会尊重单词边界,例如

red blue reddit (red -> green) green blue greendit red blue reddit (red -> green) 绿蓝 greendit

@echo off
SET FIND_STR=topaz3258
SET REPL_STR=topaz103258
SET ROOT_DIR=H:\MKS\build\host\dev\


for /R "%ROOT_DIR%" %%a in (*.txt) do (
    :: say the filename
    echo %%a

    for /f "delims=" %%i in ('type "%%a" ^| find /v /n "" ^& break ^> "%%a"') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        set "line=!line:*]=!"
        if defined line set "line=!line:%FIND_STR%=%REPL_STR%!"
        >>"%%a" echo(!line!
        endlocal
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM