none
VBS script doesn't work on Win Server 2008 R2 - Getting Error Code 80070002 RRS feed

  • Question

  • I have a script that looks for new files in a directory and runs a command based program that sends them as a DICOM transfer (medical stuff).  This script worked fine in a Windows Server 2003 R2 environment, but when I copied it over to Win 2008 Server R2, I am getting error 

    Script:

    C:\Users\Rsomerville\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\4DM-storescu-Script .vbs

    Line: 21

    Char: 5

    Error: The system cannot find the file specified.

    Code: 80070002

    Source: (null)

    Here is the script:

    Set objShell = CreateObject("Wscript.Shell")

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
        ("SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE " _
            & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
                & "TargetInstance.GroupComponent= " _
                    & "'Win32_Directory.Name=""d:\\\\4dmdropbox""'")

    Do
        Set objLatestEvent = colMonitoredEvents.NextEvent
        strNewFile = objLatestEvent.TargetInstance.PartComponent
        arrNewFile = Split(strNewFile, "=")
        strFileName = arrNewFile(1)
        strFileName = Replace(strFileName, "\\", "\")
        strFileName = Replace(strFileName, Chr(34), "")
        WScript.Sleep(20000)
        objShell.Run("storescu 172.27.157.100 104 " & strFileName & "")
    Loop

     I did create a directory D:\4DMDropbox

    And the script file resides in the same directory as the storescu.exe program

    Wednesday, December 10, 2014 8:19 PM

Answers

  • Scripts can run either with wscript.exe or cscript.exe.

    Before going much further you should really nail down the line that generates your error. You made a vague statement when asked for it yet it is basic to find out for sure. Use notepad.exe to open the script file, then make sure that Format/Word wrap is turned off. Now activate the Status bar via the View menu. Move down the code until the Status bar at the bottom reads "Ln21". What is this line?

    If it is this line:

    objShell.Run("storescu 172.27.157.100 104 " & strFileName & "")

    then its author qualifies for having his fingers chopped off. For robust scripts it is mandatory to fully qualify file names. He should have coded this line like so:

    objShell.Run("c:\My Folder\storescu.exe 172.27.157.100 104 " & strFileName & "")

    i.e. specify the folder where storescu.exe resides.

    • Marked as answer by Rsome2014 Thursday, December 11, 2014 12:16 AM
    Wednesday, December 10, 2014 11:39 PM

All replies

  • 1) You need to ask a specific question.

    2) You need to specify exactly which line is line 21 so respondents don't have to guess.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, December 10, 2014 8:22 PM
    Moderator
  • Sorry Bill, I'm new to scripting and this forum.  

    1) The Question: Why am I getting this error and how do I fix it?  Is there a different syntax or command that is needed since I am now in a different OS that is 64 bit?

    2) Re Line Number:  I don't really know, but if I'm counting the lines visually then this is line 21:

    objShell.Run("storescu 172.27.157.100 104 " & strFileName & "")

    Wednesday, December 10, 2014 8:39 PM
  • It is amazing that that event code ever worked as it breaks most of the rules.  Event code can be more easily built with PowerShell.  In WMF 4 we can even persist the event traps.

    To use WMI to trap on a file successfully we need to use slightly different structures or the trap will not give us correct data. We also want the file name and not the association whchi may not have the filename in the correct format.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, December 10, 2014 8:59 PM
    Wednesday, December 10, 2014 8:57 PM
  • 2) Re Line Number:  I don't really know, but if I'm counting the lines visually then this is line 21: objShell.Run("storescu 172.27.157.100 104 " & strFileName & "")

    Your "file not found" error means that your script can't find the command "storescu".

    If you're not familiar with scripting, then I recommend starting with the basics:

    http://technet.microsoft.com/en-us/scriptcenter/dd772284.aspx


    -- Bill Stewart [Bill_Stewart]

    Wednesday, December 10, 2014 9:05 PM
    Moderator
  • I certainly will, but in the meantime I would appreciate some layman's help.  I had copied this code from some website that I don't remember anymore, and it has worked fine for many years.  

    New development though: I tried running the script by "open with Command Prompt" and it worked fine.  Although I noticed that there is no wscript.exe in the Task Manager and I do have a command window that is staying open.

    Why does it work this way and not when running it from the shortcut to the .vbs file I placed in the Startup directory?  

    Wednesday, December 10, 2014 11:07 PM
  • Scripts can run either with wscript.exe or cscript.exe.

    Before going much further you should really nail down the line that generates your error. You made a vague statement when asked for it yet it is basic to find out for sure. Use notepad.exe to open the script file, then make sure that Format/Word wrap is turned off. Now activate the Status bar via the View menu. Move down the code until the Status bar at the bottom reads "Ln21". What is this line?

    If it is this line:

    objShell.Run("storescu 172.27.157.100 104 " & strFileName & "")

    then its author qualifies for having his fingers chopped off. For robust scripts it is mandatory to fully qualify file names. He should have coded this line like so:

    objShell.Run("c:\My Folder\storescu.exe 172.27.157.100 104 " & strFileName & "")

    i.e. specify the folder where storescu.exe resides.

    • Marked as answer by Rsome2014 Thursday, December 11, 2014 12:16 AM
    Wednesday, December 10, 2014 11:39 PM
  • Trick:

    Replace this:

     objShell.Run("storescu 172.27.157.100 104 " & strFileName & "")

    With this:

     WScript.Echo "objShell.Run(storescu 172.27.157.100 104 " & strFileName

    Look at the lines output.

    Go to the folder where this is run from and just type: storescu

    What happens?

    One action or the other will tell you your problem.


    ¯\_(ツ)_/¯

    Thursday, December 11, 2014 12:00 AM
  • If it is this line:

    objShell.Run("storescu 172.27.157.100 104 " & strFileName & "")

    then its author qualifies for having his fingers chopped off. For robust scripts it is mandatory to fully qualify file names. He should have coded this line like so:

    objShell.Run("c:\My Folder\storescu.exe 172.27.157.100 104 " & strFileName & "")

    Well get the hatchet ready because that was it!!  Thanks for the simple fix!  Works perfectly

    Thursday, December 11, 2014 12:18 AM
  • Here is a version that is more likely to work on all systems.

    Set objShell = CreateObject("Wscript.Shell")
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    
    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
        ("SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE " _
            & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
                & "TargetInstance.GroupComponent= " _
                    & "'Win32_Directory.Name=""c:\\\\4dmdropbox""'")
    
    Do
        Set evt = colMonitoredEvents.NextEvent
        Set f = GetObject("winmgmts:" & evt.TargetInstance.PartComponent)
        WScript.Echo "Processing file:" & f.Filename
        cmd = "c:\folder\storescu 172.27.157.100 104 " & f.Filename
        objShell.Run cmd,1,True
    Loop
     
    It uses the Run command correctly, validates the file names to the console and uses a path to the executable.

    I have run it.  It works minus the "storescu" which I donj't have.


    ¯\_(ツ)_/¯

    Thursday, December 11, 2014 12:33 AM