2

I have stitched, w/o any deeper cmd knowledge, a script that searches for certain string in specified files in a directory and replaces it with another. (at least should)

Goes like

  set "search=readed"
  set "replace=read"
  set "myfiles=.\*.tex"

for %%f in (%myfiles%) do (
  set /p val=<%%f
  echo "fullname: %%f"


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


  )
)
pause

Almost works. It has two issues. 1st - the less important: deletes whitespace lines. 2nd - deletes exclamations. before:

% !TEX root = TechRep.tex

after:

% TEX root = TechRep.tex

May I ask for a help? Thanks!

lit
  • 14,456
  • 10
  • 65
  • 119
Jewenile
  • 195
  • 1
  • 15
  • 3
    Remove the closing parenthesis at the end of your `echo` command. Despite its looks, it does *not* match the opening parenthesis on the same line! Instead, it matches the opening parenthesis after the `do` keyword. – DodgyCodeException Dec 05 '17 at 11:52
  • That seems to work fine. Thank you! – Jewenile Dec 05 '17 at 11:57
  • 1
    If you are editing a file with pure batch for the thrill of it (for the challenge), then knock yourself out. But if you simply want to accomplish a task, then I strongly recommend something else. Pure batch is terrible for text file editing because it is slow, and robust solutions require arcane, non-intuitive syntax and techniques. I recommend [JREPL.BAT - a hybrid regular expression text processing utility that uses JScript and batch](https://www.dostips.com/forum/viewtopic.php?f=3&t=6044): `for %%F in (*.tex) do call jrepl "readed" "read" /f "%%F" /o -` – dbenham Dec 05 '17 at 20:44
  • Hi! It doesnt bring me any special joy. I just need the stuff to work regardless the machine and its config. Using what I almost know. – Jewenile Dec 05 '17 at 21:37

1 Answers1

2

There are two problems:

  1. Skipped blank lines. This is caused by a restriction in the for /f command. You can use the old findstr trick to get around that.

  2. Removed exclamation marks. This is caused by an errant closing parenthesis ) in your echo command. The opening parenthesis in echo( on the same line is ignored, and is not matched with the closing parenthesis. Instead, the closing parenthesis is matched with the start of the do loop. The next line, containing the endlocal command, is outside the loop. This seems to cause problems as there is a setlocal inside a loop and an endlocal outside; by some strange mechanism, this somehow causes the exclamation marks to be removed. Remove the ) from the echo line and this problem disappears.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
  • 2
    No, you can't change `echo(` to `echo ` because the script will then write `ECHO is off.` if it encounters a line that consists of nothing but spaces. – dbenham Dec 05 '17 at 20:34
  • 2
    Exclamation points from line 2 and onward are removed because delayed expansion is enabled, and `%%I` will expand expressions with exclamation points (delayed expansion occurs after FOR variable expansion). – dbenham Dec 05 '17 at 20:36
  • Ill probably survive the blanks removal. The bracket ending cleared. ;) – Jewenile Dec 05 '17 at 21:41
  • @dbenham good point. I've removed the incorrect statement from my answer. – DodgyCodeException Dec 06 '17 at 08:16