gpt4 book ai didi

excel - 将文件从文件路径复制到另一个

转载 作者:行者123 更新时间:2023-12-04 22:31:01 24 4
gpt4 key购买 nike

下面的代码试图将文件从文件夹复制到另一个目录。

即使文件在那里,它也无法找到该文件。

Sub copyFiles()

Dim fldrname As String, fldrpath As String, sFileType As String
Dim sSourcePath As String, Destination As String
Dim sFile As String
Dim fso As Object, fFolder As Object, fFile As Object

Set fso = CreateObject("scripting.filesystemobject")
sSourcePath = "\\oak\data\Placeholder\APP\VMP0\sxv0_out" '
sFile = "Placeholder.csv"
fldrpath = "W:\Placeholder\2018\10. Oct\"

Set fFolder = fso.GetFolder(sSourcePath)

'For Each fFile In fFolder.files
If Not fso.FileExists(fFolder & sFile) Then
MsgBox "Specified File Not Found", vbInformation, "Not Found"

'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not fso.FileExists(fldrpath & sFile) Then
fso.Copyfile (fFolder & sFile), fldrpath, True
MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
Else
MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If

End Sub

最佳答案

我更喜欢使用 DIR在 FileSystemObject 之前是一个原生函数。这是将文件从一个文件夹复制到另一个文件夹的代码。如果需要,您还可以使用 DIR 遍历所有文件。

Option Explicit

Sub CopyFiles()

Dim fldrpath As String
Dim sSourcePath As String
Dim sFile As String, source_file As String


sSourcePath = "\\oak\data\Placeholder\APP\VMP0\sxv0_out"
sFile = "Placeholder.csv"
fldrpath = "W:\Placeholder\2018\10. Oct\"

source_file = Dir(sSourcePath & "\" & sFile)

If Trim(source_file) <> vbNullString Then
If Dir(fldrpath, vbDirectory) = vbNullString Then MkDir fldrpath
FileCopy sSourcePath & "\" & sFile, fldrpath & sFile
MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
Else
MsgBox "Specified File Not Found", vbInformation, "Not Found"
End If


End Sub

关于excel - 将文件从文件路径复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52824167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com