0

再次大家好!

我一直在研究.vb 和.vbs。我有一个关于在复制文件后重命名文件的小问题。从这个(只是在信用到期时给予信用:p)人我已经找到了如何将文件复制到另一个文件夹,但是我似乎无法重命名文件。

所以我想把文件复制下来重命名为原来的execute.HMS

这是复制的代码:

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("F:\commandfolder")

Set colFiles = objFolder.Files

dtmOldestDate = Now
 
For Each objFile in colFiles
    If objFile.DateCreated < dtmOldestDate Then
        dtmOldestDate = objFile.DateCreated
        strOldestFile = objFile.Path
    End If
Next

objFSO.CopyFile strOldestFile, "F:\commandfolder\Processed\"

在此先感谢和亲切的问候,

戴夫

4

3 回答 3

1

VBScript 不提供文件的重命名方法。您必须MoveFile改用:

objFSO.CopyFile strOldestFile, "F:\commandfolder\Processed\"
objFSO.MoveFile strOldestFile, objFSO.GetParentFolderName(strOldestFile) & "\execute.HMS"

更好的选择可能是记住文件对象而不仅仅是它的路径,然后使用对象的方法:

For Each objFile in colFiles
    If objFile.DateCreated < dtmOldestDate Then
        dtmOldestDate = objFile.DateCreated
        Set oldestFile = objFile
    End If
Next

oldestFile.Copy "F:\commandfolder\Processed\"
oldestFile.Name = "execute.HMS"
于 2013-10-30T14:03:38.337 回答
0

这是我的工作解决方案(已根据上下文进行了编辑,但如果需要,您会弄清楚的 :))

Set obj = CreateObject("Scripting.FileSystemObject")
Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("F:\commandfolder")

Set colFiles = objFolder.Files

dtmOldestDate = Now

For Each objFile in colFiles
    If objFile.DateCreated < dtmOldestDate Then
        dtmOldestDate = objFile.DateCreated
        strOldestFile = objFile.Path
    End If
Next


objFSO.CopyFile strOldestFile, "F:\commandfolder\Processed\"
obj.DeleteFile("F:\commandfolder\Action\execute.hms")
objFSO.MoveFile strOldestFile, "F:\commandfolder\Action\execute.hms" 
于 2013-11-02T11:28:52.370 回答
0

基于对“查找”问题的回答,我添加了代码以使用fileMove方法移动文件:

  Const csSrcF = "..\testdata\17806396"
  Const csDstF = "..\testdata\17806396\dst\"
  Dim goFS     : Set goFS    = CreateObject( "Scripting.FileSystemObject" )
  Dim oLstPng  : Set oLstPng = Nothing
  Dim oFile
  For Each oFile In goFS.GetFolder(csSrcF).Files
      If "png" = LCase(goFS.GetExtensionName(oFile.Name)) Then
         If oLstPng Is Nothing Then
            Set oLstPng = oFile ' the first could be the last
         Else
            If oLstPng.DateLastModified < oFile.DateLastModified Then
               Set oLstPng = oFile
            End If
         End If
      End If
  Next
  If oLstPng Is Nothing Then
     WScript.Echo "no .png found"
  Else
     WScript.Echo "found", oLstPng.Name, oLstPng.DateLastModified
     oLstPng.Move csDstF
     If goFS.FileExists(goFS.BuildPath(csDstF, oLstPng.Name)) Then
        WScript.Echo "Moved."
     Else
        WScript.Echo "Not moved."
     End If
  End If
于 2013-10-30T13:47:25.330 回答