首页 > 解决方案 > 结合两个工作 .vbs 文件

问题描述

所以我有两个工作 .vbs 脚本做两件不同的事情,一个删除文件中的只读属性,另一个删除所有带有“.v”扩展名的文件。将文件夹放到脚本上时,两者都可以工作。

我尝试将它们结合起来,但由于我的知识有限,我得到了一堆错误。

第一个代码:

   Option Explicit 
Sub main()

Dim ArgCount
Dim filExt
ArgCount = WScript.Arguments.Count
Select Case  ArgCount   
    Case 1 'Check the count of arguments
        Dim FSO,Path,File,Num_1,Num_2
        Set FSO = CreateObject("Scripting.FilesystemObject")
        Path = WScript.Arguments(0)
        If FSO.FileExists(Path) Then
            Set File = FSO.GetFile(path)
            If  (File.Attributes Mod 2) = 1 Then  'Check if the Read-Only is selected, and remove it.
                File.Attributes = File.Attributes-1 
                If Err.Number <> 0 Then 
                    MsgBox "Error :" & Path &" "& Err.Description
                Else 
                    MsgBox "Fjernelse fuldført"
                End If 

            Else 
                MsgBox "The Read-Only attribute of file is not selected"
            End If 
        Else 
            RemoveSubFolder Path,Num_1,Num_2 
            MsgBox Num_2 & " filer fuldført" & ", " & Num_1 & " filer fejlet"
        End If 
    Case Else 
        MsgBox "Træk mappen oven på denne fil"
End Select 
End Sub 

'This function is to remove the Read-Only of all files in a folder and its subfolder
Function RemoveSubFolder(FolderPath,Num_1,Num_2)

    Dim FSObject,Folder
    Dim subFolder,File
    Num_1 = 0
    Num_2 = 0
    Set FSObject = CreateObject("Scripting.FilesystemObject")
    Set Folder = FSObject.GetFolder(FolderPath) 
    For Each  subFolder In Folder.SubFolders 'Loop the subfolder in the folder
        FolderPath = subFolder.Path 
        RemoveSubFolder FolderPath,Num_1,Num_2
    Next 
    For Each  File In Folder.Files 'Remove the Read-Only attribute of files in the folder
        If  (File.Attributes Mod 2) = 1 Then 
            File.Attributes = File.Attributes-1 
            If Err.Number <> 0 Then 
                MsgBox  "Error :" & File.Path &" "& Err.Description
                Num_1 = Num_1 + 1
            Else 
                Num_2 = Num_2 + 1 
            End If 
        End If 
        Err.Clear 
    Next 




    Set FSObject = Nothing 


    End Function 

Call main 

第二个代码是这样的:

    Option Explicit

Dim FSObject, Folder, File, subFolder
Set FSObject = CreateObject("Scripting.FileSystemObject")

' Get the folder dropped onto our script...
    Folder = WScript.Arguments(0)

' Recursively check each file with the folder and its subfolders...
DoFolder Folder

