【问题标题】:vba wscript.shell copy file from folder to another folder based on cell path or filenamevba wscript.shell 根据单元格路径或文件名将文件从文件夹复制到另一个文件夹
【发布时间】:2022-01-04 13:05:30
【问题描述】:

我想用 vba wscript.shell 来做,因为复制文件更快,我想根据“E”列中的选择在 excel 单元格中根据路径或文件名复制文件,并使用“msoFileDialogFolderPicker”输出目标文件夹

我有示例代码,但需要更改。



Sub copy()
xDFileDlg As FileDialog
xDPathStr As Variant
sn = Filter(Split(CreateObject("wscript.shell").exec("cmd /c dir C:\copy\*.* /b /s").stdout.readall, vbCrLf), "\")
'For j = 0 To UBound(sn)
'If DateDiff("d", FileDateTime(sn(j)), Date) > 30 Then sn(j) = ""
'Next

sn = Filter(sn, "\")

For j = 0 To UBound(sn)
FileCopy sn(j), "C:\destcopy" & Mid(sn(j), 2)
Next
 Set xDFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
 xDFileDlg.Title = "Please select the destination folder:"
 If xDFileDlg.Show <> -1 Then Exit Sub
 xDPathStr = xDFileDlg.SelectedItems.Item(1) & "\"
End Sub

谢谢

罗伊

【问题讨论】:

  • 您请求帮助修改代码,但您没有解释您想要完成的任务。我们是否应该仅根据您的(不工作的)代码和您向我们展示的图片进行推断?从上面的图片中可以使用什么?如何?做什么?您的代码中的哪些内容无法按您的意愿工作,您需要更改\追加?
  • @FaneDuru ,从上面将使用的 excel 图像中,我想使用 vba wscript.shell 根据列 A 中的路径复制文件,并且还基于标记为 E 列中的选择“V”。我发布的示例代码不起作用。并使用“msoFileDialogFolderPicker”输出文件夹。我希望修改或替换 vba 代码。有关信息,我的工作表名称是“master”
  • 您的实际代码中要保留哪些内容?您为什么要谈论“vba wscript.shell,因为复制文件更快”,因为您想使用 文件路径,而不是文件夹来提取其文件?
  • @FaneDuru ,如果您想更改实际代码没问题,我可以使用 vba wscript.shell 根据单元格中的引用路径或文件名复制文件吗?
  • @FaneDuru ,因为如果我使用“vba wscript.shell”它会更快,因为复制了数百个文件

标签: arrays vba wscript.shell filecopy


【解决方案1】:

请测试下一个代码。它假定您需要选择目标文件夹以复制那里的所有文件。否则,VBScript 对象节省的几毫秒对于浏览每个要复制的文件目标文件夹所需的秒数来说太少了。但是,如果这是你想要的,我可以轻松地修改代码来做到这一点:

Sub copyFiles()
  Dim sh As Worksheet, lastR As Long, arrA, i As Long, k As Long
  Dim fileD As FileDialog, strDestFold As String, FSO As Object
  
  Set sh = ActiveSheet
  lastR = sh.Range("A" & sh.rows.count).End(xlUp).row ' last row on A:A column
  arrA = sh.Range("A2:E" & lastR).Value2                   'place the range in an array for faster iteration
  Set FSO = CreateObject("Scripting.FileSystemObject")
  With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select the destination folder!"
        .AllowMultiSelect = False
        If .Show = -1 Then
            strDestFold = .SelectedItems.Item(1) & "\"   'select the destination folder
        End If
  End With
  If strDestFold = "" Then Exit Sub                         'in case of  not selecting any folder
  For i = 1 To UBound(arrA)
     If UCase(arrA(i, 5)) = "V" Then                         'copy the file only if a "V" exists in column E:E
        If FSO.FileExists(arrA(i, 1)) Then                    'check if the path in excel is correct
            FSO.CopyFile arrA(i, 1), strDestFold, True     'copy the file (True, to overwrite the file if it exists)
            k = k + 1
        Else
            MsgBox arrA(i, 1) & " file could not be found." & vbCrLf & _
                        "Please, check the spelling and correct the file full path!", vbInformation, _
                        "File does not exist..."
        End If
     End If
  Next i
  MsgBox "Copied " & k & " files in " & strDestFold, , "Ready..."
End Sub

【讨论】:

  • 我已经尝试了你的代码,并根据我想要的,但是没有基于 E 列的选择,所以我可以选择我想要复制的路径。
  • @roy 我错过了那部分...请测试更新的代码并发送一些反馈。
猜你喜欢
  • 2014-12-02
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多