HTA : Reading file issue - previous written by the same subroutine

Author Message
cippall

  • Total Posts : 31
  • Scores: 0
  • Reward points : 0
  • Joined: 2/3/2009
  • Status: offline
HTA : Reading file issue - previous written by the same subroutine Tuesday, January 03, 2012 3:53 PM (permalink)
0
Hi !
I'm having a problem  reading the content of a file previous created by the same subroutine. When I ouput the content to a Messagebox it shows these characters "ÿþ1". I really have no clue where my code is failing.
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
 <meta http-equiv="X-UA-Compatible" content="IE=9" /> 
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
 <title>My HTML application</title> 
 <HTA:APPLICATION 
   APPLICATIONNAME="My HTML application" 
   ID="MyHTMLapplication" 
   VERSION="1.0"/> 
</head> <script language="VBScript"> 
Option Explicit Sub WriteResults 
Const ForReading = 1, ForWriting = 2 
Dim outputFSO : Set outputFSO = CreateObject("Scripting.FileSystemObject") 
Dim outputFile: Set outputFile = outputFSO.CreateTextFile("C:\Results.txt", ForWriting, True) 
Dim i, strComputer,strInLine 
Dim sampletext 
  For i = 0 to AvailableOptions.Options.Length - 1 
        If (AvailableOptions.Options(i).Selected) Then 
        strComputer = AvailableOptions.Options(i).Value 
    Dim strOutLine 
    strOutLine = strComputer & "Test text," 
    outputFile.WriteLine strOutLine 
     End If   
  Next   outputFile.Close 
  Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") 
     Dim objFile : Set objFile = objFSO.OpenTextFile("C:\Results.txt", ForReading) 
     
  sampletext = objFile.ReadAll 
  MsgBox sampletext 
  objFile.Close 
  
 End Sub </script> <body bgcolor="white">  <select size="13" name="AvailableOptions" style="width:110px" multiple="multiple" > 
  <option value="192.168.0.1">PC01</option> 
  <option value="192.168.0.3">PC02</option> 
  <option value="192.168.0.2">PC03</option> 
  <option value="192.168.0.4">PC04</option> 
  <option value="192.168.0.5">PC05</option> 
  <option value="192.168.0.6">PC06</option> 
  <option value="192.168.0.7">PC07</option> 
  <option value="192.168.0.8">PC08</option> 
  <option value="192.168.0.9">PC09</option> 
  <option value="192.168.0.10">PC10</option> 
  <option value="192.168.0.11">PC11</option> 
  <option value="192.168.0.12">PC12</option> 
  <option value="192.168.0.13">PC13</option> 
  <option value="192.168.0.14">PC14</option> 
 </select> 
<button type="button" onClick="WriteResults()" >Read</button> 
</body> 
</html> 

Thanks!
P.S. : Still can't use the code bloques the right way! Just press enter at lines 12 and 26 after Option Explicit and Next.
<message edited by cippall on Tuesday, January 03, 2012 4:09 PM>
 
