AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Embedded Windows Scripting (VBScript & JScript) and COM
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
pokercurious



Joined: 16 Dec 2007
Posts: 38

PostPosted: Wed May 28, 2008 12:49 am    Post subject: Reply with quote

tank wrote:
I am assuming this is directed at me
yes that is correct
copy paste error


Actually, at rad777 who wrote the initial question - sometimes the obvious answers are the ones that are overlooked.
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 679

PostPosted: Wed May 28, 2008 12:51 am    Post subject: Reply with quote

Very Happy NO PROBLEM HOPE I HELPED
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Wed May 28, 2008 3:19 am    Post subject: Reply with quote

amazing...thank god I found this script. This will make my work life easier I believe.

Anyway, can anyone translate this code for me (vbscript):
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\TestListener""'")

I have been trying to catch my tail with this code...I must have removed /replaced every quote symbol and still can't get it to work.

The line is taken from a wroking vbscript file.


Thanks.
Code:
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 679

PostPosted: Wed May 28, 2008 3:29 am    Post subject: Reply with quote

Your post went wrong the code doesnt show
at any rate
if you simply paste the working vbscript into a multiline variable there isnt a need top escape the quotes just use as is

Code:

mycode=
(
this is
a
multi

line variable

)
Ws_Exec(mycode)



i could there for do this

Code:

mycode=
(
msgbox "this was single quoted just like it would have been in vbs"
)
Ws_Exec(mycode)


but if i do this then i must double quote
Code:

Ws_Exec("msgbox ""this was single quoted just like it would have been in vbs""")

untested but you get the idea
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Wed May 28, 2008 4:52 am    Post subject: Reply with quote

Thanks tank...I thought I had a handle on this project (just stumbled on it tonight) but I still get an error on this:
Code:

WS_Initialize("VBScript", "C:\Windows\system32\msscript.ocx")

    objTcp  := WS_CreateObject ("ActiveXperts.Tcp")
    objConstants := WS_CreateObject ("ActiveXperts.ASConstants")

    WS_Exec("objTcp.Protocol := objConstants.asSOCKET_PROTOCOL_RAW")

    WS_Exec("objTcp.Connect 'servername', 6008")

    WS_Exec( "strComputer := '.'")
    WS_Exec("Set objWMIService := GetObject('winmgmts:\\' & strComputer & '\root\cimv2')")
    mycode=
    {
    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\TestListener""'")
    }
   WS_Exec(mycode)


It says missing ")"...from the above I see no idea why.
If I comment out the problem code then it runs (obviously without what I need Smile)

Thanks for the help.
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 679

PostPosted: Wed May 28, 2008 5:33 am    Post subject: Reply with quote

IN2ITive wrote:
Thanks tank...I thought I had a handle on this project (just stumbled on it tonight) but I still get an error on this:
Code:

WS_Initialize("VBScript", "C:\Windows\system32\msscript.ocx")

    objTcp  := WS_CreateObject ("ActiveXperts.Tcp")
    objConstants := WS_CreateObject ("ActiveXperts.ASConstants")

    WS_Exec("objTcp.Protocol := objConstants.asSOCKET_PROTOCOL_RAW")

    WS_Exec("objTcp.Connect 'servername', 6008")

    WS_Exec( "strComputer := '.'")
    WS_Exec("Set objWMIService := GetObject('winmgmts:\\' & strComputer & '\root\cimv2')")
    mycode=
    {
    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\TestListener""'")
    }
   WS_Exec(mycode)


It says missing ")"...from the above I see no idea why.
If I comment out the problem code then it runs (obviously without what I need Smile)

Thanks for the help.

seems the problem might be
Code:


    mycode=
    ( ' not {
    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "Win32_Directory.Name='c:\\\\TestListener'") 'took out what seem to be un needed double quotes
    ) 'not }


but its been a while since i bothered with sql in vbs

does this script run as a standalone VBS file?
if so it should be pasted into the variable as is
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Wed May 28, 2008 5:47 am    Post subject: Reply with quote

