Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 😀

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading