0

I'm attempting a batch script to allow for user-entered data to replace variables in a .js file.

I am having issues with the completion of the script removing all data from the file.

I'm looking for a quick and easy way to repeatedly find and replace strings from a .js file based on user-entered values.


Below is the script I have attempted:

@echo off > header.js
setLocal DisableDelayedExpansion

::Declare variables and user entry
::if exist header.js ren header.js
set /p FolderLinkName= Folder Link Name? 
set /p PropertyName= Property Name? 
set /p PropertyAddress1= Property Address 1? 
set /p PropertyAddress2= Property Address 2? 
set /p PropertyCity= Property City? 
set /p PropertyState= Property State? 
set /p PropertyZip= Property Zip? 
set /p PropertyContactName = Property Contact Name? 
set /p PropertyPhone = Property Phone? 
set /p PropertyPhoneExt = Property Phone Ext? 
set /p PropertyEmail = Property Email? 

::String Replacements

for /f "tokens=* delims= " %%G in (header.js) do (
    set str=%%G
    setLocal EnableDelayedExpansion
    set str=!str:[var propertyFolderLink = "propertycopy";]=[var propertyFolderLink = "[%FolderLinkName%]";]!
    set str=!str:[var propertyName = "PROPERTY COPY";]=[var propertyName = "[%PropertyName%]";]!
    set str=!str:[var propertyAddress1 = "3050 Biscayne Blvd";]=[var propertyAddress1 = "[%PropertyAddress1%]";]!
    set str=!str:[var propertyAddress2 = "Suite 602";]=[var propertyAddress2 = "[%PropertyAddress2%]";]!
    set str=!str:[var propertyCity = "Miami";]=[var propertyCity = "[%PropertyCity%]";]!
    set str=!str:[var propertyState = "FL";]=[var propertyState = "[%PropertyState%]";]!
    set str=!str:[var propertyZip = "33137";]=[var propertyZip = "[%PropertyZip%]";]!
    set str=!str:[var propertyContact = "Property Manager";]=[var propertyContact = "[%PropertyContactName%]";]!
    set str=!str:[var propertyPhone = "(555) 555-55555";]=[var propertyPhone = "[%PropertyPhone%]";]!
    set str=!str:[var propertyPhoneExt = "";]=[var propertyPhoneExt = "[%PropertyPhoneExt%]";]!
    set str=!str:[var propertyEmail = "email@email.com";]=[var propertyEmail = "[%PropertyEmail%]";]!
    >> header.js echo(!str!
    endlocal
)
Desco
  • 3
  • 3
  • You cannot search/replace the `=` character within a batch script using the `!str:find=replace!` technique. There are many existing Q and A regarding modifying text files via batch. I recommend [JREPL.BAT](http://www.dostips.com/forum/viewtopic.php?f=3&t=6044) – dbenham Feb 26 '15 at 18:31
  • Thanks for the comment. I will look into JREPL.BAT as a worthy successor to this endeavor. Didn't realize "=" couldn't be utilized for string replacement. – Desco Feb 26 '15 at 19:22
  • You don't have to worry about poison characters if you use PowerShell. – Bill_Stewart Feb 26 '15 at 19:27

1 Answers1

0

The reason your file is being cleared is this line.

@echo off > header.js

The output of @echo off (an empty string) is replacing all of the text in header.js. Then, when you try to read lines from the file with the for statement, there aren't any to read. Remove the > header.js portion of this line.

Instead, write all output to a new file header.js.output and then at the very end of the script, copy that over the original file.

Second, the SET command isn't nearly as intelligent as you are attempting here. It merely does an exact string match of everything before the first equal sign to everything after it, and (for example) will replace the text [var propertyFolderLink with "propertycopy";]=[var propertyFolderLink = "[%FolderLinkName%]";]

To replace text with more complex expressions, you need a regex-based app like sed or jrepl to do the substitutions.

Alternatively, this whole script could be rewritten to be much simpler and more powerful in PowerShell. If you want to try that, then How can I replace every occurrence of a String in a file with PowerShell? should help you get started.

Community
  • 1
  • 1
Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
  • Ryan, thanks much for the reply. I am definitely open to suggestions to improve this endeavor. I attempted using batch operations because I am somewhat familiar, but I'm certainly not opposed to PowerShell to perform this operation. Thanks again for your feedback! – Desco Feb 26 '15 at 18:57
  • No problem. This question should get you started: http://stackoverflow.com/questions/17144355/string-replace-file-content-with-powershell – Ryan Bemrose Feb 26 '15 at 19:22