Yes, it does run as a standalone script; the main reason I wanted to use this scripting capability is because it would allow me to post some stats to a GUI.

It works really well as a standalone. I will try again at work tomorrow and see what I get. Will post if the error still exists, which I suspect it will.

Thanks again for your help Tank.
Back to top
View user's profile Send private message
erictheturtle



Joined: 27 Jun 2007
Posts: 58
Location: California

PostPosted: Wed May 28, 2008 3:37 pm    Post subject: Reply with quote

IN2ITive wrote:
It says missing ")"...from the above
I didn't see it at first but one of your squiggly brackets '}' needed to be a parenthesis ')'

I can't fully test this, but it runs and I think should be very close to what you are looking for.
Code:
WS_Initialize("VBScript")

initcode =
(
    Set objTcp = CreateObject("ActiveXperts.Tcp")
    Set objConstants = CreateObject("ActiveXperts.ASConstants")
    objTcp.Protocol = objConstants.asSOCKET_PROTOCOL_RAW
    objTcp.Connect "servername", 6008
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
)
ok := WS_Exec(initcode)
If (!ok)
    Msgbox Error:`n%ErrorLevel%

mycode=
(
    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
        & "TargetInstance.GroupComponent= " _
        & "'Win32_Directory.Name=""c:\\\\TestListener""'")
)
ok := WS_Exec(mycode)
If (!ok)
    Msgbox Error:`n%ErrorLevel%

_________________
-m35
Back to top
View user's profile Send private message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Wed May 28, 2008 5:03 pm    Post subject: Reply with quote

Thanks for that Eric. Unfortunately, the autohotkey.net site is blocked here at work (don't ask why, especially since the .com site is open) and I don't have a copy of the ws4ahk script.
So either:
A) I test when I get home or
B) Maybe someone could copy and paste the latest version in this thread? Smile

Either way, thanks for the amazing utility you have provided us users and for constantly providing help in this thread.
Back to top
View user's profile Send private message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Wed May 28, 2008 6:16 pm    Post subject: Reply with quote

OK, I was able to get the include file and Eric's solution was correct.

So, now I have a folderlistener that will send any new files in the folder to a server for processing. Note...just posting for informational purposes but if you were to use this script you would need to have the ActiveExperts socket dll first.

Now to get it displaying some stuff in a GUI...which I have never worked with. Smile

Amazed at I now have the ability to have vbscript code with a GUI! Thanks for all your hard work on this project Eric!

Code:

WS_Initialize("VBScript")

    initcode =
(
    Set objTcp = CreateObject("ActiveXperts.Tcp")
    Set objConstants = CreateObject("ActiveXperts.ASConstants")
    objTcp.Protocol = objConstants.asSOCKET_PROTOCOL_RAW
    objTcp.Connect "servername", port
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
)
ok := WS_Exec(initcode)
If (!ok)
    Msgbox Error:`n%ErrorLevel%
     
   
   
    mycode=
   (
       Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
       ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
       & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
       & "TargetInstance.GroupComponent= " _
       & "'Win32_Directory.Name=""c:\\\\TestListener""'")
       
       
       Do While TRUE
          Set objEventObject = colMonitoredEvents.NextEvent()
       
          Select Case objEventObject.Path_.Class
                 Case "__InstanceCreationEvent"
                      Set objFSO = CreateObject("Scripting.FileSystemObject")
                      strReturned = objEventObject.TargetInstance.PartComponent
                      CurrentItem = Split(strReturned, "CIM_DataFile.Name=")(1)
                      CurrentItem = Replace(CurrentItem, """", "")
                      CurrentItem = Replace(CurrentItem, "\\", "\")
                      Set filetxt = objFSO.OpenTextFile(CStr(CurrentItem), 1, False)
                      contents = filetxt.ReadAll
                      filetxt.Close
                      contents = Chr(11) & contents & Chr(28) & Chr(13)
   
                      For i = 1 To Len(contents)
                          objTcp.SendByte Asc(mid(contents, i, 1))
                      Next
          End Select           

       Loop
   )
   ok := WS_Exec(mycode)
   If (!ok)
      Msgbox Error:`n%ErrorLevel%

   WS_Exec("objTcp.Disconnect")

WS_Uninitialize()
Back to top
View user's profile Send private message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Wed May 28, 2008 8:21 pm    Post subject: Reply with quote

Doh!

I spoke to soon. The data is not being sent to the server, mainly because objTCP doesn't have scope in the second vbscript code segment. Is there a way to pass a value (or in this case an object reference) to another vbcode section?

Code:

WS_Initialize("VBScript")
    initcode=
(
    Set objTcp = CreateObject("ActiveXperts.Tcp")  --->  I NEED objTCP in the "mycode" vbcode section
    Set objConstants = CreateObject("ActiveXperts.ASConstants")
    objTcp.Protocol = objConstants.asSOCKET_PROTOCOL_RAW
    objTcp.Connect "servername", 6008
)
ok := WS_Exec(initcode)
     
    mycode=
   (
       strComputer = "."
       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
       Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
       ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
       & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
       & "TargetInstance.GroupComponent= " _
       & "'Win32_Directory.Name=""c:\\\\TestListener""'")
       
       Do While TRUE
          Set objEventObject = colMonitoredEvents.NextEvent()
         
          Select Case objEventObject.Path_.Class
                 Case "__InstanceCreationEvent"
                      Set objFSO = CreateObject("Scripting.FileSystemObject")
                      strReturned = objEventObject.TargetInstance.PartComponent
                      CurrentItem = Split(strReturned, "CIM_DataFile.Name=")(1)
                      CurrentItem = Replace(CurrentItem, """", "")
                      CurrentItem = Replace(CurrentItem, "\\", "\")
                      Set filetxt = objFSO.OpenTextFile(CStr(CurrentItem), 1, False)
                      contents = filetxt.ReadAll
                      filetxt.Close
                      contents = Chr(11) & contents & Chr(28) & Chr(13)
                     
                      For i = 1 To Len(contents)
                          objTcp.SendByte Asc(mid(contents, i, 1)) --> NO REFERENCE to objTCP WHEN CODE GETS HERE
                      Next
                     
          End Select 

       Loop
   )
Back to top
View user's profile Send private message
erictheturtle



Joined: 27 Jun 2007
Posts: 58
Location: California

PostPosted: Wed May 28, 2008 9:13 pm    Post subject: Reply with quote

Scoping shouldn't be a problem. All variables in the global script space are persistent and visible in every WS_Eval/WS_Exec block until you call WS_Uninitialize(). So objTcp should still be valid in the second block.

Edit:
Something just occurred to me. Your code suggests that you want this script to run forever and ever in that loop. There's two problems with this: (1) I don't think AHK will respond to anything while that loop is running, and (2) by default the Windows Scripting control only allows scripts to run for 5 seconds before considering it 'hung' and kills the script execution (but this can be changed if needed).
_________________
-m35
Back to top
View user's profile Send private message
Guest222
Guest





PostPosted: Thu May 29, 2008 1:28 am    Post subject: Reply with quote

..
Back to top
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Thu May 29, 2008 2:44 am    Post subject: Reply with quote

I think you are right...this runs with no errors but almost immediately exits...

Code:

;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         A.N.Other <myemail@nowhere.com>
;
; Script Function:
;   Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;
#INCLUDE WindowsScript4AHK.ahk
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

IniRead, ListenFolder, C:\Documents and Settings\xxx\Desktop\Auto_Scripts\HL7_Auto_Route.ini, Setup, Listen_To_Folder

Gui, Font, S10 CDefault, Verdana
Gui, Add, Text, x16 y17 w120 h20 , Listening Folder:
Gui, Add, Edit, x186 y17 w900 h30 +ReadOnly vListenFolder, % ListenFolder
Gui, Add, GroupBox, x6 y57 w1090 h270 , New Files Detected:
Gui, Add, Text, x16 y277 w120 h30 , New Files Count:
Gui, Add, Edit, x186 y277 w110 h30 ,
Gui, Add, GroupBox, x6 y337 w1090 h470 , New Files Content:
Gui, Add, ListView, x16 y87 w1070 h180 +Grid, Time|File/Folder Name|Size (KB)
Gui, Add, Button, x505 y817 w100 h30 , Close
Gui, Add, Edit, x16 y367 w1070 h380 ,
Gui, Add, Text, x16 y757 w170 h30 , Total KB Sent To Server:
Gui, Add, Edit, x196 y757 w130 h30 ,
; Generated using SmartGUI Creator 4.0
Gui, Show, x131 y91 h861 w1113, HL7 Auto Route
;Return

WS_Initialize("VBScript")

       WS_EXEC("Set objTcp = CreateObject('ActiveXperts.Tcp')")
       WS_EXEC("Set objConstants = CreateObject('ActiveXperts.ASConstants')")
       WS_EXEC("objTcp.Protocol := objConstants.asSOCKET_PROTOCOL_RAW")
       WS_EXEC("objTcp.Connect, 'servername', 6008")
       strComputer := "."
       WS_EXEC("Set objWMIService = GetObject('winmgmts:\\'' & strComputer & '\root\cimv2')")
       WS_EXEC("Set colMonitoredEvents := objWMIService.ExecNotificationQuery('SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE ' & 'Targetinstance ISA 'CIM_DirectoryContainsFile' and ' & 'TargetInstance.GroupComponent= ' & ''Win32_Directory.Name=''c:\\\\TestListener'''')")
       
       WS_EXEC("Do While TRUE")
          WS_EXEC("Set objEventObject := colMonitoredEvents.NextEvent()")
         
          WS_EXEC("Select Case objEventObject.Path_.Class")
                 WS_EXEC("Case '__InstanceCreationEvent'")
                      WS_EXEC("Set objFSO = CreateObject('Scripting.FileSystemObject')")
                      WS_EXEC("strReturned := objEventObject.TargetInstance.PartComponent")
                      WS_EXEC("CurrentItem = Split(strReturned, 'CIM_DataFile.Name=')(1)")
                      WS_EXEC("CurrentItem = Replace(CurrentItem, '''', '')")
                      WS_EXEC("CurrentItem = Replace(CurrentItem, '\\', '\'')")
                      WS_EXEC("Set filetxt := objFSO.OpenTextFile(CStr(CurrentItem), 1, False)")
                      WS_EXEC("contents := filetxt.ReadAll")
                      WS_EXEC("filetxt.Close, ")
                      WS_EXEC("contents = Chr(11) & contents & Chr(28) & Chr(13)")
                     
                      WS_EXEC("For i = 1 To Len(contents)")
                          WS_EXEC("objTcp.SendByte, 'Asc(mid(contents, i, 1))'")
                      WS_EXEC("Next")
                     
          WS_EXEC("End Select") 

       WS_EXEC("Loop")

   WS_Exec("objTcp.Disconnect, ")
   
WS_Uninitialize() 

GuiClose:
ExitApp
Back to top
View user's profile Send private message
erictheturtle



Joined: 27 Jun 2007
Posts: 58
Location: California

PostPosted: Thu May 29, 2008 2:57 am    Post subject: Reply with quote

Umm...splitting up every VBScript line into a separate WS_Exec() call will guarantee it won't work correctly. Smile

Many of those lines will simply cause an error.
Code:
WS_EXEC("Do While TRUE") ; Error: 'Do without Loop'
Code:
WS_EXEC("Select Case objEventObject.Path_.Class") ; Error: 'Select Case without End Select
etc...

You have to combine the loops, select cases, and other code blocks into one WS_Exec call.

me wrote:
Something just occurred to me. Your code suggests that you want this script to run forever and ever in that loop. There's two problems with this: (1) I don't think AHK will respond to anything while that loop is running, and (2) by default the Windows Scripting control only allows scripts to run for 5 seconds before considering it 'hung' and kills the script execution (but this can be changed if needed).

The solution to this problem it to setup an AHK timer event. I know that doesn't really explain anything, but I hope it sends you in the right direction. Smile
_________________
-m35
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next
Page 6 of 8

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group