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!

5
  • do you use any special characters in new_variable?
    – npocmaka
    Jan 23, 2015 at 23:00
  • Works quite happily for me - please state what you used for the replacement value.
    – Magoo
    Jan 24, 2015 at 2:44
  • I'm using a value of newstring. No special characters are needed. Jan 24, 2015 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 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.
    – rojo
    Jan 26, 2015 at 15:38
  • Thanks for the feedback. Apparently I was suffering a case of the Fridays. This morning it is working! Jan 26, 2015 at 16:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.