返回列表 发帖

[问题求助] [已解决]VBS如何读取unicode编码的inf文件里等号后面的内容

本帖最后由 pcl_test 于 2016-8-14 11:48 编辑

求助---VBS如何读取 inf 文件等号后面的内容 并赋给变量

问题描述:还是处理 unicode 格式的 inf 文件,比如:

  aaa.inf
   [Strings]
   BIOS = "abcd-abcd"
   TIDS  = ABCD

用 vbs (也要求是 unicode 格式)读取 BIOS 及 TIDS 等号后面的内容,并分别赋给变量 str1 和 str2

谢谢!
76626yyn

回复 1# yuanyannian

是不是这个问题太幼稚?
76626yyn

TOP

' 读INI文件
strIniFile = ".\test.inf"
strBIOS = ReadINI(strIniFile, "Strings", "BIOS")
strTIDS = ReadINI(strIniFile, "Strings", "TIDS")
MsgBox "BIOS = " & strBIOS & vbCrLf & "TIDS  = " & strTIDS, vbInformation
' read函数
Function ReadINI(FilePath, Bar, PrimaryKey)
Dim fso, sReadLine, i, j, ss
Set fso = CreateObject("Scripting.FileSystemObject")
Set IniFile = fso.opentextfile(FilePath, 1,-1)
Do Until IniFile.atendofstream
    sReadLine = IniFile.readline
    If sReadLine = "" Then
        IniFile.skipline
    ElseIf Trim(sReadLine) = "[" & Bar & "]" Then       '找到小节名
        '查找该小节名下的键名
        Do Until IniFile.atendofstream
            sReadLine = IniFile.readline                '读取小节名后的行
            j = InStr(sReadLine, "=")
            If j > 0 Then                               '小节名后的文本行存在
                If InStr(Left(sReadLine, j), PrimaryKey) > 0 Then                       '从"="左边字符串找到键名
                    ss = Trim(Right(sReadLine, Len(sReadLine) - InStr(sReadLine, "="))) '读取等号后的部分
                    Exit Do
                End If
            End If
        Loop
    End If
Loop
IniFile.Close
Set fso = Nothing
ReadINI = ss
End FunctionCOPY
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 3# yu2n

谢谢,试了一下,但读不出啊。
76626yyn

TOP

这种情况你就把你实际的那个文件和vbs都放到附件里面来
他发这么一个东西可能自己也试过一下的
有可能你那编码就不是unicode,而是utf-8之类的

TOP

回复 3# yu2n

上传需要处理的原文件,请老师给看一下。
文件大点,给个地址:

HIVESFT.rar

http://pan.baidu.com/s/1mgl24dy
76626yyn

TOP

回复 6# yuanyannian

思密达文字可能不兼容中文系统的 fso.opentextfile(file, 1,-1) Unicode 编码,我换 ADODB.Stream  Unicode 测试OK。
strIniFile = "V:\HIVESFT.INF"
strBIOS = ReadINI(strIniFile, "Strings", "BIOS")
strTIDS = ReadINI(strIniFile, "Strings", "NDIS")
strInfo = "BIOS = " & strBIOS & vbCrLf & "NDIS  = " & strTIDS
Msgbox strInfo, vbInformation
Function ReadIni(strIniFilePath, strPrimary, strSubKey)
  Dim objStream
  Set objStream = CreateObject("ADODB.Stream")
  With objStream
      .Type = 2
      .Mode = 3
      .Open
      .Charset = "Unicode"
      .LoadFromFile strIniFilePath
       strText = .ReadText
      .Close
  End With
  Set objStream = Nothing
  arrText = Split(strText, vbCrLf)
  For Each strLine In arrText
    If intCount = 0 Then
      If strLine = "[" & strPrimary & "]" Then
          intCount = 1
      End If
    Else
      If Left(strLine,1) = "[" Then Exit For
      j = InStr(strLine, "=")
      If j > 0 Then                               '小节名后的文本行存在
        If InStr(Left(strLine, j), strSubKey) > 0 Then                       '从"="左边字符串找到键名
          ReadIni = Trim(Right(strLine, Len(strLine) - InStr(strLine, "="))) '读取等号后的部分
          Exit For
        End If
      End If
    End If
  Next
End FunctionCOPY
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

本帖最后由 yuanyannian 于 2014-10-29 06:30 编辑

回复 7# yu2n

再次感谢了。
这样可以了,但问题又来了:就思密达而言,将读出的变量写入注册表,都成了“?????????”,请老师给看一下。
另外,不能读如:STANGDARD_NAME、KLT_63 等格式的变量。
76626yyn

TOP

回复 8# yuanyannian

1. 你是如何写入注册表的?
   建议注册表使用 regedit.exe /s abc.reg 的形式写入,其中 abc.reg 为Unicode编码。
2. 你将代码14行的Unicode编码改为其他编码试试。
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

本帖最后由 yuanyannian 于 2014-10-29 12:29 编辑

回复 9# yu2n

直接在 vbs 中:
    Set Reg=WScript.CreateObject("WScript.Shell")
    Reg.RegWrite "HKCU\yyn\Desktop\Wallpaper",strBIOS,"REG_SZ"
   
如果用 .reg ,需要用vbs先写入到 .reg 文件,而且必须是 unicode 格式,这个我不会,老师可否帮一帮?
76626yyn

TOP

回复 10# yuanyannian

读写Unicode文本:
Function WriteUnicodeText(File, TextString)
With CreateObject("ADODB.Stream")
.Type = 2 :  .Mode = 3 :  .Charset = "Unicode" :  .Open
.WriteText TextString  :  .SaveToFile File, 2 :  .Close
End With
End Function
Function ReadUnicodeText(File)
With CreateObject("ADODB.Stream")
.Type = 2 :  .Mode = 3 :  .Charset = "Unicode" :  .Open
.LoadFromFile File :  ReadUTF8Text = .ReadText :  .Close
End With
End FunctionCOPY
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 11# yu2n

抱歉,看不太懂,请帮写一个代码?
76626yyn

TOP

回复 12# yuanyannian

函数 WriteUnicodeText(File, TextString)
功能:将字符串以Unicode编码写入文本文件。
参数:File 为文本文件路径,TextString 为字符串。

函数 ReadUnicodeText(File, TextString)

……你一定是在逗我,这个就不解释了。
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 13# yu2n

抱歉,我是真的不会,前一阵子还在论坛中求助过,是可以创建一个文件,但只能全新覆盖写入,不能追加写入。
请老师帮忙,谢谢!
76626yyn

TOP

回复 14# yuanyannian

追加写入?
1. 读取旧文本,保存为字符串 str1
2. 将要保存的字符串 str2 与字符串 str1 合并为 str3 。(字符串使用 & 连接符)
3. 向文件写入 str3 ,即完成追加写入。
File = "d:\abc.txt"
str1 = ReadUnicodeText(File)    ' 读取
str2 = " 我是新字符串 "              ' 新字符串
str3 = str1 & str2                    ' 合并新旧字符串(追加)
WriteUnicodeText File, str3       ' 写入文件COPY
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

返回列表