1

I need to find a specific string in the Windows PATH recursively and replace the string with another string:

Task # 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

Now I need your assistance to update "topaz3258" with "topaz103258" in all the above path identified for the string. The file type is ".ini" and ".cmd" files.

Note: Powershell is restricted in the organization, so I have to use Batch script.

1 Answer 1

1

With powershell scripts you can do a regex replacement within a file pretty easy. 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' | Out-File -encoding ASCII "output filename"

You can iterate over files pretty easily though 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. 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


If you dont have 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. 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

@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
    )
)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, but the *.ini file is not getting updated and the result is only taking the final line from the ini file and placing it under the result
ok true, the >> was needed instead of > but I also completely changed the script to also preserve blank lines and directly write the results into the original file. There are no temporary files now so please do make a complete backup of the files you are working on, at least until you know the script is working for you, it has the power to completely erase files but I did test it on my system
No problem, sorry it took so long, I thought Id get it with powershell real quick but I think in the end I learned something too. If you think my answer is helpful please accept it, Im trying to grind out my initial reputation so I can vote on other peoples answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.