Sub DoFolder(Folder)

    ' Check each file...
    For Each File In FSObject.GetFolder(Folder).Files
       If Right(File.name, 2) = ".v" Then
        FSObject.DeleteFile(Folder & "\" & File.name)
        End If
    Next

    ' Recursively check each subfolder...
    For Each subFolder In FSObject.GetFolder(Folder).SubFolders
        DoFolder subFolder.Path
    Next

End Sub

现在,我尝试将它们组合起来,但在第 34 行出现语法错误

     Option Explicit 
Sub main()

Dim ArgCount
Dim filExt
ArgCount = WScript.Arguments.Count
Select Case  ArgCount   
    Case 1 'Check the count of arguments
        Dim FSO,Path,File,Num_1,Num_2
        Set FSO = CreateObject("Scripting.FilesystemObject")
        Path = WScript.Arguments(0)
        If FSO.FileExists(Path) Then
            Set File = FSO.GetFile(path)
            If  (File.Attributes Mod 2) = 1 Then  'Check if the Read-Only is selected, and remove it.
                File.Attributes = File.Attributes-1 
                If Err.Number <> 0 Then 
                    MsgBox "Error :" & Path &" "& Err.Description
                Else 
                    MsgBox "Fjernelse fuldført"
                End If 

            Else 
                MsgBox "The Read-Only attribute of file is not selected"
            End If 
        Else 
            RemoveSubFolder Path,Num_1,Num_2 
            MsgBox Num_2 & " filer fuldført" & ", " & Num_1 & " filer fejlet"
        End If 
    Case Else 
        MsgBox "Træk mappen oven på denne fil"
End Select 

'This function is to remove the Read-Only of all files in a folder and its subfolder
Function RemoveSubFolder(FolderPath,Num_1,Num_2)

    Dim FSObject,Folder
    Dim subFolder,File
    Num_1 = 0
    Num_2 = 0
    Set FSObject = CreateObject("Scripting.FilesystemObject")
    Set Folder = FSObject.GetFolder(FolderPath) 
    For Each  subFolder In Folder.SubFolders 'Loop the subfolder in the folder
        FolderPath = subFolder.Path 
        RemoveSubFolder FolderPath,Num_1,Num_2
    Next 
    For Each  File In Folder.Files 'Remove the Read-Only attribute of files in the folder
        If  (File.Attributes Mod 2) = 1 Then 
            File.Attributes = File.Attributes-1 
            If Err.Number <> 0 Then 
                MsgBox  "Error :" & File.Path &" "& Err.Description
                Num_1 = Num_1 + 1
            Else 
                Num_2 = Num_2 + 1 
            End If 
        End If 
        Err.Clear 
    Next 

' Recursively check each file with the folder and its subfolders...
DoFolder Folder

Sub DoFolder(Folder)

    ' Check each file...
    For Each File In FSObject.GetFolder(Folder).Files
       If Right(File.name, 2) = ".v" Then
        FSObject.DeleteFile(Folder & "\" & File.name)
        End If
    Next

    ' Recursively check each subfolder...
    For Each subFolder In FSObject.GetFolder(Folder).SubFolders
        DoFolder subFolder.Path
    Next

End Sub


    Set FSObject = Nothing 


    End Function 

Call main 

它们只是不能一起工作,那么我将如何组合它们呢?

更新:我收到此代码的错误:

    Option Explicit 
Sub main()

Dim ArgCount
Dim filExt
ArgCount = WScript.Arguments.Count
Select Case  ArgCount   
    Case 1 'Check the count of arguments
        Dim FSO,Path,File,Num_1,Num_2
        Set FSO = CreateObject("Scripting.FilesystemObject")
        Path = WScript.Arguments(0)
        If FSO.FileExists(Path) Then
            Set File = FSO.GetFile(path)
            If  (File.Attributes Mod 2) = 1 Then  'Check if the Read-Only is selected, and remove it.
                File.Attributes = File.Attributes-1 
                If Err.Number <> 0 Then 
                    MsgBox "Error :" & Path &" "& Err.Description
                Else 
                    MsgBox "Fjernelse fuldført"
                End If 

            Else 
                MsgBox "The Read-Only attribute of file is not selected"
            End If 
        Else 
            RemoveSubFolder Path,Num_1,Num_2 
            MsgBox Num_2 & " filer fuldført" & ", " & Num_1 & " filer fejlet"
        End If 
    Case Else 
        MsgBox "Træk mappen oven på denne fil"
End Select 
End Sub
'This function is to remove the Read-Only of all files in a folder and its subfolder
Function RemoveSubFolder(FolderPath,Num_1,Num_2)

    Dim FSObject,Folder
    Dim subFolder,File
    Num_1 = 0
    Num_2 = 0
    Set FSObject = CreateObject("Scripting.FilesystemObject")
    Set Folder = FSObject.GetFolder(FolderPath) 
    For Each  subFolder In Folder.SubFolders 'Loop the subfolder in the folder
        FolderPath = subFolder.Path 
        RemoveSubFolder FolderPath,Num_1,Num_2
    Next 
    For Each  File In Folder.Files 'Remove the Read-Only attribute of files in the folder
        If  (File.Attributes Mod 2) = 1 Then 
            File.Attributes = File.Attributes-1 
            If Err.Number <> 0 Then 
                MsgBox  "Error :" & File.Path &" "& Err.Description
                Num_1 = Num_1 + 1
            Else 
                Num_2 = Num_2 + 1 
            End If 
        End If 
        Err.Clear 
    Next 

' Recursively check each file with the folder and its subfolders...
DoFolder Folder

Sub DoFolder(Folder)

    ' Check each file...
    For Each File In FSObject.GetFolder(Folder).Files
       If Right(File.name, 2) = ".v" Then
        FSObject.DeleteFile(Folder & "\" & File.name)
        End If
    Next

    ' Recursively check each subfolder...
    For Each subFolder In FSObject.GetFolder(Folder).SubFolders
        DoFolder subFolder.Path
    Next

End Sub


    Set FSObject = Nothing 


    End Function 

Call main

更新代码:

      Option Explicit 
Sub main()

Dim ArgCount
Dim filExt,Num_1,Num_2
ArgCount = WScript.Arguments.Count
Select Case  ArgCount   
    Case 1 'Check the count of arguments
        Dim FSO,Path,File,Num_1,Num_2
        Set FSO = CreateObject("Scripting.FilesystemObject")
        Path = WScript.Arguments(0)
        If FSO.FileExists(Path) Then
            Set File = FSO.GetFile(path)
            If  (File.Attributes Mod 2) = 1 Then  'Check if the Read-Only is selected, and remove it.
                File.Attributes = File.Attributes-1 
                If Err.Number <> 0 Then 
                    MsgBox "Error :" & Path &" "& Err.Description
                Else 
                    MsgBox "Fjernelse fuldført"
                End If 

            Else 
                MsgBox "The Read-Only attribute of file is not selected"
            End If 
        Else 
            RemoveSubFolder Path,Num_1,Num_2 
            MsgBox Num_2 & " filer fuldført" & ", " & Num_1 & " filer fejlet"
        End If 
    Case Else 
        MsgBox "Træk mappen oven på denne fil"
End Select 
End Sub
'This function is to remove the Read-Only of all files in a folder and its subfolder


    Dim FSObject,Folder
    Dim subFolder,File
    Num_1 = 0
    Num_2 = 0
    Set FSObject = CreateObject("Scripting.FilesystemObject")
    Set Folder = FSObject.GetFolder(FolderPath) 
    For Each  subFolder In Folder.SubFolders 'Loop the subfolder in the folder
        FolderPath = subFolder.Path 
        RemoveSubFolder FolderPath,Num_1,Num_2
    Next 
    For Each  File In Folder.Files 'Remove the Read-Only attribute of files in the folder
        If  (File.Attributes Mod 2) = 1 Then 
            File.Attributes = File.Attributes-1 
            If Err.Number <> 0 Then 
                MsgBox  "Error :" & File.Path &" "& Err.Description
                Num_1 = Num_1 + 1
            Else 
                Num_2 = Num_2 + 1 
            End If 
        End If 
        Err.Clear 
    Next 

' Recursively check each file with the folder and its subfolders...
DoFolder Folder

Sub DoFolder(Folder)

    ' Check each file...
    For Each File In FSObject.GetFolder(Folder).Files
       If Right(File.name, 2) = ".v" Then
        FSObject.DeleteFile(Folder & "\" & File.name)
        End If
    Next

    ' Recursively check each subfolder...
    For Each subFolder In FSObject.GetFolder(Folder).SubFolders
        DoFolder subFolder.Path
    Next

End Sub

Function RemoveSubFolder(FolderPath, Num_1, Num_2)
    Set FSObject = Nothing 


    End Function 

Call main 

我现在收到此错误:

https://i.imgur.com/TDfgvLI.png

编辑:删除第 9 行中的 Num_1 和 Num_2 定义后,我收到此错误:

https://i.imgur.com/uEfkGCh.png

标签: vbscript

解决方案


VBScript 不允许将过程或函数定义嵌套在其他过程或函数中。移动DoFolder函数外的定义RemoveSubFolder

Sub DoFolder(Folder)
    'Check each file...
    For Each File In FSObject.GetFolder(Folder).Files
        If Right(File.Name, 2) = ".v" Then
            FSObject.DeleteFile(Folder & "\" & File.Name)
        End If
    Next

    'Recursively check each subfolder...
    For Each subFolder In FSObject.GetFolder(Folder).SubFolders
        DoFolder subFolder.Path
    Next
End Sub

Function RemoveSubFolder(FolderPath, Num_1, Num_2)
    ...
End Function

推荐阅读