0

I have tried to make a batch which searches in a folder for .txt files and if the text file contains special words, it should write a word before and after this word.

Example:

*.TXT contains:

alpha
beta
rose
delta

The batch should search for rose and overwrite the text file with:

alpha
beta
before
rose
after
delta

My batch looks at the moment as follows:

@echo off
for %%g in (*.txt) do (
    >"temp.txt" (
        for /f "usebackq delims=" %%h in ("%%~g") do (
            echo before %%h after
        )
    )
    move /y "temp.txt" "%%~g"
)

But I think somewhere is missing an if function that the text should only be written before/after the word rose.

Can somebody give me a hint how to proceed?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    Why didn't you place an `if` condition? it shouldn't be that hard to find the right place for it, right? I'd use `if "%%h"=="rose"`… – aschipfl Sep 14 '21 at 11:10
  • Yes, I have tried that also like: for /f "usebackq delims=" %%h in ("%%~g") do ( if "%%h"=="rose" echo before %%h after ---- but the Problem is, that this will delete all but "before rose after"... – Batch1234 Sep 14 '21 at 11:13
  • 2
    Well, then you will have to include an `else` clause, right? type `if /?` into a command prompt window to learn how to use it… – aschipfl Sep 14 '21 at 12:40
  • You did not specify if `rose` for example will always be alone on his line or not. – XouDo Sep 20 '21 at 08:34

1 Answers1

0

I found my old code with a similar problem and played around a bit .. Not optimized but it works :)

@echo off
set FolderTXT="d:\files\*.txt"
set TempFile="d:\files\temp.txt"
set bfr=before
set aftr=after

for %%g in (%FolderTXT%) do ( 
 echo. 2>%TempFile%
 for /F "tokens=*" %%A in (%%g) do (
  IF "%%A"=="rose" (
  ECHO %bfr% >>%TempFile%
  ECHO %%A >>%TempFile%
  ECHO %aftr%  >>%TempFile%
 ) ELSE ( echo %%A >>%TempFile% )
)
 move /y %TempFile% "%%~g" 
)

Or you can use powershell code. Just ceate a test1.ps1 file with code:

Get-ChildItem "d:\files\*.txt" -File  |
 ForEach {   (Get-Content $_.FullName) |
  ForEach  { $_ -replace "rose","before rose after"  } |
   Set-Content $_.FullName     }

and call from cmd

powershell -noprofile -nologo -noninteractive -executionpolicy Bypass -file .\test1.ps1

if 'before' and 'after' must be on the new lines

just use this and set replaced string:

ForEach  { $_ -replace "rose","before`r`nrose`r`nafter"  }
TV193
  • 46
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 15 '21 at 01:09
  • Hm, looks pretty good in first instance, but it does not work. Text is deleted completely by this batch :( – Batch1234 Sep 16 '21 at 08:35
  • I make the test file test1.txt with 6 lines : alpha beta rose delta And it was changed correct ... Try to remove "@echo off" from the batch, its look like a problem with reading %%A from file – TV193 Sep 16 '21 at 09:32
  • or you can try powershell, its be only 1 row of the code :) [link](https://stackoverflow.com/questions/17144355/how-can-i-replace-every-occurrence-of-a-string-in-a-file-with-powershell) – TV193 Sep 16 '21 at 10:04
  • I dont have experience with ps but i have installed it on my computer. Maybe it is worth a try. Can you send me the code? If it works, I will learn the programming afterwards :) – Batch1234 Sep 17 '21 at 08:12