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
  • 31
  • 2
  • 1
    If 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 Apr 25 '18 at 11:33
  • I didn't get you.can you please give me within the batch file solution? – Meera Apr 25 '18 at 12:09
  • I recently started using [GrepWin](https://github.com/stefankueng/grepWin) to find and replace across multiple text files and locations. I've been impressed with it. – Andi Mohr Apr 25 '18 at 12:34
  • @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 Apr 25 '18 at 21:04

0 Answers0