Solved

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

Posted on 2006-06-30
11
597 Views
Last Modified: 2008-02-20
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??
0
Comment
Question by:stvbrx
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 30

Expert Comment

by:callrs
Comment Utility
I saw this before somewhere...did you ask a similar question? I mentioned OM there, which makes such changes a snap...
0
 

Author Comment

by:stvbrx
Comment Utility
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
 
LVL 30

Expert Comment

by:callrs
Comment Utility
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
 
LVL 57

Expert Comment

by:giltjr
Comment Utility
Does remotedbname and DSN have to be on the same line?  Can they be on different lines?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 57

Expert Comment

by:giltjr
Comment Utility
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
 

Author Comment

by:stvbrx
Comment Utility
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
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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
 
LVL 30

Expert Comment

by:callrs
Comment Utility
>>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
 
LVL 30

Accepted Solution

by:
callrs earned 500 total points
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now