2

I am trying to replace a string in a file through a batch file in post-build action of visual studio. The first string is lMkUk= and the second string (replacement string) is ##DummyValues##.

While replacing the first string with the second string using a batch file in Visual Studio post-build action, the first string was not replaced properly, but as =##DummyValues##=; = was added at the starting and end of the second string.

But if I remove the = (and make it lMkUk) at the end of the first string using the same batch file, the string will be replaced successfully. It seems that the = is the problem in the second string.

Here is my code:

@echo on&setlocal
setlocal enableextensions disabledelayedexpansion
::License key replacement file
set sourceFile=%1

::Replacement string
set FirstVariable=lMkUk=
set SecondVariable=##DummyValues##

::Replacement statement
if NOT "%FirstVariable%" == "" (
        for /f "delims=" %%i in ('type "%sourceFile%" ^& break ^> "%sourceFile%" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        >>"%sourceFile%" echo(!line:%FirstVariable%=%SecondVariable%!
        endlocal
    )
)

How to resolve this in Visual Studio post-build action?

Gokul P
  • 31
  • 4
  • 2
    You can use `PowerShell` from a `Batch File`, `@PowerShell -NoP "(GC '%~1') -Replace 'lMkUk=','##DummyLicense##'|SC '%~1'"` – Compo Feb 26 '19 at 14:00
  • 4
    The problem is that variable `FirstVariable` contains an `=`-sign, so the replacement code becomes `!line:lMkUk==##DummyLicense##!`, and the *first* `=`-sign separates search from replacement string. In other words: the search string cannot contain `=`-signs. Although it is possible with pure batch scripting (for example, [Batch file : how to search and replace a string that have an “=” inside](https://stackoverflow.com/a/37823807)), let me recommend to switch to another language, like PowerShell, for instance... – aschipfl Feb 26 '19 at 14:28
  • Possible duplicate of [Batch file : how to search and replace a string that have an "=" inside](https://stackoverflow.com/questions/37724410/batch-file-how-to-search-and-replace-a-string-that-have-an-inside) – double-beep Feb 26 '19 at 18:32
  • @Compo, Can we use the PowerShell in Visual Studio post-build action? – Karthi Feb 27 '19 at 06:37
  • @Karthi, is there a reason why you haven't tried it directly as a command, as part of a batch file _(as I showed you)_, or couldn't perform a simple search of either [this site](https://stackoverflow.com/a/6501719) for running powershell scripts? – Compo Feb 27 '19 at 09:12

0 Answers0