0

I'm working on a content editor, which will edit the data inside a txt file, changing it to whatever you programmed it to.

This is my original script:

@echo off 
setlocal enableextensions disabledelayedexpansion

set "search=Stars"
set "replace=Falling Stars"

set "textFile=0Originals\Document.txt"

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

This is my attempt:

@echo off 
setlocal enableextensions disabledelayedexpansion

set "search=Stars"
set "replace=Falling Stars"

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

I am trying to to make it read any file, but everything I do fails to work.

Any idea on why the *.txt fails to work?

Compo
  • 36,585
  • 5
  • 27
  • 39
JR Santos
  • 137
  • 1
  • 11
  • 1
    Why not use an outer `for` loop, i.e. ```For %%G In ("0Originals\*.txt") Do For /F "Delims=" %%i In ('Type "%%~G" ^& Break 1^>"%%~G"') Do …``` – Compo Sep 30 '22 at 22:55
  • 1
    I'd prefer however to change the outer loop to only perform the task on files which include the search string, so would probably change the outer loop to a `for /f` with `findstr.exe` and its `/M` option. – Compo Sep 30 '22 at 23:01
  • @Compo - Like this - `Do findstr.exe /m "Delims="` I'm sure I'm placing this wrong because when I added this it failed... does the `/m` belong somewhere else – JR Santos Sep 30 '22 at 23:58
  • like this `Do For /F "Delims="` worked perfectly and you what on the way home, I thought about separating this and doing something like this `For %%G In ("0Originals\*.txt")` - all my testing was build everything together - and now I have a fix for another script – JR Santos Sep 30 '22 at 23:59
  • what is the best way to isolate the search for example I have `Stars` and `HotStars` and I only want to find Stars – JR Santos Oct 01 '22 at 00:03
  • 1
    No, like this: ```For /F Delims^=^ EOL^= %%G In ('%SystemRoot%\System32\findstr.exe /MLIC:"%search%" "0Originals\*.txt" 2^>NUL') Do For /F "Delims=" %%i In ('Type "%%~G" ^& Break 1^>"%%~G"') Do …``` If you wanted just the word `Stars` then perhaps ```%SystemRoot%\System32\findstr.exe /RMIC:"\<%search%\>" "0Originals\*.txt"```. Honestly, those things are explained in the help information `findstr /?`, and that should be your first port of call, not here. – Compo Oct 01 '22 at 00:08
  • Wow...OK yes I am no good with `.exe` methods – JR Santos Oct 01 '22 at 00:10
  • 1
    `Any idea on why the *.txt fails to work?` Yes - you can't redirect to a set of files (`*.txt`) but only to a unique file name. That's why previous comments suggested two `for` loops (one to separate `*.txt` to single files, the other to process those files) – Stephan Oct 01 '22 at 09:19

0 Answers0