0

finally created an account. Learning a lot these past few weeks and have created a batch tool for my fellow techs to use in their travels as we deal with only one software and it's plugins.

I've got this file called sifiledb.ini which has a key and property that needs to be changed fairly often. It's terrible to move to 20 different workstations to do this so I thought I'd wrap it into my batch file.

In this file (sifiledb.ini) we see this.

[FromStation0]
file=\\hostname\PDATA\siomin.sdx

And we need only edit the hostname - the rest of the property needs to stay there. Thing is, it's right in the middle of the ini and it's never in the same spot.

The rest of the file works perfectly and I've tried mixing some pieces in my file from here but I can't put it together.

UPDATE. See below for comment. This is what I have so far. Made a few edits for testing but I get "could not find string"

 if "%Oldserver%"=="" echo you need to specify the OLD server's hostname.
set "Oldserver="
set /p "Oldserver=Enter The Value: "
echo.
if "%Newserver%"=="" echo you need to specify the NEW server's hostname.
set "Newserver="
set /p "Newserver=Enter The Value: "
:: file containing string to replace
set file=C:\Sidexis\SIFILEDB.ini
:: string to replace in file (does NOT need to be the whole line)
set searchString=file=\\%Oldserver%\PDATA\siomin.sdx
:: string to write to file (DOES need to be the whole line)
set repString=file=\\%Newserver%\PDATA\siomin.sdx

setLocal enableDelayedExpansion
set count=0

if not exist %file% echo cannot find file - %file% & goto :EOF

:: Search for string - and get it's line number
for /F "delims=:" %%a in ('findstr /N /I /C:"%searchString%" "%file%"') do set searchLine=%%a

if not defined searchLine echo cannot find string - %searchString% - in file - %file% & goto :EOF

:: Read file into variables - by line number
for /F "delims=~!" %%b in ('type %file%') do (
    set /a count=!count!+1
    set line!count!=%%b
)

:: Edit the one line
set line%searchLine%=%repString%

:: Empty file and write new contents
del %file%
for /L %%c in (1,1,!count!) do echo !line%%c!>>%file%
cls
echo Done
pause

And here is my sifiledb.ini for reference if anyone wants to play with it.

[Location]
Station=JONMER-WIN7X64
DBOwner=
ODBCSrc=PDATA_SQLEXPRESS
ForceReadOnlyOnCreate=1
ForceReadOnlyOnRead=0
ForceReadOnlyOnUpdate=1
[Dialogues]
TimeoutDlg=60
TimeoutMsg=10
QuickSearch=1
DefPatSort=5010
PatListColWidth1=170
PatListColWidth2=75
PatListColWidth3=75
PatListColWidth4=90
PatListColWidth5=100
List=0
TakeGroup=??
[Export]
AddAcceptance=0
TestTimeDelta=90
SetLRLabelXP=1
[TestReports]
documentHeight=842
documentWidth=595
encoding1042=KSCms-UHC-H
encoding1041=90ms-RKSJ-H
encoding1028=ETen-B5-H
encoding2052=GBK-EUC-H
encoding1049=ISO8859-5
encoding1029=CP1250
font1042=Dotum
font1041=MS-Gothic
font1028=SimSun
font2052=MingLiU
[FromStation0]
File=\\JONMER-WIN7X64\PDATA\siomin.sdx
[FromStation1]
File=\\JONMER-WIN7X64\PDATA\siomin1.sdx
[Take]
SetLRLabelXS=1
SetLRLabelXP=1
[Teeth01XP]
00=1
[Teeth02XP]
00=1
[Teeth10XP]
00=1
[Teeth11XP]
00=1
[Teeth12XP]
11=1
12=1
21=1
22=1
31=1
32=1
41=1
42=1
51=1
52=1
61=1
62=1
71=1
72=1
81=1
82=1
[Teeth14XP]
20=1
30=1
[Teeth15XP]
10=1
40=1

FromStation0 is the entry that needs to be changed. FromStation1 can be ignored.

Found the solution! I removed too much and added too little. Here's the perfectly working end result. Thanks guys!

if "%Oldserver%"=="" echo you need to specify the OLD server's hostname.
set "Oldserver="
set /p "Oldserver=Enter The Value: "
echo.
if "%Newserver%"=="" echo you need to specify the NEW server's hostname.
set "Newserver="
set /p "Newserver=Enter The Value: "
:: file containing string to replace
set file=C:\Sidexis\SIFILEDB.ini
:: string to replace in file (does NOT need to be the whole line)
set searchstring=%Oldserver%\PDATA\siomin.sdx
:: string to write to file (DOES need to be the whole line)
set repstring=file=\\%Newserver%\PDATA\siomin.sdx

setLocal enableDelayedExpansion
set count=0

if not exist %file% echo cannot find file - %file% & goto :EOF

:: Search for string - and get it's line number
for /F "delims=:" %%a in ('findstr /N /I /C:"%searchstring%" "%file%"') do set searchLine=%%a

if not defined searchLine echo cannot find string - %searchstring% - in file - %file% & goto :EOF

:: Read file into variables - by line number
for /F "delims=~!" %%b in ('type %file%') do (
    set /a count=!count!+1
    set line!count!=%%b
)

:: Edit the one line
set line%searchLine%=%repstring%

:: Empty file and write new contents
del %file%
for /L %%c in (1,1,!count!) do echo !line%%c!>>%file%
cls
echo Done
pause
Redracer68
  • 958
  • 1
  • 7
  • 12

2 Answers2

0

Take a look at this question.

@echo off

if "%1"=="" echo you need to specify the hostname & goto :EOF

:: file containing string to replace
set file=sifiledb.ini
:: string to replace in file (does NOT need to be the whole line)
set searchString=\PDATA\siomin.sdx
:: string to write to file (DOES need to be the whole line)
set repString=file=\\%1\PDATA\siomin.sdx

setLocal enableDelayedExpansion
set count=0

if not exist %file% echo cannot find file - %file% & goto :EOF

:: Search for string - and get it's line number
for /F "delims=:" %%a in ('findstr /N /I /C:"%searchString%" "%file%"') do set searchLine=%%a

if not defined searchLine echo cannot find string - %searchString% - in file - %file% & goto :EOF

:: Read file into variables - by line number
for /F "delims=~!" %%b in ('type %file%') do (
    set /a count=!count!+1
    set line!count!=%%b
)

:: Edit the one line
set line%searchLine%=%repString%

:: Empty file and write new contents
del %file%
for /L %%c in (1,1,!count!) do echo !line%%c!>>%file%

pause

This can be quite easily modified to run twice using different search/replace parameters. Let me know if you need help with that.

Community
  • 1
  • 1
unclemeat
  • 5,029
  • 5
  • 28
  • 52
  • Seems like I was already trying that out... The main problem is that hostname is the name of a server and changes depending on our client. Which would mean changing this file upwards of 20 times a day. It's possible I may just have the batch ask for the old server name and the new server name and use those. – Redracer68 Jan 13 '14 at 22:42
  • if you call the batch script from the command line, you can call it like this - `C:\>repString.bat hostname` - then `%1` will output `hostname`. You can include this script as a part of a larger script. I'll try to modify it to be closer to exactly what you want. – unclemeat Jan 13 '14 at 22:46
  • @Redracer68 It should now be closer to what you want. – unclemeat Jan 13 '14 at 22:49
  • 1
    that is amazing. Exactly what I'm looking for. I'll see if I can implement into my existing tool and let you know the results! – Redracer68 Jan 13 '14 at 22:52
  • Ok, here is what I have so far. Just made a few modifications for testing.... But every time I test I get output saying "Could not find string in file" – Redracer68 Jan 14 '14 at 14:52
  • I wouldn't include `%oldserver%` at all. You don't need it, unless there are multiple instances of `*\PDATA\siomin.sdx` in the text file, otherwise it's unnecessary, and might be causing the problem. – unclemeat Jan 14 '14 at 22:19
  • Ah, I've just realized I missed left in `file=` when setting `searchString`. I will edit my answer. That is what was causing you to get the `string not found` error. It should now work, without having to include %oldserver%. – unclemeat Jan 14 '14 at 23:23
0

Another way to do it :

Just call this Bat with the newname as argument.

@echo off
setlocal EnableDelayedExpansion

set $Newhostname=%1

set $sw=0
for /f "delims=" %%a in ('type sifiledb.ini') do (
    set $test=%%a
    if !$test!==[FromStation0] (set $sw=1)
    if !$sw! Equ 1 (
        set $test=%%a
        if "!$test:~0,4!"=="file" call:change
    )
    echo !$test!>>NewIni.txt
)
del sifiledb.ini
ren Newini.ini sifiledb.ini
exit /b

:change
for /f "tokens=1,2,3,4 delims=\" %%b in ('echo !$test!') do set $test=%%b\\%$NewHostName%\%%d\%%e
set $sw=0
unclemeat
  • 5,029
  • 5
  • 28
  • 52
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • That's an interesting way to do it. I think I'll keep that in my back pocket. Got something coming up that will be perfect for this. Thanks! – Redracer68 Jan 14 '14 at 18:48