2

I've searched a thousand of example and tried, but none of them actually works for me. My requirement is pretty straight forward, I have a file - config.txt, it has one of lines:

sqlServer=localhost

I'm trying to update this line to:

sqlServer=myMachine\sql2012

I looked examples online, some of them are just working with set variables, or some are not replacing but inserting. There are some examples are writing into a new file, but the new file has line number in front of each line. I don't find a useful instruction how to write batch scripts, and none of the update file batch scripts works for me.

It will be very helpful if you leave some comments.

Thanks in advance

user3621995
  • 33
  • 1
  • 1
  • 3

2 Answers2

9

EDITED - to adapt to comments

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "config=.\config.txt"
    set "dbServer=localhost\sql2012"

    for /f "tokens=*" %%l in ('type "%config%"^&cd.^>"%config%"'
    ) do for /f "tokens=1 delims== " %%a in ("%%~l"
    ) do if /i "%%~a"=="sqlServer" (
        >>"%config%" echo(sqlServer=%dbServer%
    ) else (
        >>"%config%" echo(%%l
    )

    type "%config%"

    endlocal

Input file is read line by line (for /f %%l), then each line is split (for /f %%a) and if the first token in the line is "sqlserver" then the line is replaced, else the original line is sent to file.

The command used to retrieve the information in the first for loop includes an cd.>"%config%" that will remove the contents of the file, so the final resulting lines (that have been read in memory by the for command before removing them) are sent directly to the same file.

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • what if I want to pass parameter to assign the sqlServer value? I updated to: set dbServer=localhost\sql2012 .............. SET SqlServer=%dbServer% but it's not working – user3621995 May 12 '14 at 19:09
  • @user3621995, Not sure about the code in your comment. See updated answer. Is this what you were trying? – MC ND May 12 '14 at 19:18
  • It actually works, thanks for the reply. The problem before has something to do with the file coding, for "set", it adds some special characters before it, so it's not recognized as a command. I created another batch file, and copied everything and it worked. Is this scripts only search for the token, meaning the start string separated by space? I have another example , I need to replace "localhost" to %dbServer%, but the scripts is not working, I think it cannot find out "DatabaseServer" because it's in the middle of a token, right? – user3621995 May 12 '14 at 21:06
5

You can do this:

FINDSTR /I /V /B "sqlServer=" config.txt > config.new
ECHO sqlServer=myMachine\sql2012 >> config.new
DEL config.txt
REN config.new config.txt

The FINDSTR will remove all lines that start with sqlServer= and create a new file called newfile.

The ECHO will add a line at the end with sqlServer=MyMachine\sql2012.

The last two lines delete your existing config.txt and replace it with the output of the first two lines.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • That looks correct, except you should us a /q for no prompt del: `del /q config.txt` – Alex May 09 '14 at 22:49
  • hi Mark, the scripts removes all other lines as well, "sqlServer= "is one of the line, and there are other parameters i don't want to change, do you know how can keep those? – user3621995 May 12 '14 at 18:54
  • Change the last part of the very first line of my script so it exactly matches the line you want to remove, i.e. FINDSTR /V /B "sqlServer=localhost" Failing that, edit your question and show how your file looks. – Mark Setchell May 12 '14 at 19:19