vbScript执行目录中的所有文件

发布于 2024-12-10 14:26:57 字数 1222 浏览 0 评论 0原文

我正在尝试编写一个 vbScript 来执行给定目录中的所有文件(主要是批处理文件)。

我尝试修改删除所有文件的脚本,但无法使其正常工作。

这是我所拥有的:

Option Explicit 
'=========================================================================== 
'  Scheduled Task - Visual Basic ActiveX Script 
'=========================================================================== 

Call ExecuteDirectory("c:\users\public\documents\schedule\daily") 


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
        Next 

        Set f1 = Nothing 
        Set fc = Nothing 
        Set f = Nothing 


    End If 
    Set fso = Nothing 
End Function

msgbox 行显示我要执行的完整路径和文件名,但运行行生成文件未找到错误。

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") 


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
        Next 

        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-12-17 14:26:57

变量 strFiles 不断构建一个文件列表,文件之间有换行符。例如,如果您的文件夹包含文件“test1.bat”和“test2.bat”,您最终将得到以下结果:

迭代 1
strFiles =

test1.bat

迭代 1
strFiles =

test1.bat
test2.bat

我认为这不是您想要做的。如果您只想按顺序运行每个脚本,您应该只传递单个脚本名称。

尝试将内部循环更改为:

    For Each f1 in fc  
        Dim fileToRun
        fileToRun = strPath2Folder & "\" & f1.Name
        WshShell.Run Chr(34) & fileToRun & Chr(34), 1, true 
    Next  

The variable strFiles continually builds up a list of files with line breaks in between. For example, if your folder contains the files "test1.bat" and "test2.bat", you will end up with this:

Iteration 1:
strFiles =

test1.bat

Iteration 1:
strFiles =

test1.bat
test2.bat

I don't think this is what you want to do. If you want to just run each script in order, you should just pass the single script name.

Try changing the inner loop to this:

    For Each f1 in fc  
        Dim fileToRun
        fileToRun = strPath2Folder & "\" & f1.Name
        WshShell.Run Chr(34) & fileToRun & Chr(34), 1, true 
    Next  
明月夜 2024-12-17 14:26:57

这是一种非常草率的做法。如果您需要一次执行整个目录的批处理文件,那么您就没有正确使用它们。您任何时候都应该只需要一个批处理文件或一个脚本。我会开始审视你的整个系统,寻找更好的方法来完成你想要完成的任务。

This is a very sloppy approach. If you are needing to execute an entire directory of batch files at one time, then you are not using them correctly. You should only need one batch file or one script an any time. I would begin looking at your whole system for a better approach to whatever it is that you are trying to accomplish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文