SET a:b=c won't work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: SET a:b=c won't work

#16 Post by ghostmachine4 » 13 Jul 2010 20:39

Code: Select all

C:\test>more words
[###]
crazy

C:\test>more template
[###] little dog walked on a street and saw [###] little cats
the [###] little dog started
chasing the [###] little cats

C:\test>gawk "FNR==NR&&!/\[/{s=$1;next}{gsub(/\[###\]/,s)}1"  words template

crazy little dog walked on a street and saw crazy little cats
the crazy little dog started
chasing the crazy little cats


aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: SET a:b=c won't work

#17 Post by aGerman » 14 Jul 2010 06:40

Using native batch you have to escape all special characters, like ^<>|&.
I hope this will work for you:

Code: Select all

@echo off &setlocal
set /p "replace="<"words.txt"
for /f "usebackq skip=1 delims=" %%a in ("words.txt") do (
  >"%%a.txt" type nul
  set "word=%%a"
  call :proc
)

pause
goto :eof

:proc
for /f "delims=: tokens=1*" %%a in ('findstr /n "^" "template.txt"') do (
  set "line=%%b"
  call :repl
)
goto :eof

:repl
if not defined line (
  >>"%word%.txt" echo.
  goto :eof
)
set "line=%line:^=^^%"
set "line=%line:<=^<%"
set "line=%line:>=^>%"
set "line=%line:|=^|%"
set "line=%line:&=^&%"

>>"%word%.txt" call echo.%%line:%replace%=%word%%%
goto :eof


[EDIT: Double code removed /]


Regards
aGerman

Post Reply