Отвечено vbs function arguments question

  • 7 мая 2012 г. 18:36
     
     

    I'm calling a test.vbs script and passing two arguments. I can get the first one to print, but when I add the second one it hangs. I'm thinking it has to do with the arguments, b/c if I drop var1 from the function and function call it places var2 in the text file. Not sure what's wrong. The call that i make to the file is

    test.vbs var1 var2

    here is the test.vbs code:

    Function WriteLineToFile(var1,var2)
      Const ForReading = 1, ForWriting = 2
      Dim fso, f
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.OpenTextFile("C:\test.txt", ForWriting, True)
      f.WriteLine var1
      f.WriteLine var2
      Set f = fso.OpenTextFile("C:\test.txt", ForReading)
      WriteLineToFile = f.ReadAll
    End Function

    Dim Arg, var1, var2
    Set Arg = WScript.Arguments
    var1= Arg(0)
    var2= Arg(1)

    WriteLineToFile(var1,var2)

    Any help is greatly appreciated. Thanks

Все ответы

  • 7 мая 2012 г. 20:06
     
     Отвечено

    found it

    remove the para arounds the function call

    WriteLineToFile var1,var2

    • Помечено в качестве ответа sic77 7 мая 2012 г. 20:06
    •  
  • 7 мая 2012 г. 20:06
    Модератор
     
     Предложенный ответ С кодом

    Hi,

    Try it this way:

    Function WriteLineToFile(ByVal Var1, ByVal Var2)
      Const ForReading = 1, ForWriting = 2
      Dim FSO, TS
      Set FSO = CreateObject("Scripting.FileSystemObject")
      Set TS = FSO.OpenTextFile("C:\test.txt", ForWriting, True)
      TS.WriteLine Var1
      TS.WriteLine Var2
      TS.Close()
      Set TS = FSO.OpenTextFile("C:\test.txt", ForReading)
      WriteLineToFile = TS.ReadAll()
      TS.Close()
    End Function
    
    Dim Args, Var1, Var2
    Set Args = WScript.Arguments
    Var1 = Args.Item(0)
    Var2 = Args.Item(1)
    
    WScript.Echo WriteLineToFile(Var1, Var2)
    

    The main changes I made were to close the TextStream object after writing the two lines and after the ReadAll() method.

    Bill