OpenTextFile Method

本文介绍如何使用 OpenTextFile 方法打开文件,并返回 TextStream 对象,以便进行读取、写入或追加操作。支持 JScript 和 VBScript 两种语言。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

OpenTextFile Method

Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.

object.OpenTextFile(filename[, iomode[, create[, format]]])
Arguments
object
Required. Object is always the name of a FileSystemObject.
filename
Required. String expression that identifies the file to open.
iomode
Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create
Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created, False if it isn't created. If omitted, a new file isn't created.
format
Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.
Settings

The iomode argument can have any of the following settings:

ConstantValueDescription
ForReading1Open a file for reading only. You can't write to this file.
ForWriting2Open a file for writing.
ForAppending8Open a file and write to the end of the file.

The format argument can have any of the following settings:

ValueDescription
TristateTrueOpen the file as Unicode.
TristateFalseOpen the file as ASCII.
TristateUseDefaultOpen the file using the system default.
Remarks

The following code illustrates the use of the OpenTextFile method to open a file for appending text:

[JScript]
var fs, a, ForAppending;
ForAppending = 8;
fs = new ActiveXObject("Scripting.FileSystemObject");
a = fs.OpenTextFile("c://testfile.txt", ForAppending, false);
...
a.Close();
[VBScript]
Sub OpenTextFileTest
   Const ForReading = 1, ForWriting = 2, ForAppending = 8
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:/testfile.txt", ForWriting, True)
   f.Write "Hello world!"
   f.Close
