合并两个工作的.vbs文件

时间:2018-06-21 10:42:13

标签: vbscript

因此,我有两个工作的.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 

它们只是不能一起工作,所以我将如何将它们组合在一起?

更新:此代码出现this错误:

    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

1 个答案:

答案 0 :(得分:1)

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