0

I'm a newbie in vbs and try to make a script starting exe or services with a defined delay between each one I mixed it in a hta file and it works well. But I need to use it on many computers and then, need to make this script easier to configure and thought to add a config file .csv. But I don't success to make my script reading all the lines of my csv one after the other.

My script working:

<!-- ----- ExeScript Options Begin -----  ScriptType: window,invoker  DestDirectory: temp  Icon:
     C:\Users\stream\Desktop\startapp.bmp  File:
     C:\Users\stream\Desktop\startapp.bmp  OutputFile:
     C:\Users\stream\Desktop\test.exe  ----- ExeScript Options End -----
     -->

    <head>
    <title>Start App</title>

    <HTA:APPLICATION
      APPLICATIONNAME="Start App"
      ID="startapp"
      VERSION="1.3"
      MAXIMIZEBUTTON="no"
      ICON="startapp.ico"
      SINGLEINSTANCE="yes"
      SELECTION="no"/>

    <SCRIPT TYPE="text/javascript">
    window.resizeTo(350,275);
    window.moveTo(2,720);
    </SCRIPT>

    <SCRIPT language="vbscript">
    Dim WshShell

    Sub Window_onLoad
        StartTimer      
    End Sub

    Sub StartTimer
        MonTimer0 =window.setTimeOut ("welcome", 5000, "VBScript") 'Appel de MonScript
        MonTimer1 = window.setTimeOut ("Start01", 35000, "VBScript") 'Appel de MonScript 
        MonTimer2 = window.setTimeOut ("Start02", 45000, "VBScript") 'Appel de MonScript 
        MonTimer8 = window.setTimeOut ("Startend", 55000, "VBScript") 'Appel de MonScript 
        MonTimer9 = window.setTimeOut ("Startclose", 65000, "VBScript") 'Appel de MonScript 
    End sub

    Sub Welcome
    S = SetTimeOut("MaSub (""Vos applications vont se lancer dans un instant... Veuillez patienter..."")", 100)
    End Sub     

    Sub Start01
    Set WshShell = CreateObject ("Wscript.Shell")
    WshShell.Run """C:\Program Files\soft1\soft1.exe"""
    S = SetTimeOut("MaSub (""soft1 a bien été lancé !"")", 100)
    End Sub

    Sub Start02
    Set WshShell = CreateObject ("Wscript.Shell")
    WshShell.Run """C:\Program Files\soft2\soft2.exe"""
    S = SetTimeOut("MaSub (""Soft2 a bien été lancé !"")", 100)
    End Sub

    Sub Startend
    S = SetTimeOut("MaSub (""Toutes les applications ont été correctement lancées ! StartApp va se fermer..."")", 100)
    End Sub

    Sub Startclose
    window.close()
    End Sub

    Set WshShell = nothing

    </SCRIPT>

    </head>

    <Body STYLE="font:10 pt arial; color:white;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#000033', EndColorStr='#0000FF')">

    <p><center> <img src=".\startapp.bmp" /> </center></p>
    <br/><br/><br/>

    <p><center>
    <span style="color:white">
    <div id = D></div>

    <script language = vbs>
    Sub MaSub (E)
    Document.All.D.innerHTML = E
    ClearTimeOut S
    End Sub
    </script>
    </center></p>

    </span>
    </body>
    </html>

Now the code I'm trying to make... csv file:

    MonTimer0;Start01;5000;"C:\Program Files\soft1\soft1.exe";soft1 a bien été lancé !
    MonTimer1;Start02;10000;"C:\Program Files\soft1\soft2.exe";soft2 a bien été lancé !;

hta file:

    <!-- ----- ExeScript Options Begin -----
     ScriptType: window,invoker
     DestDirectory: temp
     Icon: D:\Bureau\startapp.bmp
     File: D:\Bureau\startapp.bmp
     OutputFile: D:\Bureau\test.exe
     ----- ExeScript Options End ----- -->

    <head>
    <title>Start App</title>

    <HTA:APPLICATION
      APPLICATIONNAME="Start App"
      ID="startapp"
      VERSION="1.2"
      MAXIMIZEBUTTON="no"
      ICON="startapp.ico"
      SCROLL="no"
      SINGLEINSTANCE="yes"
      SELECTION="no"/>

    <SCRIPT TYPE="text/javascript">
    window.resizeTo(350,275);
    window.moveTo(10,10);
    </SCRIPT>

    <SCRIPT language="vbscript">
      Dim FSO, LeFichier
                Dim CheminNomFichier, MeTbl, PourTbl, T


    Set fso = CreateObject("Scripting.FileSystemObject")
    CheminNomFichier =  "D:\Mes documents\PROGRAMMATION\Startapp\StartApp.csv"
    Set LeFichier = fso.OpenTextFile(CheminNomFichier,1)

    PourTbl = LeFichier.ReadAll

    LeFichier.Close

       Dim Var0, Var1, Var2, Var3, Var4
    ' .......................
    'PourTbl contient: "un;deux;trois;quatres;cinqiéme et dernier;"
        MeTbl = split(PourTbl,";")
        for T =0 to ubound(MeTbl)-1

        next
        var0 = MeTbl(0)
        var1 = MeTbl(1)
        var2 = MeTbl(2)
        var3 = MeTbl(3)
