复制受损文件时如何避免“权限被拒绝”?

时间:2015-05-13 22:00:01

标签: vbscript

我写了一个VBScript来将文件从E盘复制到C盘。 E驱动器中有许多系统文件和损坏的文件,因此在复制这些文件时,脚本将停止。脚本运行时传递或跳过这些文件的任何方法?

代码是将所有文件夹从E盘复制到C盘

Const hd = "E:\"
Const cd = "C:\"

Dim path



Sub GenPath()
path = cd 

End Sub

Sub GenFolder()
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateFolder path
 Set objFso = Nothing

End Sub

Set fso=WScript.CreateObject("scripting.filesystemobject")
Set fs=fso.GetFolder("E:\")
Set f=fs.SubFolders
For Each uu In f

            Set Ws = WScript.CreateObject("Scripting.filesystemobject")
            Ws.CopyFolder  uu,path & "\"

1 个答案:

答案 0 :(得分:0)

For Each uu In f

       Set Ws = WScript.CreateObject("Scripting.filesystemobject")
       Ws.CopyFolder hd & uu,path1
   End If 


Next    

变为     设置Ws = WScript.CreateObject(" Scripting.filesystemobject")

On Error Resume Next
For Each uu In f

       Ws.CopyFolder uu.path, path1
       If err.number <> 0 then err.clear

Next    

另外,由于未知原因,您有End If

这是基础知识,但您可以使用它来重新创建文件夹结构(这会将所有文件转储到一个文件夹中)。

On error resume next
Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFolder("C:\Users\David Candy\Desktop")
fso.createfolder("C:\Users\David Candy\test123")
Folder2 = "C:\Users\David Candy\test123"
For Each thing in f.subfolders
        msgbox thing.path
        If err.number <> 0 then 
            msgbox err.description
            err.clear
        End If
    For Each thingy in thing.files
        msgbox thingy.path
        thingy.copy(Folder2 & "\" & thingy.name)
        If err.number <> 0 then 
            msgbox err.description
            err.clear
        End If
    Next
Next

仅在另一行上添加了一行和一个编辑来重新创建文件结构。

On error resume next
Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFolder("C:\Users\David Candy\Desktop")
fso.createfolder("C:\Users\David Candy\test123")
Folder2 = "C:\Users\David Candy\test123"
Set log = fso.CreateTextFile("c:\logfile.txt") 
For Each thing in f.subfolders
    fso.createfolder(folder2 & "\" & thing.name)
    If err.number <> 0 then 
        log.writeline thing.path & err.description
        err.clear
    End If
    For Each thingy in thing.files
        thingy.copy(Folder2 & "\" & thing.name & "\" & thingy.name)
        If err.number <> 0 then 
            log.writeline thingy.path & err.description 
            err.clear
        End If
    Next
Next