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
:AddLine
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??
stvbrxAsked:
Who is Participating?
 
callrsConnect With a Mentor Commented:
It's late (I was worn out earlier this month). But here's the customized Quick Macros script:

 Initialize:

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)

newServerName="blablabla"

 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"
name="remotedbname="
findNameValue.from(name newServerName)
out "searching for %s" findNameValue
 search for [odbcrtr] key
foundKeyAt=find(sf findKey)
if(foundKeyAt<0)
      out "key %s not found. Aborting" findKey
      ret
 search for the server name & replace it if found
foundNameAt=find(sf findNameValue foundKeyAt)
if(foundNameAt>=0)
      sf.replace("DO NOT USE", foundNameAt+name.len, newServerName.len)
      cop+ f f;; backup the ini file before overwriting it
      sf.setfile(f)
      out "%s changed to %sDO NOT USE" findNameValue name
else
      out "%s was not found. No action taken" findNameValue

0
 
callrsCommented:
I saw this before somewhere...did you ask a similar question? I mentioned OM there, which makes such changes a snap...
0
 
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!
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
callrsCommented:
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...
0
 
giltjrCommented:
Does remotedbname and DSN have to be on the same line?  Can they be on different lines?
0
 
giltjrCommented:
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.

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

goto DONE

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

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

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

:DONE
rename sql.ini sql.bkup
rename sql.new sql.ini
0
 
stvbrxAuthor Commented:
callrs,

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?

thanks!
0
 
oBdACommented:
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
setlocal
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!
  pause
  goto :eof
) else (
  del "%SQLIni%"
)
type "%SQLBak%" | find /i /v "OldServerName" >>"%SQLIni%"
0
 
callrsCommented:
>>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.  
0
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.

All Courses

From novice to tech pro — start learning today.