1

I'm looking to create a bat file that when ran, will prompt the user for a simple value. That value is then used to edit a source file where it will replace a generic value and then save the file as a new file. I've got the file creation part done, but I'm having an issue with the prompt as the new file just has a blank value.

This is what I have so far:

@echo off
set "replace=CHANGE-ME"
set /p new_variable="replaced"

set "source=download.volt"
set "target=download.shtml"

setlocal enableDelayedExpansion
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%new_variable%!"
      echo(!line!
   )
) > %target%
endlocal

I think my syntax is close but I'm not sure where I'm off.

Thanks!

Shawn Farmer
  • 51
  • 1
  • 3
  • do you use any special characters in `new_variable`? – npocmaka Jan 23 '15 at 23:00
  • Works quite happily for me - please state what you used for the replacement value. – Magoo Jan 24 '15 at 02:44
  • I'm using a value of newstring. No special characters are needed. – Shawn Farmer Jan 24 '15 at 17:53
  • I see nothing wrong with this code. Is maybe the system or read-only attribute set on download.shtml? Try `>NUL 2>&1 del /f "%target%"` before your `enabledelayedexpansion`. Does `%target%` contain spaces? Try quotes. If that doesn't help and you run out of things to try, you could try dbenham's excellent [jrepl.bat](http://www.dostips.com/forum/viewtopic.php?f=3&t=6044) to perform the substitution. Or you could have your batch script generate download.shtml without bothering with the intermediate download.volt by employing a [batch script heredoc](http://stackoverflow.com/a/15032476/1683264). – rojo Jan 26 '15 at 15:38
  • Thanks for the feedback. Apparently I was suffering a case of the Fridays. This morning it is working! – Shawn Farmer Jan 26 '15 at 16:32

0 Answers0