find string from multiple files and replace it without creating new file

3

I want to find string e.g. "date" from my files of a folder which contains multiple ".c" and ".h" files in it and replace it with "date 24-April-2018" using batch file. what should do? Please see below code. I want to do the same but not for one file Input.txt , I want to do for multiple files with extensions of a folder

@echo off 
setlocal enableextensions disabledelayedexpansion

set "search=@date"
set "replace=@date 24-apr-2018"

set "textFile=Input.txt"

for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    >>"%textFile%" echo(!line:%search%=%replace%!
    endlocal
)

Meera

Posted 2018-04-25T11:10:02.000

Reputation: 31

1If you define textFile by set "textFile=%1", then you can call this batch file (eg named SetDate.cmd) to update the file in the passed parameter. Then you can use for %i in (DirPath\FileMask) do call SetDate.cmd %i, doubling the % if it is in a batch file itself. – AFH – 2018-04-25T11:33:17.987

I didn't get you.can you please give me within the batch file solution? – Meera – 2018-04-25T12:09:06.653

I recently started using GrepWin to find and replace across multiple text files and locations. I've been impressed with it.

– Andi Mohr – 2018-04-25T12:34:52.177

@Meera - I don't know what else I can say to explain, without going into the sort of detail that would require an answer. If your batch file is SetDate.cmd, edit it so that the third set command becomes set "textFile=%1", then call it with for %f in (DirPath\FileMask) do call SetDate.cmd %f from the command line or for %%f in (DirPath\FileMask) do call SetDate.cmd %%f within another batch file. I hope it's clear what DirPath and FileMask signify. – AFH – 2018-04-25T21:04:43.493

No answers