文件已生成但文件中没有脚本

时间:2016-10-26 19:05:27

标签: vbscript readfile createfile

我编写了以下脚本,希望脚本从文件夹中的文件中获取信息,并将它们汇总到新创建的文件中。当我尝试运行这个脚本时,我得到错误(代码:800A1A8,第53行)“需要对象:'objReadFiles'”,但是当我在第53行上面插入'objReadFiles'时,我收到错误说“类型不匹配:” objReadFiles'“(代码:800A000D)。新文件也不包含任何内容。

'Create new output file
Set objFile = objFSO.CreateTextFile("M:\vbscripts\folder\TEST RUN\Summary.txt")


'Read through folder
Set objFolder = objFSO.GetFolder("M:\vbscripts\folder\TEST RUN\COMP")
Set colFiles = objFolder.Files 


'Get information and write it in file
For Each objFiles in colFiles
    strName = objFiles.Name
    num = len(strName) - 13
    string_part = left(strName, num)
    datetime = CDATE(objReadFiles.DateLastModified)
    Do While objReadFile.AtEndOfStream <> True
        contents = objReadFile.ReadLine
        If Not InStr(contents, "INSTALLED SOFTWARE") > 0 AND InStr(contents, " ") > 0 then
            objFile.Write string_part & "," & datetime & "," & contents & vbCRLF
        End If
    Loop
Next 


'Write result into new output file
objFile.Write "end of file"

2 个答案:

答案 0 :(得分:1)

重命名你的varaiables,以便清楚你在做什么。

我怀疑你正试图:

  • 创建输出文件
  • 处理目录中的所有文件并阅读每个文件以查找文本
  • 根据某些条件写入输出文件

此代码应该更好一点:

'Create new output file
Set objOutputFile = objFSO.CreateTextFile("M:\vbscripts\folder\TEST RUN\Summary.txt")

'Read through folder
Set objFolder = objFSO.GetFolder("c:\temp\batches\archive")

'Get information and write it in file
For Each objFile in objFolder.Files
    num = len(objFile.name) - 13
    string_part = left(objFile.name, num)
    datetime = CDATE(objFile.DateLastModified)

    set objReadStream = objFile.OpenAsTextStream()

    Do While objReadStream.AtEndOfStream <> True
        contents = objReadStream.ReadLine
        If Not InStr(contents, "INSTALLED SOFTWARE") > 0 AND InStr(contents, " ") > 0 then
            objOutputFile.Write string_part & "," & datetime & "," & contents & vbCRLF
        End If
    Loop

    objReadStream.Close()
Next 

objOutputFile.Close()

答案 1 :(得分:0)

我不知道我做了什么来修复它,但今天早上似乎工作了。只是想分享我的最终剧本:

Dim objFSO, objFolder, objFile, colFiles

'Format time stamp for YYYYMMDD_HHMM
strDay = Day(now)
If Len(strDay) < 2 Then
    strDay = "0" & strDay
End If

strMonth = Month(now)
If Len(strMonth) < 2 Then
    strMonth = "0" & strMonth
End If

strYear = Year(now)

strHour = Hour(now)
If Len(strHour) < 2 Then
    strHour = "0" & strHour
End If

strMinute = Minute(now)
If Len(strMinute) < 2 Then
    strMinute = "0" & strMinute
End If

Formated_Stamp = strYear & strMonth & strDay & "_" & strHour & strMinute


Set objFSO = CreateObject("Scripting.FileSystemObject")

'Rename old file
If objFSO.FileExists ("M:\vbscripts\folder\TEST\summary.txt") Then
    objFSO.MoveFile "M:\vbscripts\folder\TEST\summary.txt", "M:\vbscripts\folder\TEST\Old\summary_" & Formated_Stamp & ".txt"
End If

'Read through folder
Set objFolder = objFSO.GetFolder("M:\vbscripts\folder\TEST\COMP")
Set colFiles = objFolder.Files 

'Create new output file
Set objFile = objFSO.CreateTextFile("M:\vbscripts\folder\TEST\summary.txt")

'Get information and write it in file
For Each objFiles in colFiles
    Set objReadFile = objFSO.OpenTextFile(objFiles)
    Do While objReadFile.AtEndOfStream <> True
        contents = objReadFile.ReadLine
        If Not InStr(contents, "INSTALLED SOFTWARE") > 0 AND InStr(contents, " ") > 0 then
            datetime = CDATE(objFiles.DateLastModified)
            strName = objFiles.Name
            num = len(strName) - 13
            string_part = left(strName, num) 
            objFile.Write string_part & ", " & datetime & ", " & contents & vbCRLF
        End If
    Loop
Next 

objFile.Write "End of file."

Wscript.Echo "File has been created."