CreateTextFile&OpenTextFile

创建与操作文本文件
本文介绍了如何使用FileSystemObject创建和操作文本文件的方法,包括创建新文件及读写操作。提供了JScript和VBScript示例代码。
创建指定的文件名并返回一个 TextStream 对象,可以使用这个对象对文件进行读写。

object.CreateTextFile(filename[, overwrite[, unicode]])
参数
object
必选项。应为 FileSystemObject 或 Folder 对象的名称。
filename
必选项。指明所要创建文件的字符串表达式。
overwrite
可选项。Boolean 值,指明能否覆盖已有文件。如果文件可以覆盖,则值为 true ,否则为 false。如果忽略,则已有文件不能被覆盖。
unicode
可选项。Boolean 值,指明文件是否以 Unicode 或 ASCII 文件方式创建。如果文件作为 Unicode 文件创建,则值为 true ,如果作为 ASCII 文件创建,则为 false。如果忽略,则假定为 ASCII 文件。
说明
下面的代码说明了如何使用 CreateTextFile 方法来创建和打开一个文本文件。

[JScript]
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c://testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
[VBScript]
Sub CreateAfile
    Dim fso, MyFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile = fso.CreateTextFile("c:/testfile.txt", True)
    MyFile.WriteLine("This is a test.")
    MyFile.Close
End Sub
如果 overwrite 参数是 false ,或者没有提供这个参数,那么对于已有的 filename 将产生一个错误。

打开指定的文件并返回一个 TextStream 对象,可以通过这个对象对文件进行读、写或追加。

object.OpenTextFile(filename[, iomode[, create[, format]]])
参数
object
必选项。object 应为 FileSystemObject 的名称。
filename
必选项。指明要打开文件的字符串表达式。
iomode
可选项。可以是三个常数之一:ForReading 、 ForWriting 或 ForAppending 。
create
可选项。Boolean 值,指明当指定的 filename 不存在时是否创建新文件。如果创建新文件则值为 True ,如果不创建则为 False 。如果忽略,则不创建新文件。
format
可选项。使用三态值中的一个来指明打开文件的格式。如果忽略,那么文件将以 ASCII 格式打开。
设置
iomode 参数可以是下列设置中的任一种:

常数 值 描述
ForReading 1 以只读方式打开文件。不能写这个文件。
ForWriting 2 以写方式打开文件
ForAppending 8 打开文件并从文件末尾开始写。

format 参数可以是下列设置中的任一种:

值 描述
TristateTrue 以 Unicode 格式打开文件。
TristateFalse 以 ASCII 格式打开文件。
TristateUseDefault 使用系统默认值打开文件。

说明
下面的代码说明了如何使用 OpenTextFile 方法打开文件并追加文本:

