gpt4 book ai didi

vbscript - 检查文件是否为空

转载 作者:行者123 更新时间:2023-12-02 15:24:30 26 4
gpt4 key购买 nike

我正在使用一个 .hta,它在文本文件中包含一个单词列表 当这个文件有 0kb 时就会出现问题...如果文件的大小为 0kb 而不是错误我会怎么做并加载空白文件?

我的代码:

  <script type="text/vbscript">
Option Explicit
Window.resizeTo 373,610
Const csFSpec = "MyList.TxT"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

Sub Window_OnLoad()
If goFS.FileExists(csFSpec) Then
document.all.DataArea.value = goFS.OpenTextFile(csFSpec).ReadAll()
Else

self.close
End If

If document.all.DataArea.value =vbcrlf Then

document.all.DataArea.value =""
Else

End If
End Sub

Sub SaveFile()
If document.all.DataArea.value = "" Then
document.all.DataArea.value =vbcrlf
goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value

self.close
Else
document.all.DataArea.value = Replace(document.all.DataArea.value, "\", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "/", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, ":", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "*", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "?", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, """", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "<", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, ">", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "|", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "&", "_")
document.all.DataArea.value = Replace(document.all.DataArea.value, "!", "_")
goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value

DIM objFSO
DIM objFile
DIM STRLINE
DIM STRNEWCONTENTS

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("MyList.TxT", ForReading)

Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile("MyList.TxT", ForWriting)
objFile.Write strNewContents
objFile.Close

document.all.DataArea.value = goFS.OpenTextFile(csFSpec).ReadAll()
document.all.DataArea.value = Replace(document.all.DataArea.value, vbcrlf,"|")
document.all.DataArea.value = left(document.all.DataArea.value,len(document.all.DataArea.value)-1)
document.all.DataArea.value = Replace(document.all.DataArea.value,"|",vbcrlf)
goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value

self.close
End if
End Sub

Sub QuitEdit()

self.close
End Sub

Sub Redefine()

document.all.DataArea.value ="CD" & vbcrlf & "Album" & vbcrlf & "Song" & vbcrlf & "DJ" & vbcrlf
End Sub

Sub Clean()

document.all.DataArea.value = ""

End Sub
</script>

最佳答案

验证文件存在后测试 .size(未测试):

If goFS.FileExists(csFSpec) Then
SET objFile = goFS.GetFile(csFSpec)
If objFile.Size > 0 Then
document.all.DataArea.value = goFS.OpenTextFile(csFSpec).ReadAll()
Else
'Do something else
End If
Else

关于vbscript - 检查文件是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com