I'm making a script in hta and needed him to have the following functions:
It has the following functions:
- Do not open the script if the file .txt does not exist.
- Remove the the inputbox and still be able to save the changes.
- If the User try to save an empty text it presents an error.
- If the User makes a valid modification (not empty) present a message stating that it was saved.
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>