3

I have a batch script that can replace text, and I want it to replace strings that are followed by a newline of text below the original. An example would be as follows:

Before:

Old string

After:

New
String

I have tried a number of newline commands used in batch (some of which are found on Stack Exchange sites) and none of them work. I believe it is because of the syntax of the script. Here is the script I am working with:

@echo off
setlocal enableextensions disabledelayedexpansion

set search=Old string
set replace=New string
set textFile=Test.txt

:PowerShell
SET PSScript=%temp%\~tmpStrRplc.ps1
ECHO (Get-Content "%~dp0%textFile%").replace("%search%", "%replace%") ^| Set-Content "%~dp0%textFile%">"%PSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
EXIT

How can I implement a newline "injection" in my script?

  • Sounds good, I'll work on the script some more and post a new question when need be. – Mr. Mendelli Feb 27 '18 at 3:33
  • No worries, I'd just need more detail to troubleshoot that and it seems like something better suited for a new question going outside the scope of the original question a bit. It didn't sound like anything obvious to me as you explained in the simple comment space so having more detail of the error, examples of the path, the final logic you're using script wise that's having the issue, etc. would be very helpful and with so much more detail being needed, might as well make it a new question. – Pimp Juice IT Feb 27 '18 at 3:51
3

You can put enter image description here in place of the space between the replacement string where you want the newline (CRLF) to be placed to get the expected result—backtick "r" backtick "n" (see below).

This only requires that one small change to the existing script and it'll work as expected per your description and example output results.


Script

@echo off
setlocal enableextensions disabledelayedexpansion

set search=Old string
set replace=New`r`nstring
set textFile=Test.txt

:PowerShell
SET PSScript=%temp%\~tmpStrRplc.ps1
ECHO (Get-Content "%~dp0%textFile%").replace("%search%", "%replace%") ^| Set-Content "%~dp0%textFile%">"%PSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
PAUSE
EXIT

Note: Be sure to not put any spaces between the two strings in the replace= variable to ensure there is no trailing or leading spaces in the new string with the newline as you need.

enter image description here


Results

Before

Old string

After

New
String

Further Resources

  • Thank you for your answer. This does work, but it would appear to only do so on simpler text documents. There is a document I am working with I wanted to try this on that is more complex, and your method returns this error: You cannot call a method on a null-valued expression. I replaced the string successfully when it was the only present content in the document. Any ideas as to why this is? – Mr. Mendelli Feb 27 '18 at 2:47
  • @Mr.Mendelli Is there any way you could try to get this error with a smaller sample file content wise and provide me something to test with? If I could test with what you are working with or something smaller perhaps that also has this issue, I can probably figure it out for you. Let me know. – Pimp Juice IT Feb 27 '18 at 2:49
  • @Mr.Mendelli - I figured out the backtick issue with the comments so try set search=;-------- MSI Parameters ---------- and then use set replace=;-------- MSI Parameters ----------`r`n.ShowSB=0 to get the expected result per your updated clarification. Script example: i.imgur.com/yEXNsI8.png and Output example: i.imgur.com/q451qtb.png – Pimp Juice IT Feb 27 '18 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.