datevbscriptfile-move

Trying to move a file and add a date timestamp


Been reading through some of the questions and answers last couple days and can't work out where I'm going wrong here I would appreciate any help given. So I'm trying to move a file to a different fold before pulling in a new copy but I want the one I'm moving to have a Date timestamp after (just so I have a record of it in future). Apologies if this is a bit rough its my second time trying to do something like this.

DIM Date1

Date1 = Now()
DIM FSO 
SET FSO=CreateObject("Scripting.FileSystemObject")


FSO.Movefile "I:\This is it\External Storage\Replen\Data\LOCATIONS\*ITEM INVENTORY BY PURCHASE ORDER.csv*", "I:\This is it\External Storage\Replen\Data\LOCATIONS\ITEM INVENTORY BY PURCHASE ORDER\" & Date1.csv

With CreateObject("Scripting.FileSystemObject")

.MoveFile "C:\Users\s.c\Downloads\*.csv*", "I:\This is it\External Storage\Replen\Data\LOCATIONS\"
End With

Solution

  • This code will move the sSourceFile (ITEM INVENTORY BY PURCHASE ORDER.csv in your case) to sDestinationFolder and rename (to YYYY-MM-DD.csv) it using your computer's date:

    Dim sSourceFile
    Dim sDestinationFolder
    Dim sDate
    Dim objFSO
    
    sSourceFile = "I:\This is it\External Storage\Replen\Data\LOCATIONS\ITEM INVENTORY BY PURCHASE ORDER.csv"
    sDestinationFolder = "I:\This is it\External Storage\Replen\Data\LOCATIONS\ITEM INVENTORY BY PURCHASE ORDER\"
    
    ' Build date string
    sDate = Year(Now) & "-" & Month(Now) & "-" & Day(Now)
    
    ' Create FileSystem object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Check if File exists
    If objFSO.FileExists(sSourceFile) Then
    
        ' Move file
        objFSO.Movefile sSourceFile, sDestinationFolder & sDate & ".csv"
    
    Else
        ' File does not exist
    End If