var4 = MeTbl(4)

    Dim WshShell

            Sub Window_onLoad
                StartTimer      
            End Sub

    Sub StartTimer
                var0 =window.setTimeOut ( var1 , var2, "VBScript") 'Appel de MonScript
            End sub

    Sub var1

    Set WshShell = CreateObject ("Wscript.Shell")
    WshShell.Run WshShell.ExpandEnvironmentStrings(var3)

    S = SetTimeOut("MaSub (var4)", 100)
    End Sub

    Sub Startend
    S = SetTimeOut("MaSub (""toutes les applications ont été correctement lancées !"")", 100)
    End Sub

    Sub Startend
    window.close()
    End Sub

    Set WshShell = nothing 
    </SCRIPT>

    </head>

    <Body STYLE="font:10 pt arial; color:white;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#000033', EndColorStr='#0000FF')">

    <p><center> <img src=".\startapp.bmp" /> </center></p>
    <br/><br/><br/>

    <p><center>
    <span style="color:white">
    <div id = D></div>

    <script language = vbs>
    Sub MaSub (E)
    Document.All.D.innerHTML = E
    ClearTimeOut S
    End Sub
    </script>
    </center></p>

    </span>
    </body>
    </html>

running the hta it tells me syntax error on line 68 [ sub (var1) ] and does nothing...

If you could help me, it would be very kind ! nb: I'm sorry if my English is a bit bad, it's a long time I didn't use this language.

odelh
  • 3
  • 3

2 Answers2

1
Set LeFichier = fso.OpenTextFile(CheminNomFichier,1)
PourTbl = LeFichier.ReadAll
LeFichier.Close
...
MeTbl = split(PourTbl,";")

The above is most likely what causes your problem that the HTA seems to not read all lines. If you read the entire CSV at once (ReadAll) you must split the text into lines before you split each line into separate fields:

Set LeFichier = fso.OpenTextFile(CheminNomFichier,1)
PourTbl = LeFichier.ReadAll
LeFichier.Close
...
For Each line In Split(PourTbl, vbNewLine)
  MeTbl = split(PourTbl,";")
  var0 = MeTbl(0)
  ...
Next

Otherwise you have to read the file line by line:

Set LeFichier = fso.OpenTextFile(CheminNomFichier,1)
...
Do Until LeFichier.AtEndOfStream
  PourTbl = LeFichier.ReadLine
  MeTbl = split(PourTbl,";")
  var0 = MeTbl(0)
  ...
Next
LeFichier.Close

Either way, you need to process the lines of the file in a loop.

Another issue is that the loop you have in your code

MeTbl = split(PourTbl,";")
for T =0 to ubound(MeTbl)-1

next
var0 = MeTbl(0)
var1 = MeTbl(1)
...

doesn't actually do anything. You split the content of the CSV, then increment T up to UBound(MeTbl)-1 without doing anything else, then assign the first 5 values (i.e. the values from the first line of the CSV) to variables.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Shouldn't `For Each line In Split(PourTbl)` be `For Each line In Split(PourTbl, vbCrLf)`? – Ekkehard.Horner May 22 '13 at 20:09
  • I prefer `vbNewLine`, but yes. Fixed. – Ansgar Wiechers May 22 '13 at 20:17
  • Thanks, I agree wrt vbNewLine. – Ekkehard.Horner May 22 '13 at 20:25
  • Hello! Could you have a look to my last code (24th, May), please?My script is running but all exe files are started at the same time instead of starting with the wanted delay.Thanks a lot for helping me – odelh May 29 '13 at 09:23
  • Hello Ansgar Wiechers![I'm going on, even if all isn't perfect... From the one hand]....(answered May 24 at 12:35) all is running but at the wrong time...all exe are launched at the same time and the delays and texts are displayed after – odelh May 30 '13 at 06:54
  • @odelh Your answer from May 24 was deleted by a moderator. That means it's no longer visible to anyone who doesn't have access to moderator tools. Why didn't you post it as a new question like I asked you? – Ansgar Wiechers May 31 '13 at 17:07
  • Oh.ok.I apologized, I hadn't understood that way, because on my way I still can see my answer. Then I'll create a new question.Thanks – odelh Jun 03 '13 at 07:31
0

You can't have a variable and a sub named var1. (And you shouldn't use such lousy names in the first place.)

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • Hello Ekkehard.Horner !First, why shouldn't I use this kind of name?Then, What is the way to name my sub with the variable's text ?Thank you – odelh May 22 '13 at 10:35
  • @odelh Names should be meaningful, "var1" etc aren't. If you want to call a function by a name provided at run-time, use GetRef() as shown here: http://stackoverflow.com/a/13127606/603855 – Ekkehard.Horner May 22 '13 at 13:26
  • I re wrote my code... It runs better but with many errors.but I have to wait 8 hours to post it 'cause it's to long for posting it in comments.btw I looked at your link and it will help me in many cases. – odelh May 22 '13 at 15:21