Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit text and save

Tags:

vbscript

hta

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>
like image 671
Humberto Freitas Avatar asked Mar 02 '26 23:03

Humberto Freitas


1 Answers

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
like image 53
Ekkehard.Horner Avatar answered Mar 06 '26 02:03

Ekkehard.Horner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!