标题: [文本处理] [已解决]批处理如何修改指定文件下某行的内容? [打印本页]
作者: huangbo8290 时间: 2019-1-9 13:07 标题: [已解决]批处理如何修改指定文件下某行的内容?
本帖最后由 huangbo8290 于 2019-1-10 13:13 编辑
XP系统文件是在C:\windows\system32
WIN7系统文件是在C:\windows\SysWOW64
原内容:
[HTTP_SERVER]
default.Host=10.73.189.14
default.Method=0
default.Port=7010
default.WebAppName=eapdomain
default.CHARSET = UTF-8
要把default.Host=10.73.189.14修改成default.Host=128.36.10.114其余的内容不变
作者: huangbo8290 时间: 2019-1-9 13:24
希望能增加批处理文件放在桌面就能查找到此文件后进行修改,而不是放在文件原目录下在运行批处理。
作者: huangbo8290 时间: 2019-1-9 14:07
文件:
作者: xczxczxcz 时间: 2019-1-9 15:10
本帖最后由 xczxczxcz 于 2019-1-9 19:35 编辑
win7 可能需要权限- $file='C:\windows\SysWOW64\eapagent.txt'
- $(GC $file |%{$_ -replace '(?<host>.*)=\d+(\.\d+){3}','${host}=128.36.10.114'}) | sc $file -force
复制代码
作者: yhcfsr 时间: 2019-1-9 16:11
本帖最后由 yhcfsr 于 2019-1-9 18:38 编辑
- @echo off
-
- set "IP=128.36.10.114"
-
- ver|find "5.1"&&cd/d "C:\windows\system32"||cd/d "C:\windows\SysWOW64"
- ren eapagent.txt eapagent.txt ||(echo;权限不够或文件不存在&pause&exit)
-
- (for /f "tokens=1*delims==" %%a in (eapagent.txt) do (
- if "%%b"=="" (echo;%%a
- ) else if "%%a"=="default.Host" (echo;%%a=%IP%
- ) else (echo;%%a=%%b)
- ))>tmp
- move /y tmp eapagent.txt>NUL&&echo;修改完成
- pause
复制代码
作者: ivor 时间: 2019-1-9 19:44
本帖最后由 ivor 于 2019-1-9 20:06 编辑
转载自:https://gallery.technet.microsof ... 0-bf32-eca99b4e42f4- $path = "C:\windows\SysWOW64\eapagent.ini"
- $ini=Get-IniContent $path
- $ini["HTTP_SERVER"]["default.Host"]="128.36.10.115"
- Out-IniFile -InputObject $ini -FilePath $path -Force
-
-
- Function Get-IniContent {
-
- [CmdletBinding()]
- Param(
- [ValidateNotNullOrEmpty()]
- [ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".ini")})]
- [Parameter(ValueFromPipeline=$True,Mandatory=$True)]
- [string]$FilePath
- )
-
- Begin
- {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}
-
- Process
- {
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"
-
- $ini = @{}
- switch -regex -file $FilePath
- {
- "^\[(.+)\]$" # Section
- {
- $section = $matches[1]
- $ini[$section] = @{}
- $CommentCount = 0
- }
- "^(;.*)$" # Comment
- {
- if (!($section))
- {
- $section = "No-Section"
- $ini[$section] = @{}
- }
- $value = $matches[1]
- $CommentCount = $CommentCount + 1
- $name = "Comment" + $CommentCount
- $ini[$section][$name] = $value
- }
- "(.+?)\s*=\s*(.*)" # Key
- {
- if (!($section))
- {
- $section = "No-Section"
- $ini[$section] = @{}
- }
- $name,$value = $matches[1..2]
- $ini[$section][$name] = $value
- }
- }
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"
- Return $ini
- }
-
- End
- {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
- }
-
- Function Out-IniFile {
-
- [CmdletBinding()]
- Param(
- [switch]$Append,
-
- [ValidateSet("Unicode","UTF7","UTF8","UTF32","ASCII","BigEndianUnicode","Default","OEM")]
- [Parameter()]
- [string]$Encoding = "Unicode",
-
-
- [ValidateNotNullOrEmpty()]
- [ValidatePattern('^([a-zA-Z]\:)?.+\.ini$')]
- [Parameter(Mandatory=$True)]
- [string]$FilePath,
-
- [switch]$Force,
-
- [ValidateNotNullOrEmpty()]
- [Parameter(ValueFromPipeline=$True,Mandatory=$True)]
- [Hashtable]$InputObject,
-
- [switch]$Passthru
- )
-
- Begin
- {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}
-
- Process
- {
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing to file: $Filepath"
-
- if ($append) {$outfile = Get-Item $FilePath}
- else {$outFile = New-Item -ItemType file -Path $Filepath -Force:$Force}
- if (!($outFile)) {Throw "Could not create File"}
- foreach ($i in $InputObject.keys)
- {
- if (!($($InputObject[$i].GetType().Name) -eq "Hashtable"))
- {
- #No Sections
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $i"
- Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" -Encoding $Encoding
- } else {
- #Sections
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing Section: [$i]"
- Add-Content -Path $outFile -Value "[$i]" -Encoding $Encoding
- Foreach ($j in $($InputObject[$i].keys | Sort-Object))
- {
- if ($j -match "^Comment[\d]+") {
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing comment: $j"
- Add-Content -Path $outFile -Value "$($InputObject[$i][$j])" -Encoding $Encoding
- } else {
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $j"
- Add-Content -Path $outFile -Value "$j=$($InputObject[$i][$j])" -Encoding $Encoding
- }
-
- }
- Add-Content -Path $outFile -Value "" -Encoding $Encoding
- }
- }
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Writing to file: $path"
- if ($PassThru) {Return $outFile}
- }
-
- End
- {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
- }
复制代码
作者: huangbo8290 时间: 2019-1-10 13:11
回复 5# yhcfsr
谢谢,完美解决!
作者: huangbo8290 时间: 2019-1-10 13:11
yhcfsr 发表于 2019-1-9 16:11
很感谢
作者: 滴血雄鹰 时间: 2019-1-13 16:00
yhcfsr 发表于 2019-1-9 16:11
批处理如何批量修改指定文件夹中文件的内容?
例:
c:\111\system.ini中有一段:pcname=AAA
想要将AAA改成123,批处理怎么写?
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |