2017-02-27 53 views
0

我试图合并几个不同的脚本,这些脚本工作正常,直到我更改strPath的路径。当我将其更改为其他任何内容时,我收到一条错误消息更改路径时无效的过程调用或参数

无效的过程调用或参数。

脚本是为了找到任何目录(包括子文件夹)的最新文件,并将该文件复制并粘贴到一个文件夹

Dim strPath, oFSO, oFile, oFolder, dteDate, strName, N 

strPath = "C:\Users\parjo16\Documents\Archived" 
Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set oFolder = oFSO.GetFolder(strPath) 
For Each oFile In oFolder.Files 
    If oFile.DateLastModified > dteDate Then 
    dteDate = oFile.DateLastModified 
    strName = oFile.Name 
    End If 
    N = N + 1 
Next 'oFile 

Call FindTheSubFolderFiles(oFolder, N, dteDate, strNme) 

Const strfolder = "C:\SalaryData\" 
Const Overwrite = True 
Dim oFSOd 

Set oFSOd = CreateObject("Scripting.FileSystemObject") 

If Not oFSOd.FolderExists(strfolder) Then 
    oFSOd.CreateFolder strfolder 
End If 

oFSOd.CopyFile strNme, strfolder & "salaries.xlsx", Overwrite 

Set oFSOd = Nothing 
Set oFSO = Nothing 
Set oFile = Nothing 
Set oFolder = Nothing 

Function FindTheSubFolderFiles(ByRef oParentFolder, ByRef lngR, ByRef dteDte, ByRef strNme) 
    Dim oSubFolder 
    Dim oFile 
    For Each oSubFolder In oParentFolder.SubFolders 
    For Each oFile In oSubFolder.Files 
     If oFile.DateLastModified > dteDte Then 
     dteDte = oFile.DateLastModified 
     strNme = oFile.Path 
     End If 
     lngR = lngR + 1 
    Next 
    FindTheSubFolderFiles oSubFolder, lngR, dteDte, strNme 
    Next 'oSubFolder 
    Set oSubFolder = Nothing 
    Set oFile = Nothing 
End Function 
+0

什么* *究竟你改变'strPath'到价值,并声明* *究竟是抛出这个错误吗? –

+0

我只是试图将其更改为任何其他路径,例如C:\ Users \ parjo16 \ Documents \ CloseFiles。该错误在oFSOd.CopyFile strNme,strfolder&“salaries.xlsx”上覆盖,覆盖。感谢您的关注 –

+0

在尝试复制文件之前,检查'strNme'和'strFolder&“salaries.xlsx”'的值。 –

回答

0

看起来像剧本做两次检查,一个用于主文件夹,一个用于子文件夹。如果结果是另一个,那么它会变回空白。

这似乎是现在的工作:

Function FindTheSubFolderFiles(ByRef oParentFolder, ByRef lngR, ByRef dteDte, ByRef strNme) 
    Dim oSubFolder 
    Dim oFile 
    For Each oSubFolder In oParentFolder.SubFolders 
     For Each oFile In oSubFolder.Files 
      If oFile.DateLastModified > dteDte Then 
       dteDte = oFile.DateLastModified 
       strNme = oFile.path 
      End If 
      lngR = lngR + 1 
     Next 
     FindTheSubFolderFiles oSubFolder, lngR, dteDte, strNme 
    Next 'oSubFolder 
    Set oSubFolder = Nothing 
    Set oFile = Nothing 
End Function 

Dim strPath, oFSO, oFile, oFolder, dteDate, strName, N 
strPath = "C:\Users\parjo16\Documents\Archived\" 
Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set oFolder = oFSO.GetFolder(strPath) 
For Each oFile In oFolder.Files 
    If oFile.DateLastModified > dteDate Then 
     dteDate = oFile.DateLastModified 
     strName = oFile.Name 
    End If 
    N = N + 1 
Next 'oFile 

Call FindTheSubFolderFiles(oFolder, N, dteDate, strNme) 

Const strfolder = "C:\SalaryData\" 
Const Overwrite = True 
Dim oFSOd 

Set oFSOd = CreateObject("Scripting.FileSystemObject") 

If Not oFSOd.FolderExists(strfolder) Then 
    oFSOd.CreateFolder strfolder 
End If 

On Error Resume Next 

oFSOd.CopyFile strPath & strName, strfolder & "salaries.xlsx", Overwrite 
oFSOd.CopyFile strNme, strfolder & "salaries.xlsx", Overwrite 

Set oFSOd = Nothing 

Set oFSO = Nothing 
Set oFile = Nothing 
Set oFolder = Nothing 
相关问题