Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 需要使用.bat替换单个ini文件中的属性_Batch File_Cmd - Fatal编程技术网

Batch file 需要使用.bat替换单个ini文件中的属性

Batch file 需要使用.bat替换单个ini文件中的属性,batch-file,cmd,Batch File,Cmd,最后创建了一个帐户。在过去的几周里,我学到了很多东西,并为我的技术伙伴们创建了一个批处理工具,以便他们在旅行中使用,因为我们只处理一个软件和它的插件 我有一个名为sifiledb.ini的文件,它有一个需要经常更改的密钥和属性。移动到20个不同的工作站来做这件事是很糟糕的,所以我想我应该把它打包到我的批处理文件中 在这个文件(sifiledb.ini)中,我们看到了这一点 [FromStation0] file=\\hostname\PDATA\siomin.sdx 我们只需要编辑主机名-其余

最后创建了一个帐户。在过去的几周里,我学到了很多东西,并为我的技术伙伴们创建了一个批处理工具,以便他们在旅行中使用,因为我们只处理一个软件和它的插件

我有一个名为sifiledb.ini的文件,它有一个需要经常更改的密钥和属性。移动到20个不同的工作站来做这件事是很糟糕的,所以我想我应该把它打包到我的批处理文件中

在这个文件(sifiledb.ini)中,我们看到了这一点

[FromStation0]
file=\\hostname\PDATA\siomin.sdx
我们只需要编辑主机名-其余的属性需要保留在那里。事情是,它正好在INI的中间,而不是在同一个地点。 文件的其余部分工作得很好,我试着在我的文件中混合一些片段,但我不能把它们放在一起

更新。 请参阅下面的评论。这就是我目前所拥有的。为测试做了一些编辑,但我得到“找不到字符串”

这是我的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
看一看

这可以很容易地修改为使用不同的搜索/替换参数运行两次。如果您需要帮助,请告诉我。

另一种方法:

只要用新名称作为参数来调用这个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

好像我已经试过了。。。主要问题是主机名是服务器的名称,并且会根据客户机的不同而变化。这意味着每天更改此文件20次以上。我可能只是让批处理请求旧服务器名和新服务器名并使用它们。如果从命令行调用批处理脚本,可以这样调用-
C:\>repString.bat hostname
-然后
%1
将输出
hostname
。您可以将此脚本作为较大脚本的一部分包含。我将尝试修改它,使其更接近您想要的内容。@Redracer68它现在应该更接近您想要的内容。这太棒了。正是我要找的。我会看看我是否可以实现到我现有的工具,让你知道结果!好的,这是我到目前为止的资料。只是为了测试做了一些修改。。。。但每次我测试时,我都会得到输出,说“在文件中找不到字符串”,这是一种有趣的方法。我想我会把它放在后口袋里。接下来会有一些完美的东西。谢谢
@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
@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