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

Asked

Viewed 6,078 times

1

What I want to do is kind of a sed in shell.

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

How far I’ve done 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:: that I receive the information must replace COD01.

Now I don’t know how.

  • Welcome to Stack Overflow, Maressa! Please edit your question and explain better what you are trying to do - how many files do you intend to modify? Is it just a line of text within each or more of them? Also include the code you’ve already made. And enjoy it and tour to get to know the site better! :)

1 answer

0

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

Utilizes the to find the sequence and replace, I used your variable example and changed the code, in addition make 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.