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
636 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 30

Expert Comment

by:callrs
ID: 17021472
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
ID: 17021572
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
ID: 17021625
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 57

Expert Comment

by:giltjr
ID: 17021764
Does remotedbname and DSN have to be on the same line?  Can they be on different lines?
0
 
LVL 57

Expert Comment

by:giltjr
ID: 17021781
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
ID: 17024985
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 85

Expert Comment

by:oBdA
ID: 17026387
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
ID: 17182218
>>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
ID: 17200181
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction How to create multiboot configuration with XP\Vista and Windows 7 on it? And most important question - how to do this correctly so not to have any kind of nightmares we get when system gets screwed? First of all one should realize t…
Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

621 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