I'm trying to write a vbScript that will execute all files in a given directory (will be mostly batch files).

I've tried to modify a script that deletes all files but I'm not able to get it to work.

Here is what I have:

Option Explicit 
'  Scheduled Task - Visual Basic ActiveX Script 
Call ExecuteDirectory("c:\users\public\documents\schedule\daily") 
'  Name    : ExecuteDirectory() 
'  Desc    : Executes Files from a Directory 
Function ExecuteDirectory(strPath2Folder) 
    Dim fso, f, fc, f1, strFiles, intFiles
	Dim WshShell

    Set WshShell = CreateObject("WScript.Shell")
    strFiles = "" 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    If (fso.FolderExists(strPath2Folder)) Then 
        Set f = fso.GetFolder(strPath2Folder) 
        Set fc = f.Files 
        '-- Execute each file in Folder 
        For Each f1 in fc 
            strFiles = strFiles & f1.Name & vbCrLf 
msgbox strPath2Folder & "\" & strFiles
            WshShell.Run Chr(34) & strFiles & Chr(34), 1, true
        Set f1 = Nothing 
        Set fc = Nothing 
        Set f = Nothing 
    End If 
    Set fso = Nothing 
End Function

The msgbox line displays the full path and file name that I want to execute, but the run line generates file not found error.

Why are you putting a vbcrlf in the file name? Unless vbcrlf character happens to be part of the file name, don't include it as part of the file name.

MessageBox will not show you the vbcrlf character: it's invisible to the human eye.

That was it.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.