VBS - 从路径复制特定文件

时间:2018-04-17 13:51:59

标签: vbscript

我已经通过一些代码示例来获得所需的结果,但是,因为我目前正在使用下面的代码(略微修改)用于其他目的,我认为它也适用于此。

在第42行,我收到错误:

  

指定的网络名称不再可用

第42行:For Each objFile In objFolder.Files

' Require variables to be defined
Option Explicit

' Global variables
Dim strBaseFolder
Dim strDestFolder
Dim objFSO      
Dim objFolder
Dim objFile
Dim searchname1, searchname2, searchname3, searchname4, searchname5

' Define folders to work with
strBaseFolder = "X:\Source\Data"
strDestFolder = "X:\Test\Terminations"

searchname1 = "MyFile"
searchname2 = "YourFile"
searchname3 = "HerFile"
searchname4 = "HisFile"
searchname5 = "OurFile"

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Exit if base folder does not exist
If Not objFSO.FolderExists(strBaseFolder) Then
    Wscript.Echo "Missing base folder : """ & strBaseFolder & """"
    Wscript.Quit
End If

' Exit if dest folder does not exist
If Not objFSO.FolderExists(strDestFolder) Then
    Wscript.Echo "Missing dest folder : """ & strDestFolder & """"
    Wscript.Quit
End If

' Look at each subfolder of base folder
For Each objFolder In objFSO.GetFolder(strBaseFolder).SubFolders
    ' Continue if we want this folder

        ' Check each file in this folder
        For Each objFile In objFolder.Files
            ' Continue if we want this file
            If IncludeFile(objFile) Then
                ' Copy the file
                objFSO.copyFile objFile, strDestFolder & "\" & objFile.Name
            End If
        Next

Next

If Err.Number = 0 Then
    wscript.echo "Files Seccuessfully Copied"

Else If Err.Number <> 0 Then

    wscript.echo "An Error Occurred"

End If

End If

' Logic to determine if we process a file
Function IncludeFile(objFile)
    IncludeFile = False
    Select Case LCase(objFSO.GetExtensionName(objFile.Path))
        ' Include only these extensions
        Case "csv", "xls", "xlsx", "txt"
            ' Include only files dated today
            if instr(objFile, searchname1) = 1 or instr(objFile, searchname2) = 1  or instr(objFile, searchname3) = 1  or instr(objFile, searchname4) = 1 or instr(objFile, searchname5) = 1 then
                IncludeFile = True 
            end if
    End Select
End Function

我不明白这一点,因为我可以手动进入共享并复制文件,如果我运行不同的脚本访问相同的共享,那么它的效果很好..

2 个答案:

答案 0 :(得分:0)

我认为如果存在您无权阅读的隐藏或系统子文件夹,就会发生这种情况。通过从脚本中将文件夹的名称转储到控制台进行检查,查看它扼杀了哪个文件夹,然后查看是否可以从资源管理器中看到该文件夹​​。

答案 1 :(得分:0)

回答上面的问题:

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = FSO.GetFolder("X:\\Data\")
For Each objFile In objFolder.Files 
        fileName=objFile.name 
    If instr(fileName,"MyFile") Then
        FSO.copyfile objFile, "X:\Test\Other Files" & "\", True
    End If

    If instr(fileName,"YourFIle") Then
        FSO.copyfile objFile, "X:\Test\3rd Party" & "\", True
    End If

    If LCase(FSO.GetExtensionName(objFile.Name)) = "txt" Then

        If instr(fileName,"OurFile") Then
            FSO.copyfile objFile, "X:\Test\Other Files" & "\", True
        End If

        If instr(fileName,"TheirFile") Then
            FSO.copyfile objFile, "X:\Test\Other Files" & "\", True
        End If

        If instr(fileName,"someFile") Then
            FSO.copyfile objFile, "X:\Test\Other Files" & "\", True
        End If

    End If

Next

If Err.Number > 0 Then

    WScript.Echo "Error: " & Err.Description
    Err.Clear

  Else

  WScript.Echo "Other Files Copied!"

End If