使用vb脚本读取和存储数据

时间:2012-04-30 09:18:40

标签: file-io vbscript

我有一个包含以下内容的文件

  aaaaaaaaa    filename1.txt
  bbbbbbbbbb   filename2.dat

我想读取此文件并将数据分别存储到相关文件中。

例如: - aaaaa ---> filename1.txt

有人可以告诉我这样做的方法吗?如果您有任何实例,那将是一个真正的帮助!

Sub Main

Set ctlpath = "\\Download_Directory\CA.ctl"
Set subDirectory = "AUB"
Set MkrExtention = ".mkr"

ReadCtl(ctlpath,subDirectory,MkrExtention )

End Sub

 Function ReadCtl(ctlFileName,ctlSubDirectory,MarkerFileExtension)

Const ForReading = 1
Const ForWriting = 2

Dim lineData,hashValue,fileName,fileToWrite

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(ctlFileName) Then

Set objFile = objFSO.OpenTextFile(ctlFileName, ForReading)

Do Until objFile.AtEndOfStream

    lineData = objFile.ReadLine
    hashValue =Split(lineData,"  ") (0)
    fileName =Split(lineData,"  ") (1)

    WriteFileText("\\Download"+ctlSubDirectory+"
\"+fileName+MarkerFileExtension,hashValue)

Loop

objFile.Close
objFSO.Close

End If

ReadCtl = True

End Function

Function WriteFileText(sFilePath, sText)

Dim objFSO1 'As FileSystemObject
Dim objTextFile 'As Object

Set objFSO1 = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO1.CreateTextFile(sFilePath, True)

' Write hash data in to the file .mkr file
objTextFile.Write (sText)

objTextFile.Close
objFSO1.Close

 End Function

1 个答案:

答案 0 :(得分:1)

很长一段时间以来,你问这个,让我感到惊讶,没有人回答,这很简单。 希望你还在,无论如何,这里作为参考。这不是真正的哈希,例如在Ruby当然,但我保留了足够的脚本,以便你可以比较。

ReadCtl("file2hash1.txt")

function ReadCtl(ctlFileName)
  const ForReading = 1, ForWriting = 2, ForAppending = 8, createIfNeeded = true
  set oFso = createObject("Scripting.FileSystemObject")
  Set oRegExpre = new RegExp
  oRegExpre.Global = true
  oRegExpre.Pattern = "(.*) +(.*)"
  if oFso.FileExists(ctlFileName) then
    set objFileRead  = oFso.OpenTextFile(ctlFileName, ForReading)
    do until objFileRead.AtEndOfStream
      lineData = objFileRead.ReadLine
      hashValue = oRegExpre.Replace(lineData,"$1")
      fileName  = oRegExpre.Replace(lineData,"$2")
      if oFso.fileExists(fileName) then
        modus = ForAppending
      else
        modus = ForWriting
      end if
      set objFileWrite = oFso.OpenTextFile(fileName, modus, createIfNeeded)
      objFileWrite.writeLine hashValue
      objFileWrite.close
    loop
    objFileRead.close
  end if
  set oFso = nothing
end function