End Sub
修改VBA代码,不要将@Backing(type="int") 误识别为方法Sub ProcessAidlFiles() Dim fso As Object Dim objFolder As Object Dim objFile As Object Dim wb As Workbook Dim ws As Worksheet, summaryWs As Worksheet Dim filePath As String, fileContent As String Dim newWs As Worksheet Dim targetSheetName As String Dim methodStart As Long, methodEnd As Long, endSearch As Long Dim methodName As String, methodLine As String Dim contentStart As Long Dim packagePos As Long Dim summaryRow As Long Set wb = ThisWorkbook Set summaryWs = wb.Sheets("Sheet1") If summaryWs Is Nothing Then Set summaryWs = wb.Sheets.Add summaryWs.Name = "Sheet1" End If filePath = wb.Path If Right(filePath, 1) <> "\" Then filePath = filePath & "\" Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(filePath) Then MsgBox "路径不存在: " & filePath, vbCritical Exit Sub End If Set objFolder = fso.GetFolder(filePath) summaryRow = 5 ' 从D5开始记录方法 ' 清空之前的汇总结果(保留标题) If summaryRow < summaryWs.Rows.Count Then summaryWs.Range("C" & summaryRow & ":D" & summaryWs.Rows.Count).ClearContents End If ' 处理每个aidl文件 For Each objFile In objFolder.Files If LCase(fso.GetExtensionName(objFile.Name)) = "aidl" And _ UCase(Left(objFile.Name, 1)) = "I" Then ' 打开文件读取内容 Set objStream = fso.OpenTextFile(objFile.Path, 1) fileContent = objStream.ReadAll objStream.Close ' 创建新工作表(文件名作为表名) On Error Resume Next targetSheetName = Left(fso.GetBaseName(objFile.Name), 31) & "." & fso.GetExtensionName(objFile.Name) If Not WorksheetExists(targetSheetName) Then Set newWs = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)) newWs.Name = targetSheetName Else Set newWs = wb.Sheets(targetSheetName) newWs.Cells.Clear End If On Error GoTo 0 ' 处理package行前内容 packagePos = InStr(1, fileContent, "package") If packagePos > 0 Then contentStart = InStr(packagePos, fileContent, vbCrLf) If contentStart = 0 Then contentStart = InStr(packagePos, fileContent, vbLf) If contentStart > 0 Then fileContent = Mid(fileContent, contentStart + 1) Else fileContent = Mid(fileContent, packagePos) End If End If ' 写入处理后的内容到A1 newWs.Range("A1").Value = fileContent ' 提取方法名并记录到Sheet1 methodStart = 1 endSearch = Len(fileContent) Do While methodStart < endSearch ' 查找方法开始位置 methodStart = InStr(methodStart, fileContent, "(") If methodStart = 0 Then Exit Do ' 回溯查找方法名 Dim charPos As Long charPos = methodStart - 1 Do While charPos > 0 Dim currentChar As String currentChar = Mid(fileContent, charPos, 1) If currentChar = " " Or currentChar = vbCr Or currentChar = vbLf Then Exit Do End If charPos = charPos - 1 Loop If charPos > 0 Then methodName = Trim(Mid(fileContent, charPos + 1, methodStart - charPos - 1)) ' 检查方法名有效性 If methodName <> "" And InStr(methodName, " ") = 0 Then ' 查找方法结束位置 methodEnd = InStr(methodStart, fileContent, ";") If methodEnd > 0 Then ' 写入汇总表 summaryWs.Cells(summaryRow, "C").Value = newWs.Name summaryWs.Cells(summaryRow, "D").Value = methodName summaryRow = summaryRow + 1 End If End If End If methodStart = methodStart + 1 Loop End If Next objFile MsgBox "处理完成! 共找到 " & summaryRow - 5 & " 个方法", vbInformation End Sub ' 检查工作表是否存在 Function WorksheetExists(sheetName As String) As Boolean On Error Resume Next WorksheetExists = (Not Sheets(sheetName) Is Nothing) End Function
最新发布
06-06
fiddler编写自动化脚本导出文件但报错,以下是脚本内容以及我要获取的api 和获取的文本内容if (oSession.fullUrl.Contains("填写需要抓取的域名")) { var fso; var file; fso = new ActiveXObject("Scripting.FileSystemObject"); //文件保存路径,可自定义 file = fso.OpenTextFile("填写保存TXT文件地址",8 ,true, true); file.writeLine("Request url: " + oSession.url); file.writeLine("Request header:" + "\n" + oSession.oRequest.headers); file.writeLine("Request body: " + oSession.GetRequestBodyAsString()); file.writeLine("\n"); file.close(); }GET /api/im/messages/history?limit=20&chat_user_id=60c6905c000000000100a5c9&last_id=12&start_id=0 HTTP/1.1content={"content":"{\"title\":\"没去苏州!这里真的是广州!阴雨天真的好美\",\"image\":\"http://sns-img-al.xhscdn.com/1040g0083119lo4akma0048lunsumnadv45d7r5g?imageView2/2/w/360/format/jpg/q/75\",\"link\":\"xhsdiscover://item/66126b8b000000001a0118a6?app_platform=android&ignoreEngage=true&app_version=8.23.1&share_from_user_hidden=true&type=video&author_share=1&sourceId=message&feedType=single\",\"desc\":\"\",\"type\":\"note\",\"sourceTag\":\"\",\"noteType\":\"video\",\"user\":{\"id\":\"57c93d6ba9b2ed697085a9bf\",\"nickname\":\"He 琦琦\",\"image\":\"https://sns-avatar-qc.xhscdn.com/avatar/5c8f3f70540882000118c22b.jpg?imageView2/2/w/120/format/jpg\"},\"id\":\"66126b8b000000001a0118a6\",\"ts\":0,\"expression\":\"[{\\\"emoji\\\":\\\"[喝咖啡H]\\\",\\\"name\\\":\\\"已阅\\\"},{\\\"emoji\\\":\\\"[走起H]\\\",\\\"name\\\":\\\"走起\\\"},{\\\"emoji\\\":\\\"[派对R]\\\",\\\"name\\\":\\\"带我去\\\"}]\",\"cover\":\"http://sns-img-al.xhscdn.com/1040g0083119lo4akma0048lunsumnadv45d7r5g?imageView2/2/w/360/format/jpg/q/75\",\"minorPrice\":\"\",\"expectedPrice\":\"\",\"goodsImage\":\"\",\"goodsId\":\"\",\"itemTitle\":\"\",\"buttonLink\":\"\"}","content_type":3,"not_front_chain":false,"not_unread_count":false,"front_chain":"[笔记] 没去苏州!这里真的是广州!阴雨天真的好美"}
03-26
if (oSession.fullUrl.Contains("填写需要抓取的域名")) { var fso; var file; fso = new ActiveXObject("Scripting.FileSystemObject"); //文件保存路径,可自定义 file = fso.OpenTextFile("填写保存TXT文件地址",8 ,true, true); file.writeLine("Request url: " + oSession.url); file.writeLine("Request header:" + "\n" + oSession.oRequest.headers); file.writeLine("Request body: " + oSession.GetRequestBodyAsString()); file.writeLine("\n"); file.close(); }GET /api/im/messages/history?limit=20&chat_user_id=60c6905c000000000100a5c9&last_id=12&start_id=0 HTTP/1.1content={"content":"{\"title\":\"没去苏州!这里真的是广州!阴雨天真的好美\",\"image\":\"http://sns-img-al.xhscdn.com/1040g0083119lo4akma0048lunsumnadv45d7r5g?imageView2/2/w/360/format/jpg/q/75\",\"link\":\"xhsdiscover://item/66126b8b000000001a0118a6?app_platform=android&ignoreEngage=true&app_version=8.23.1&share_from_user_hidden=true&type=video&author_share=1&sourceId=message&feedType=single\",\"desc\":\"\",\"type\":\"note\",\"sourceTag\":\"\",\"noteType\":\"video\",\"user\":{\"id\":\"57c93d6ba9b2ed697085a9bf\",\"nickname\":\"He 琦琦\",\"image\":\"https://sns-avatar-qc.xhscdn.com/avatar/5c8f3f70540882000118c22b.jpg?imageView2/2/w/120/format/jpg\"},\"id\":\"66126b8b000000001a0118a6\",\"ts\":0,\"expression\":\"[{\\\"emoji\\\":\\\"[喝咖啡H]\\\",\\\"name\\\":\\\"已阅\\\"},{\\\"emoji\\\":\\\"[走起H]\\\",\\\"name\\\":\\\"走起\\\"},{\\\"emoji\\\":\\\"[派对R]\\\",\\\"name\\\":\\\"带我去\\\"}]\",\"cover\":\"http://sns-img-al.xhscdn.com/1040g0083119lo4akma0048lunsumnadv45d7r5g?imageView2/2/w/360/format/jpg/q/75\",\"minorPrice\":\"\",\"expectedPrice\":\"\",\"goodsImage\":\"\",\"goodsId\":\"\",\"itemTitle\":\"\",\"buttonLink\":\"\"}","content_type":3,"not_front_chain":false,"not_unread_count":false,"front_chain":"[笔记] 没去苏州!这里真的是广州!阴雨天真的好美"}
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值