[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
' NC文件修改工具 - 终极PowerShell版(修复版) ' 文件名:NC_Editor_Ultimate_Fixed.vbs ' 修复内容:路径处理、错误处理、用户取消操作处理 ' 新增功能:文件查重避免二次修改 Set objFSO = CreateObject("Scripting.FileSystemObject") Set wshShell = CreateObject("WScript.Shell") ' 1. 读取上次路径(改进注册表存储方式) On Error Resume Next lastPath = wshShell.RegRead("HKCU\Software\NCEditor\LastPath") If Err.Number <> 0 Then lastPath = wshShell.ExpandEnvironmentStrings("%USERPROFILE%\Desktop") End If On Error GoTo 0 ' 2. 调用PowerShell高级文件夹选择对话框(改进版) psCommand = "Add-Type -AssemblyName System.Windows.Forms;" & _ "$$$dialog = New-Object Windows.Forms.FolderBrowserDialog;" & _ "$$dialog.Description = '请选择包含NC文件的文件夹';" & _ "$$$dialog.SelectedPath = '" & lastPath & "';" & _ "$$dialog.ShowNewFolderButton = $$$true;" & _ "if ($$dialog.ShowDialog() -eq 'OK') { $dialog.SelectedPath } else { '' }" Set psExec = wshShell.Exec("powershell -WindowStyle Hidden -command """ & psCommand & """") Do While psExec.Status = 0 WScript.Sleep 100 Loop folderPath = Trim(psExec.StdOut.ReadAll) folderPath = Replace(folderPath, """", "") folderPath = Replace(folderPath, vbCrLf, "") folderPath = Replace(folderPath, vbLf, "") If folderPath = "" Then MsgBox "未选择文件夹,操作已取消", vbInformation, "提示" WScript.Quit End If If Not objFSO.FolderExists(folderPath) Then MsgBox "指定的路径不存在: " & folderPath & vbCrLf & _ "请确认路径是否正确且不包含特殊字符", vbExclamation, "错误" WScript.Quit End If wshShell.RegWrite "HKCU\Software\NCEditor\LastPath", folderPath, "REG_SZ" Set colFiles = objFSO.GetFolder(folderPath).Files successCount = 0 errorCount = 0 errorLog = "" skippedCount = 0 For Each file In colFiles If LCase(objFSO.GetExtensionName(file.Name)) = "nc" Then filePath = folderPath & "\" & file.Name ' 查重检查(简化版,只检查第一行) On Error Resume Next Set f = objFSO.OpenTextFile(filePath, 1) firstLine = f.ReadLine f.Close If Err.Number <> 0 Then errorCount = errorCount + 1 errorLog = errorLog & "文件: " & file.Name & " 读取错误: " & Err.Description & vbCrLf Err.Clear On Error GoTo 0 GoTo NextFile End If If InStr(firstLine, "G54") > 0 Then skippedCount = skippedCount + 1 errorLog = errorLog & "跳过: " & file.Name & " (文件已包含G54)" & vbCrLf On Error GoTo 0 GoTo NextFile End If ' 文件处理 On Error Resume Next Set f = objFSO.OpenTextFile(filePath, 1) content = f.ReadAll f.Close firstT = True newContent = "" For Each line In Split(content, vbCrLf) trimLine = Trim(line) If trimLine <> "" Then If Left(trimLine, 1) = "T" Then toolNum = Mid(Split(trimLine, " ")(0), 2) If firstT Then newContent = newContent & "G54" & vbCrLf newContent = newContent & "T" & toolNum & "M6" & vbCrLf newContent = newContent & "S18000M3" & vbCrLf newContent = newContent & "G43H" & toolNum & vbCrLf firstT = False Else newContent = newContent & "M5" & vbCrLf newContent = newContent & "T" & toolNum & "M6" & vbCrLf newContent = newContent & "S18000M3" & vbCrLf newContent = newContent & "G43H" & toolNum & vbCrLf End If ElseIf Not Left(trimLine, 4) = "M03 " Then newContent = newContent & line & vbCrLf End If End If Next If Right(newContent, 15) <> "G00X0Y2400Z30.000" & vbCrLf & "M5" & vbCrLf & "M30" Then newContent = newContent & "G00X0Y2400Z30.000" & vbCrLf & "M5" & vbCrLf & "M30" End If Set f = objFSO.CreateTextFile(filePath, True) f.Write newContent f.Close If Err.Number = 0 Then successCount = successCount + 1 Else errorCount = errorCount + 1 errorLog = errorLog & "文件: " & file.Name & " 写入错误: " & Err.Description & vbCrLf Err.Clear End If On Error GoTo 0 End If NextFile: Next resultMsg = "处理完成!" & vbCrLf & _ "成功修改: " & successCount & " 个NC文件" & vbCrLf & _ "跳过已修改文件: " & skippedCount & " 个" If errorCount > 0 Then resultMsg = resultMsg & vbCrLf & _ "处理失败: " & errorCount & " 个文件" & vbCrLf & _ "错误日志已复制到剪贴板" Set objHTML = CreateObject("htmlfile") Set objClip = objHTML.parentWindow.clipboardData objClip.SetData "text", errorLog End If MsgBox resultMsg, vbInformation, "完成"
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值