2013-10-30 25 views
0

大家好!使用vbs复制并重命名最早的文件

我一直在挖掘.vb和.vbs。复制后重命名文件有一个小问题。从this(只是给信贷的信用到期: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\" 

感谢,

戴夫

回答

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" 
0

基于this answer to a 'find' problem,我添加了代码来移动文件通过使用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 
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"
+0

有一个File/Folder.Move方法:http://msdn.microsoft.com/en-us/library/kxtftw67 %28v = vs.84%29.aspx –

+1

@ Ekkehard.Horner我知道。但是,如果我正确理解OP,他想将文件复制到'F:\ commandfolder \ Processed',然后重命名源文件夹中的文件。 –

+0

当然,你是对的。 –