终于创建了一个帐户。过去几周学到了很多东西,并为我的同事们创建了一个批处理工具,用于他们的旅行,因为我们只处理一个软件及其插件。
我有一个名为sifiledb.ini的文件,它有一个需要经常更改的键和属性。移动到20个不同的工作站来执行此操作非常糟糕,所以我想我会将其包装到我的批处理文件中。
在这个文件(sifiledb.ini)中,我们看到了这一点。
[FromStation0]
file=\\hostname\PDATA\siomin.sdx
我们只需要编辑主机名 - 其余的属性需要留在那里。事实是,它正好在ini的中间,而且它永远不会在同一个地方。
文件的其余部分工作得很好,我尝试在这里混合一些文件,但我不能把它放在一起。
更新。 请参阅下面的评论。这就是我到目前为止所拥有的。做了一些测试编辑,但我得到“找不到字符串”
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
这是我的sifiledb.ini供参考,如果有人想玩它。
[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是需要更改的条目。 可以忽略FromStation1。
找到解决方案! 我删除了太多,添加太少。 这是完美的最终结果。 谢谢你们!
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
答案 0 :(得分:0)
@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
这可以非常容易地修改为使用不同的搜索/替换参数运行两次。如果您需要帮助,请告诉我。
答案 1 :(得分:0)
另一种方法:
只需使用newname作为参数调用此Bat。
@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