【问题标题】:Vbs won't execute after functionVbs不会在函数后执行
【发布时间】:2019-08-30 02:33:33
【问题描述】:

我正在设置一个我从网上获得的 vbs,将一些文件从一个文件夹复制到另一个文件夹。 代码中一切正常,除了“结束函数”之后。

脚本在第 73 行之后不再工作。睡眠代码以及 wscript.shell 此后不再执行。

有人可以帮忙吗。

Option Explicit

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""C:\Program Files (x86)\VideoLAN\VLC\vlc.exe""")

WScript.sleep 5000

Dim ws
Set ws=CreateObject("WScript.Shell")
ws.Run "TASKKILL.exe /F /IM vlc.exe"

WScript.sleep 8500


Dim srcFolder, trgFolder,WshShell,UserProfilePath
Set WshShell = CreateObject("wscript.Shell")
UserProfilePath = WshShell.ExpandEnvironmentStrings("%UserProfile%")
srcFolder = "C:\Test\"
trgFolder = UserProfilePath & "\AppData\Roaming\"

CopyFilesAndFolders srcFolder, trgFolder
WScript.Quit

Sub CopyFilesAndFolders (ByVal strSource, ByVal strDestination)
Dim ObjFSO, ObjFolder, ObjSubFolder, ObjFile, files
Dim TargetPath
Set ObjFSO = CreateObject("scripting.filesystemobject")
'connecting to the folder where is going to be searched
Set ObjFolder = ObjFSO.GetFolder(strSource)
TargetPath = Replace (objFolder.path & "\", strSource, strDestination,1,-1,vbTextCompare)
If Not ObjFSO.FolderExists (TargetPath) Then ObjFSO.CreateFolder (TargetPath)
Err.clear
On Error Resume Next
'Check all files in a folder
For Each objFile In ObjFolder.files
    If Err.Number <> 0 Then Exit For 'If no permission or no files in folder
    On Error goto 0
    If CheckToCopyFile (objFile.path, TargetPath & "\" & objFile.name) Then 
        objFSO.copyfile objFile.path, TargetPath & "\" & objFile.name, True
    End If
Next
'Recurse through all of the subfolders
On Error Resume Next
Err.clear
For Each objSubFolder In ObjFolder.subFolders
    If Err.Number <> 0 Then Exit For 'If no permission or no subfolder in folder
    On Error goto 0
    'For each found subfolder there will be searched for files
    CopyFilesAndFolders ObjSubFolder.Path & "\", TargetPath & ObjSubFolder.name & "\"
Next
Set ObjFile = Nothing
Set ObjSubFolder = Nothing
Set ObjFolder = Nothing
Set ObjFSO = Nothing
End Sub

Sub CopyFilesAndFolders (ByVal strSource, ByVal strDestination)
    Dim ObjFSO, ObjFolder, ObjSubFolder, ObjFile, files
    Dim TargetPath
    Set ObjFSO = CreateObject("scripting.filesystemobject")
    'connecting to the folder where is going to be searched
    Set ObjFolder = ObjFSO.GetFolder(strSource)
    TargetPath = Replace (objFolder.path & "\", strSource, strDestination,1,-1,vbTextCompare)
    If Not ObjFSO.FolderExists (TargetPath) Then ObjFSO.CreateFolder (TargetPath)
    Err.clear
    On Error Resume Next
    'Check all files in a folder
    For Each objFile In ObjFolder.files
        If Err.Number <> 0 Then Exit For 'If no permission or no files in folder
        On Error goto 0
        If CheckToCopyFile (objFile.path, TargetPath & "\" & objFile.name) Then 
            objFSO.copyfile objFile.path, TargetPath & "\" & objFile.name, True
        End If
    Next
    'Recurse through all of the subfolders
    On Error Resume Next
    Err.clear
    For Each objSubFolder In ObjFolder.subFolders
        If Err.Number <> 0 Then Exit For 'If no permission or no subfolder in folder
        On Error goto 0
        'For each found subfolder there will be searched for files
        CopyFilesAndFolders ObjSubFolder.Path & "\", TargetPath & ObjSubFolder.name & "\"
    Next
    Set ObjFile = Nothing
    Set ObjSubFolder = Nothing
    Set ObjFolder = Nothing
    Set ObjFSO = Nothing
End Sub

Function CheckToCopyFile (ByVal strSourceFilePath, ByVal strDestFilePath)
    Dim oFSO, oFile, SourceFileModTime, DestFileModTime
    CheckToCopyFile = True
    Set oFSO = CreateObject("scripting.filesystemobject")
    If Not oFSO.FileExists (strDestFilePath) Then Exit Function
    Set oFile = oFSO.GetFile (strSourceFilePath)
    SourceFileModTime = oFile.DateLastModified
    Set oFile = Nothing
    Set oFile = oFSO.GetFile (strDestFilePath)
    DestFileModTime = oFile.DateLastModified
    Set oFile = Nothing
    If SourceFileModTime =< DestFileModTime Then CheckToCopyFile = False
    Set oFSO = Nothing
    End Function

WScript.sleep 8000

Dim objShell1
Set objShell1 = WScript.CreateObject( "WScript.Shell" )
objShell1.Run("""C:\Program Files (x86)\VideoLAN\VLC\vlc.exe""")

【问题讨论】:

  • 编辑您的帖子并使用标签进行代码格式化。
  • 嗨,Tom.Necessary 已经完成。
  • 您的脚本从不调用 sub 或函数。它只是休眠 8 秒,打开一个 shell,然后启动 vlc。还有比这更多的吗?您如何确定它不在该行之后运行?有错误吗?意外行为?您也可以在该行发表评论,以便我们确定您在说什么。
  • 嗨 JNevill,对不起,我没有放我的完整代码。我已经更新了我的帖子。实际上第 73 行之后没有错误。代码运行但 vlc.exe 无法打开。

标签: vbscript


【解决方案1】:

在第 23 行你有WScript.Quit。 您在开始时执行所有操作,taskkill,复制文件和文件夹,然后关闭脚本。如果要运行 VLC,请将 objShell1.Run("""C:\Program Files (x86)\VideoLAN\VLC\vlc.exe""") 放在第 23 行之前。 比如:

Dim srcFolder, trgFolder,WshShell,UserProfilePath
Set WshShell = CreateObject("wscript.Shell")
UserProfilePath = WshShell.ExpandEnvironmentStrings("%UserProfile%")
srcFolder = "C:\Test\"
trgFolder = UserProfilePath & "\AppData\Roaming\"

CopyFilesAndFolders srcFolder, trgFolder

WScript.sleep 8000

Dim objShell1
Set objShell1 = WScript.CreateObject( "WScript.Shell" )
objShell1.Run("""C:\Program Files (x86)\VideoLAN\VLC\vlc.exe""")
WScript.Quit 

【讨论】:

  • 嗨,Alex Marin,感谢您的代码。它现在可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
相关资源
最近更新 更多