3

I'm making a script in hta and needed him to have the following functions:

It has the following functions:

  1. Do not open the script if the file .txt does not exist.
  2. Remove the the inputbox and still be able to save the changes.
  3. If the User try to save an empty text it presents an error.
  4. 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>
Humberto Freitas
  • 461
  • 1
  • 6
  • 21
  • Forgive me, is that you said you were not able to view and thought my description was wrong ... I have a code but needs a few adjustments, I posted the same. – Humberto Freitas Apr 08 '15 at 13:16
  • Well, this is much better now ; ). Just out of curiosity, is somewhere out there a VBS documentation, which tells you to put scripts between `head` and `body`? I mean I've seen a lot of these kind of codes, especially when VBS is involved. It works, but is considered invalid, and might cause troubles in some situations. – Teemu Apr 08 '15 at 13:54
  • I do not know to respond, although actually occur what you say, some of my scripts had error because of this format, though most of the scripts I make are based on some pre-existing and therefore usually leave the way the author began. – Humberto Freitas Apr 08 '15 at 14:44

1 Answers1

2

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
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • Unfortunately this mess in the code was me who made you and wiped the code so I could edit and finish even with a very precarious knowledge hta ... THANK YOU. – Humberto Freitas Apr 08 '15 at 18:18