How to change (replace) information from within a .txt file using .bat? [closed]

1

What I want to do is type a sed in shell.

I have a .txt file as several information inside, I want to replace one sequence of numbers with another.

As far as I did the script identifies the files through a variable SET /P COD01=Cod_01 : and copies to another folder.

I created another variable SET /P COD02=Cod_0: : I get the information should replace COD01.

Now I do not know how to do the challenge.

    
asked by anonymous 01.06.2017 / 06:52

1 answer

0

Some time ago I needed to change an acronym within a file on several different servers, so by searching I found the solution below.

Use the to find the sequence and replace, I used your variable example and changed the code, in addition it makes a backup.

@echo off    
@SET /P COD01=Cod_01
@SET /P COD02=Cod_02

@call:DoReplace %COD01% %COD02% arquivooriginal.txt arquivomodificado.txt
@exit /b

::Cria o arquivo do PowerShell para ser executado conforme parâmetros acima.
:DoReplace
@echo ^(Get-Content "%3"^) ^| ForEach-Object { $_ -replace %1, %2 } ^| Set-Content %4>Rep.ps1
::Executa o arquivo criado.
@Powershell.exe -executionpolicy ByPass -File Rep.ps1
::Deleta o arquivo.
@if exist Rep.ps1 del Rep.ps1
:: Renomeia.
@RENAME arquivooriginal.txt arquivooriginal_old.txt
@RENAME arquivomodificado.txt arquivooriginal.txt
    
01.06.2017 / 16:02