全部博文(2065)
分类: 系统运维
2010-01-16 21:13:18
Vbs操作INI文件专题
整理时间:
介绍一下有关于VBS中的FSO操作
一、创建FSO对象
Dim fso
Set fso =CreateObject("Scripting.FileSystemObject")
二、访问现有的驱动器、文件和文件夹
方法依次为:fso.getDrive 、fso.getFolder、fso.getFile
示例:访问现有文件
Set f1 =
fso.getFile("D:\bf.vbs")
三、访问对象的属性
首先要得到一个对象的句柄,然后要访问它的属性。就是要获取到此对象的一个实例
Set fso
=CreateObject("Scripting.FileSystemObject")
Set f1 =
fso.GetFile("D:\bf.vbs")
MsgBox
f1.DateLastModified
FileSystemObject(FSO) 对象模式包含下面的对象和集合。
对象/集合 |
描述 |
FileSystemObject |
主对象。包含用来创建、删除和获得有关信息,以及通常用来操作驱动器、文件夹和文件的方法和属性。和该对象相关联的许多方法,与其他 FSO 对象中的方法完全相似;它们是为了方便才被提供的。 |
Drive |
对 象。包含用来收集信息的方法和属性,这些信息是关于连接在系统上的驱动器的,如驱动器的共享名和它有多少可用空间。请注意,"drive" 并非必须是硬盘,也可以是 CD-ROM 驱动器,RAM 磁盘等等。并非必须把驱动器实物地连接到系统上;它也可以通过网络在逻辑上被连接起来。 |
Drives |
集合。提供驱动器的列表,这些驱动器实物地或在逻辑上与系统相连接。Drives 集合包括所有驱动器,与类型无关。要可移动的媒体驱动器在该集合中显现,不必把媒体插入到驱动器中。 |
File |
对象。包含用来创建、删除或移动文件的方法和属性。也用来向系统询问文件名、路径和多种其他属性。 |
Files |
集合。提供包含在文件夹内的所有文件的列表。 |
Folder |
对象。包含用来创建、删除或移动文件夹的方法和属性。也用来向系统询问文件夹名、路径和多种其他属性。 |
Folders |
集合。提供在 Folder 内的所有文件夹的列表。 |
TextStream |
对象。用来读写文本文件。 |
二、整理
如何让VBS脚本获取配置文件中的数据内容
我的INI文件内容如下:
[NASIP]
b1=222.179.110.250
[PASS]
b1=13213123223
[IP]
b1=192.168.20.25
[Initial
Catalog]
b1=hnebony
[User ID]
b1=sa
[Password]
b1=mypassword
读取INI内容的代码如下
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
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
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
Dim name,configpath
Set objShell = CreateObject("Wscript.Shell")
configpath = objShell.CurrentDirectory '得到当前脚本的目录位置
configpath = configpath & "\config.ini"
name = GetINIString("Initial Catalog", "b1",
"", configpath)
MsgBox name
PS:这样的话我就可以方便地在我的任务计划脚本中写上了!
三、从INI中修改值
我的INI文件内容如下:
[NASIP]
b1=222.179.110.250
[PASS]
b1=13213123223
[IP]
b1=192.168.20.25
[Initial
Catalog]
b1=mossdify
[User ID]
b1=sa
[Password]
b1=mypassword
修改的过程与函数如下:
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
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
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
调用方法
Dim
name,configpath
Set
objShell = CreateObject("Wscript.Shell")
configpath
= objShell.CurrentDirectory '得到当前脚本的目录位置
configpath
= configpath & "\config.ini"
WriteINIString
"Initial Catalog", "b1", "mossdify", configpath