David Vandenbroucke
David Vandenbroucke

Reputation: 114

Copy and rename oldest file using vbs

Hello again everybody!

I've been digging into .vb and .vbs. I have a small problem concerning renaming a file after copying it. From this (just giving credit where credit is due :p) person I've found how to copy the file to another folder, however I seem not to have been able to rename the file.

So I want to copy the file and rename the original to execute.HMS

This is the code for copying:

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

Thanks in advance and kind regards,

Dave

Upvotes: 0

Views: 1407

Answers (3)

David Vandenbroucke
David Vandenbroucke

Reputation: 114

This is my working solution (it's edited to the context but you'll figure it out if you need it :))

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" 

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

VBScript doesn't provide a rename method for files. You have to use MoveFile instead:

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

A better option might be remembering the file object instead of just its path and then using the object's methods:

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"

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Based on this answer to a 'find' problem, I added code to move the file by using the file's Move method:

  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

Upvotes: 0

Related Questions