I'm making a script in hta and needed him to have the following functions:
It has the following functions:
CODE:
<HTML>
<head><title>Name</Title>
<HTA:Application
Border= "thin"
Application="/md/input"
Scoll="NO"
Singleinstance="Yes"
Icon="01.ico">
ShowInTaskbar="Yes"
Caption="Yes">
</Head>
<Script Language="VBSCRIPT" Type = "text/vbscript">
Sub Window_Onload
Window.resizeTo 400,500
End Sub
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set fSo1 = CreateObject("Scripting.FileSystemObject")
wkDir = "test.txt"
'----------------------------------------------------------
sub Window_onLoad()
Window.resizeTo 400,500
set oFSO=CreateObject("Scripting.FileSystemObject")
set oFile=oFSO.OpenTextFile("Test.txt",1)
text=oFile.ReadAll
document.all.DataArea.value=text
oFile.Close
end sub
'----------------------------------------------------------
FUNCTION SaveFile(FileName, DataArea)
CALL FileStat(FileName, msg)
on error resume next
sFile = wkDir & FileName.value
Set wrFile = fSo1.OpenTextFile(sFile, ForWriting)
wrFile.writeline(DataArea.value)
self.close
END FUNCTION
'----------------------------------------------------------
FUNCTION CloseFile(FileName, DataArea)
Call FileStat(FileName, msg)
on error resume next
cFile = wkDir & FileName.value
Set wrFile = fSo1.OpenTextFile(cFile, ForAppending)
wrFile.Close
DataArea.value = ""
FileName.Value = ""
END FUNCTION
'----------------------------------------------------------
FUNCTION QuitEdit
self.close
END FUNCTION
'----------------------------------------------------------
FUNCTION FileStat(FileName, msg)
eFile = wkDir & FileName
IF (fSo1.FileExists(eFile)) THEN
msg = oFile & " exists."
ELSE
on error resume next
END IF
END FUNCTION
'----------------------------------------------------------
</Script>
<body bgcolor="C0C0C0">
<Table>
<Th> Name </Th>
<TR><td><input type="text" name="FileName"></td></TR>
</Table>
<Table border="2">
<TR><td>
<textarea name="DataArea" rows="18" cols=37></textarea>
</td></TR>
<TR><td>
<input type="BUTTON" value="Save" onclick="SaveFile FileName, DataArea">
<input type="BUTTON" value="Cancel" onclick="QuitEdit">
</td></TR>
</Table>
</body>
</html>
90% of problems are caused by code written. So the easy way to get a good script is to get rid of that code. 4% of VBScript problems are caused by not starting with "Option Explicit". So let's add that. 4% of VBScript problems are caused by hiding errors via "On Error Resume Next". Never use this globally and never without a check at most two lines after it.
Deleting (a) everything that has nothing to do with editing a file (Icon, other irrelevant HTA properties, resize, repetions (wkDir, "test.txt"), things that look like being written by a programmer paid per hour (Call statements, Functions without return, variable used once) and (b) - for the moment - code that must be written solve the "editing a file" problem, we get:
<html>
<head>
<title>Edit File Demo</title>
<hta:application
id="demo"
></hta>
<script type="text/vbscript">
Option Explicit
Sub Window_OnLoad()
End Sub
Sub SaveFile()
End Sub
</script>
</head>
<body>
<form>
<textarea name="DataArea" rows="18" cols=37></textarea>
<input type="BUTTON" value="Save" onclick="SaveFile">
</form>
</body>
</html>
Without all the fat, putting the script into the head and the widgets into a form just comes naturally.
Now for the code needed: We need a file spec (better than just a name) and a/the one and only FileSystemObject (never to be named ofs1); if the file exists, it should be loaded into the textarea; the content of the textarea should be saved. So the new VBScript part is:
Option Explicit
Const csFSpec = "E:\trials\SoTrials\answers\8841045\hta\29505115.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
document.all.DataArea.value = csFSpec & " created"
End If
End Sub
Sub SaveFile()
goFS.CreateTextFile(csFSpec).Write document.all.DataArea.value
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With