• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 667
  • Last Modified:

How do I modify a sql.ini file with a script to change the name of the SQL server to a new server?

I have a script to add a server to the sql.ini file:

@echo off
SET SQLINI=%userprofile%\Application Data\(App Name)\SQL.INI
findstr /I (new server name) "%SQLINI%"
If errorlevel 1 goto :AddLine
Goto :EOF
Echo remotedbname=(new server name),DSN=(app name here) (new server name here)>>"%SQLINI%"
Goto :EOF

I need to find a server name, and either delete the line with the server name, or change the server name to "DO NOT USE". How would I script this??
  • 4
  • 2
  • 2
  • +1
1 Solution
I saw this before somewhere...did you ask a similar question? I mentioned OM there, which makes such changes a snap...
stvbrxAuthor Commented:
I just asked a similar question the other day which helped to create the script you see listed.
It was created to add a new sql server name.
Now I need to modify it to delete the original server name.

Thanks is advance!
MS-DOS doesn't make it easy.  Unless you get the MS-DOS script working, some options are::--Read-made program if you can find one, --write a high-level language program (C++, Python, etc), --Windows Scripting Host (VBScript etc.).  --But the easiest  solution I've used for years for a million tasks is what is mentioned in the last post. I'd even taken the time to customize a script for you...
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Does remotedbname and DSN have to be on the same line?  Can they be on different lines?
You can what I have below.  I wrote this (I think this is the final copy) to replace a string in a file and all I had was docs commands.  You MIGHT be able to leave DSN on the same line, I'm not 100% sure what this will do.

for /F "tokens=1-3* delims==" %%i in (SQL.ini) do call :FINDIT %%i %%j %%k

goto DONE

if %1 == remotedbname      set /a tm1 = newname      & goto SET1

echo %1=%2 %3 %4 %5 %6 %7 %8 %9 >> SQL.new
goto DONE

echo %1=(%tm1%) %3 %4 %5 %6 %7 %8 %9 >> SQL.new
goto DONE

rename sql.ini sql.bkup
rename sql.new sql.ini
stvbrxAuthor Commented:

Your script that you wrote did the job perfectly on my last post.
However, I've now been asked to remove the original server.

Is there a "delete line" or "remove line" command?.....something opposite of the "add line" command you wrote from your script?

Assuming that the old server name only appears in the line(s) which should be deleted, all you need is a "find" command (and a bit of error checking, just in case ...).
Oh, and note that there's a dedicated environment variable for the application data, "AppData" ("Application Data" is only named "Application Data" on English systems, %AppData% works everywhere).

@echo off
set SQLIni=%AppData%\(App Name)\SQL.ini
set SQLBak=%AppData%\(App Name)\SQL.bak
copy "%SQLIni%" "%SQLBak%"
if errorlevel 1 (
  echo Can't create backup file, exiting!
  goto :eof
) else (
  del "%SQLIni%"
type "%SQLBak%" | find /i /v "OldServerName" >>"%SQLIni%"
>>Your script that you wrote did the job perfectly on my last post. ...Is there a "delete line"...
stvbrx, sorry for delay due to various issues. But my last scripts can be customized to do what you need. Since you've tried QM now, the QM help file explains it all & the QM Forum also has answers.  
It's late (I was worn out earlier this month). But here's the customized Quick Macros script:


str myuser myclient myapp f kValue newServerName
myuser=getenv("username"); myapp="Some App"; myclient.from("XP" myuser)
f.format("C:\Documents and Settings\%s\Application Data\%s\SQL.ini" myuser myapp)


 Next, if there was only one remotedbname key under [odbcrtr], then you could use a QM script such as:

 rget kValue "remotedbname" "odbcrtr" f
 if(kValue=newServerName) rset "DO NOT USE" "remotedbname" "odbcrtr" f

 But are there multiple "remotedbname" keys??? That's unusual, but if yes, then QM has string search & replace functions.
 Here's a complete & tested example (add to the Initialize code above):

str sf name findNameValue findKey("[odbcrtr][]")
int foundKeyAt foundNameAt

sf.getfile(f); newServerName="blablabla"
findNameValue.from(name newServerName)
out "searching for %s" findNameValue
 search for [odbcrtr] key
foundKeyAt=find(sf findKey)
      out "key %s not found. Aborting" findKey
 search for the server name & replace it if found
foundNameAt=find(sf findNameValue foundKeyAt)
      sf.replace("DO NOT USE", foundNameAt+name.len, newServerName.len)
      cop+ f f;; backup the ini file before overwriting it
      out "%s changed to %sDO NOT USE" findNameValue name
      out "%s was not found. No action taken" findNameValue

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now