vbs读写ini配置文件

感谢程序员先驱们的无私奉献,作为一名程序员我也希望能为江海填一滴水,哪怕为看到此文的同行节省一点时间,也是我无上的荣幸。

config.ini

[Age]
val=39
[Name]
val=andy

 test.vbs

Dim cp,configpath
Set objShell = CreateObject("Wscript.Shell")
configpath = objShell.CurrentDirectory                             '得到当前脚本的目录位置
configpath = configpath & "\config.ini"

WriteINIStringVirtual "Age", "val", "39", configpath            '修改配置项
cp = GetINIStringVirtual("Age", "val", "", configpath)         '读取配置项
MsgBox cp

Sub WriteINIStringVirtual(Section, KeyName, value, FileName)
WriteINIString Section, KeyName, value, FileName
End Sub
 Function GetINIStringVirtual(Section, KeyName, Default, FileName)
GetINIStringVirtual = GetINIString(Section, KeyName, Default, FileName)
End Function  



 ' Work with INI files In VBS (ASP/WSH)
' v1.00
' 2003 Antonin Foller, PSTRUH Software, http://www.pstruh.cz
' Function GetINIString(Section, KeyName, Default, FileName)
' Sub WriteINIString(Section, KeyName, value, FileName)

 Sub WriteINIString(Section, KeyName, value, FileName)
Dim INIContents, PosSection, PosEndSection

'Get contents of the INI file As a string
INIContents = GetFile(FileName)

'Find section
PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
If PosSection>0 Then
'Section exists. Find end of section
PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
'?Is this last section?
If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1

'Separate section contents
Dim OldsContents, NewsContents, Line
Dim sKeyName, Found
OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
OldsContents = split(OldsContents, vbCrLf)

'Temp variable To find a Key
sKeyName = LCase(KeyName & "=")

'Enumerate section lines
For Each Line In OldsContents
If LCase(Left(Line, Len(sKeyName))) = sKeyName Then
Line = KeyName & "=" & value
Found = True
End If
NewsContents = NewsContents & Line & vbCrLf
Next

If isempty(Found) Then
'key Not found - add it at the end of section
NewsContents = NewsContents & KeyName & "=" & value
Else
'remove last vbCrLf - the vbCrLf is at PosEndSection
NewsContents = Left(NewsContents, Len(NewsContents) - 2)
End If

'Combine pre-section, new section And post-section data.
INIContents = Left(INIContents, PosSection-1) & _
NewsContents & Mid(INIContents, PosEndSection)
else'if PosSection>0 Then
'Section Not found. Add section data at the end of file contents.
If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then 
INIContents = INIContents & vbCrLf 
End If
INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
KeyName & "=" & value
end if'if PosSection>0 Then
WriteFile FileName, INIContents
End Sub

 Function GetINIString(Section, KeyName, Default, FileName)
Dim INIContents, PosSection, PosEndSection, sContents, value, Found

'Get contents of the INI file As a string
INIContents = GetFile(FileName)

'Find section
PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
If PosSection>0 Then
'Section exists. Find end of section
PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
'?Is this last section?
If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1

'Separate section contents
sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)

If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then
Found = True
'Separate value of a key.
value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
End If
End If
If isempty(Found) Then value = Default
GetINIString = value
End Function

 ' Separates one field between sStart And sEnd
 Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
If PosB > 0 Then
PosB = PosB + Len(sStart)
Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
If PosE = 0 Then PosE = Len(sFrom) + 1
SeparateField = Mid(sFrom, PosB, PosE - PosB)
End If
End Function

 ' File functions
 Function GetFile(ByVal FileName)
Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
'Go To windows folder If full path Not specified.
If InStr(FileName, ":") = 0 And Left (FileName,2)<>"/" Then 
FileName = FS.GetSpecialFolder(0) & "" & FileName
End If
On Error Resume Next

GetFile = FS.OpenTextFile(FileName).ReadAll
End Function

 Function WriteFile(ByVal FileName, ByVal Contents)

Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
'On Error Resume Next

'Go To windows folder If full path Not specified.
If InStr(FileName, ":") = 0 And Left (FileName,2)<>"/" Then 
FileName = FS.GetSpecialFolder(0) & "" & FileName
End If

Dim OutStream: Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vbs 批量修改文件,bat 批处理文件调用执行vbs,并在cmd窗口打印返回值(vbs运行结果) 使用vbs 脚本对工作目录下的字符串进行替换并统计被修改的文件数主调的批处理方法将捕捉该vbs脚本运行后的结果并打印在cmd窗口。 示例代码(t.vbs)如下: Set fso=Wscript.CreateObject("Scripting.FileSystemObject") flrName="D:\Workspace\src\" 'dir that you need deal with count=0 'get the count of modified files function Traversal(dir) set flr=fso.getfolder(dir) set fs=flr.files findstr1="[assembly: SecurityTransparent]" 'find string that need to be replaced findstr2=replace(findstr1," ","") 'becasuse there isspace char in findstr,so add this findstr2 replaceStr="[assembly: AllowPartiallyTrustedCallers()]" 'destination stringin replace for each f in fs if lcase(f.name)="assemblyinfo.cs" then set findf=fso.opentextfile(f) do while findf.atendofstream=false d=f.datelastmodified 'get modified datetime of current file alltext=findf.readall if(InStr(alltext,findstr1)> 0 ) then s=replace(alltext,findstr1, replaceStr) count=count+1 findf.close() 'If no findstr2, delete the following elseif block elseif(InStr(alltext,findstr2) > 0) then s=replace(alltext,findstr2, replaceStr) count=count+1 findf.close() else findf.close() exit do end if set r=fso.opentextfile(f, 2, true) r.write s d2=f.datelastmodified if d2>=d then exit do loop end if next set fs=flr.subfolders for each f in fs Traversal(f.path) next end function Traversal(flrName) 'msgbox ("Done! "& count &" files were modified successfully.") wscript.echo "Done! "& count & " files were modified successfully." 'value popup by"wscript.echo" can be received by .bat easily Wscript.quit count 'this variable "count" in order to get a return value by%ErrorLevel% 上面的操作只执行了处理文件的步骤 如果不需要提取运行结果,在 批处理 或主调vbs脚本里直接 运行该vbs文件就行了。 如果在bat 里调用该vbs脚本,并希望打印出提示信息 可以用下面的两种方法来实现: 1,在bat中加入如下代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值