#1
    ehvbs

    • Total Posts : 3320
    • Scores: 112
    • Reward points : 0
    • Joined: 6/22/2005
    • Location: Germany
    • Status: offline
    Re:HTA : Reading file issue - previous written by the same subroutine Tuesday, January 03, 2012 9:29 PM (permalink)
    0
    Your
    Set outputFile = outputFSO.CreateTextFile("C:\Results.txt", ForWriting, True)
    opens the file for Unicode - see
    object.CreateTextFile(filename[, overwrite[, unicode]])
     
     
    #2
      cippall

      • Total Posts : 31
      • Scores: 0
      • Reward points : 0
      • Joined: 2/3/2009
      • Status: offline
      Re:HTA : Reading file issue - previous written by the same subroutine Tuesday, January 03, 2012 10:05 PM (permalink)
      0
      Thank you, that explained a lot of my previous mistakes. I have though another small issue with this HTA and I may ask here first before opening another thread.
      For the time being I'm writing each line like this:
      strOutLine = strComputer & "Test text,"
      .
      How can I write the last line without the ","?

      <message edited by cippall on Tuesday, January 03, 2012 10:54 PM>
       
      #3
        ehvbs

        • Total Posts : 3320
        • Scores: 112
        • Reward points : 0
        • Joined: 6/22/2005
        • Location: Germany
        • Status: offline
        Re:HTA : Reading file issue - previous written by the same subroutine Tuesday, January 03, 2012 11:07 PM (permalink)
        0
        There are 3 strategies to concatenate/output the elements of a collection separated (not delimited!) by a separator:
        1. Join - collection must be joinable (=> Array)
        2. For loop with special treatment of first (or last) element - collection must support access by index
        3. For Each loop with separator change - clumsy but 'always' applyable
        In code:
         
         >> a = Array(1,2,3)
        >> WScript.Echo Join(a,"-")
        >> WScript.Stdout.Write a(0)
        >> For i = 1 To UBound(a) : WScript.Stdout.Write "-" & a(i) : Next
        >> WScript.Echo ""
        >> s = ""
        >> For Each e In a : WScript.StdOut.Write s & e : s = "-" : Next
        >> WScript.Echo ""
        >>
        1-2-3
        1-2-3
        1-2-3 

         
         
        #4
          59cobalt

          • Total Posts : 969
          • Scores: 91
          • Reward points : 0
          • Joined: 7/17/2011
          • Status: online
          Re:HTA : Reading file issue - previous written by the same subroutine Wednesday, January 04, 2012 10:26 AM (permalink)
          0
          ehvbs
          2. For loop with special treatment of first (or last) element - collection must support access by index
          Not necessarily. You could use a boolean indicator to distinguish between the first and all other elements:
          isFirstItem = True
          For Each item In collection
           If isFirstItem Then
           WScript.StdOut.Write item
           isFirstItem = False
           Else
           WScript.StdOut.Write "-" & item
           End If
          Next
          This code snippet prepends the separator to all items except for the first one, because with a non-indexed collection it'd be more difficult to detect the last item (which is what you need to do to be able to append the separator to all items except for the last one).
           
          #5
            ehvbs

            • Total Posts : 3320
            • Scores: 112
            • Reward points : 0
            • Joined: 6/22/2005
            • Location: Germany
            • Status: offline
            Re:HTA : Reading file issue - previous written by the same subroutine Wednesday, January 04, 2012 10:45 AM (permalink)
            0
            I would classify your case as a variation of (3) - do something special in the For Each loop to avoid a leading or trailing separator. What I intended with (2) was to illustrate how you can keep the loop clean/simple, if you handle the first (or last) item separately/out of the loop.
             
            #6
              cippall

              • Total Posts : 31
              • Scores: 0
              • Reward points : 0
              • Joined: 2/3/2009
              • Status: offline
              Re:HTA : Reading file issue - previous written by the same subroutine Sunday, January 08, 2012 10:40 AM (permalink)
              0

              Thank you both for replying to my questions. Because my lack of knowledge I ended up writing the file with each line followed by comma and opening it again to delete the last 3 characters (carriage return and comma) which worked alright.But i would like try your solutions also.
               
              The solution using isFirstItem works only for first item very well and I'll keep that in mind next time. 
               
              Now, i'm intrigued about reading the selected option into a indexed array for future knowledge. Being given  this select element
               
               <select size="13" name="AvailableOptions" style="width:110px" multiple="multiple" >
                <option value="192.168.0.1">PC01</option>
                <option value="192.168.0.3">PC02</option>
                <option value="192.168.0.2">PC03</option>
                <option value="192.168.0.4">PC04</option>
                <option value="192.168.0.5">PC05</option>
                <option value="192.168.0.6">PC06</option>
                <option value="192.168.0.7">PC07</option>
                <option value="192.168.0.8">PC08</option>
                <option value="192.168.0.9">PC09</option>
                <option value="192.168.0.10">PC10</option>
                <option value="192.168.0.11">PC11</option>
                <option value="192.168.0.12">PC12</option>
                <option value="192.168.0.13">PC13</option>
                <option value="192.168.0.14">PC14</option>
               </select>
               
               
              I've managed to put the selected options into an array:
               
               
               Dim aSelected : aSelected = Array() 
                Dim nIdx 
                 For nIdx = 0 to AvailableOptions.Options.Length - 1 
                   
                   If AvailableOptions.Options(nIdx).Selected Then 
                    ReDim Preserve aSelected( UBound( aSelected ) + 1 ) 
                   aSelected( UBound( aSelected ) ) = AvailableOptions.Options(nIdx).Value 
                  End If 
                 Next 

               
              And this is where I think it could be done better:
               Dim strComputer   Dim aIdx : aIdx = 0        For Each strComputer In aSelected       If aIdx=UBound(aSelected) Then 
                   MsgBox strComputer 
                   Else 
                   MsgBox strComputer & "," 
                   End If 
                   aIdx=aIdx+1 
                   Next 

               
              For future knowledge also i would like to know a couple of things:
              1. how to retrieve the number of items in an array [ for the  time being i consider that to be ubound(a)+1]? is there any myarray.count method?
              2. how to create an indexed array? is the array i've created considered an indexed array ?

              Thanks!
              <message edited by cippall on Sunday, January 08, 2012 10:44 AM>
               
              #7
                59cobalt

                • Total Posts : 969
                • Scores: 91
                • Reward points : 0
                • Joined: 7/17/2011
                • Status: online
                Re:HTA : Reading file issue - previous written by the same subroutine Sunday, January 08, 2012 12:15 PM (permalink)
                0
                cippall
                1. how to retrieve the number of items in an array [ for the  time being i consider that to be ubound(a)+1]? is there any myarray.count method?
                No, the builtin arrays don't have a .Count method. UBound()+1 is the correct way to calculate the number of elements the array can hold.
                cippall
                2. how to create an indexed array? is the array i've created considered an indexed array ?
                An array is by definition always indexed.
                 
                #8
                  cippall

                  • Total Posts : 31
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 2/3/2009
                  • Status: offline
                  Re:HTA : Reading file issue - previous written by the same subroutine Sunday, January 08, 2012 11:13 PM (permalink)
                  0
                  Thanks.
                   
                  #9
                    59cobalt

                    • Total Posts : 969
                    • Scores: 91
                    • Reward points : 0
                    • Joined: 7/17/2011
                    • Status: online
                    Re:HTA : Reading file issue - previous written by the same subroutine Monday, January 09, 2012 12:45 AM (permalink)
                    0
                    You're welcome.
                     
                    #10

                      Online Bookmarks Sharing: Share/Bookmark

                      Jump to:

                      Current active users

                      There are 0 members and 1 guests.

                      Icon Legend and Permission

                      • New Messages
                      • No New Messages
                      • Hot Topic w/ New Messages
                      • Hot Topic w/o New Messages
                      • Locked w/ New Messages
                      • Locked w/o New Messages
                      • Read Message
                      • Post New Thread
                      • Reply to message
                      • Post New Poll
                      • Submit Vote
                      • Post reward post
                      • Delete my own posts
                      • Delete my own threads
                      • Rate post

                      2000-2012 ASPPlayground.NET Forum Version 3.9