Search code examples
windowspowershellautomationvbscript

excecuting vbscript(?) code: automatically open new files in a directory


im new to vbs-,Powershell-, ... coding

i need to build something that checks for new files in a specific directory and opens all newly added files automatically.

i've found this post which seems to fit my needs perfectly: https://devblogs.microsoft.com/scripting/how-can-i-automatically-open-new-files-added-to-a-folder/

I tweaked the code to my situation like this:

Set objShell = CreateObject(“Wscript.Shell”)

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    (“SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE ” _
        & “Targetinstance ISA ‘CIM_DirectoryContainsFile’ and ” _
            & “TargetInstance.GroupComponent= ” _
                & “‘Win32_Directory.Name=””T:\\\\Reporting\\\\AutoOpen””‘”)
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    strNewFile = objLatestEvent.TargetInstance.PartComponent
    arrNewFile = Split(strNewFile, “=”)
    strFileName = arrNewFile(1)
    strFileName = Replace(strFileName, “\\”, “\”)
    strFileName = Replace(strFileName, Chr(34), “”)
    objShell.Run(“excel.exe ” & strFileName)
Loop

Bummer that the above post contains no details on how to actually use the code or what programming language it is :D

I assumed this snipped is for vbscript. When i put it in a .vbs file and execute it, i get the error-message "invalid character, line 1, character 29, compiling error, ..." -> this character is the first quotation mark in line 1.

So i also tried powershell, but it seems like the code doesnt fit there either.

... So what kind of code is this after all? And how can i put it in an executable file?

Thank you so much in advance.


Solution

  • The slanted or smart quotes “.” you are using are invalid in vbscript code.

    You should replace all of them with regular double quotes "

    Applications such as MS Word tend to convert regular quotes with these slanted ones, so that might be where you got them from.