Submitted By: David Lyons

Renames a file, prepending a customized date to the name. The customized date can include the month, day, year, quarter, day of week, hours, minutes, seconds, or week of year. Run the script without parameters to view the documentation.

Visual Basic
Edit|Remove
const ForWriting = 2
const ForReading = 1
const ForAppending = 8

if wscript.arguments.count <> 2 then
    showhelp
else
    Dim fso, FILENAME, file, newname, ext, path
    FORMATARG = wscript.arguments.item(0)
    FILENAME  = wscript.arguments.item(1)
    set fso = CreateObject("Scripting.FileSystemObject")
    if fso.FileExists(FILENAME) then
        ' Retrieve the file object
        set file = fso.GetFile(FILENAME)
        ' Determine the new name
        ext = mid(file.Name, instrrev(file.name, "."))
        newname = left(file.name, len(file.name)-len(ext)) & getdate(FORMATARG, now()) & ext
        path = left(file.path, instrrev(file.path, "\")) 

        log newname

        ' Make sure that the new file does not exist
        do until not fso.FileExists(path & newname)
            fso.DeleteFile(path & newname)
        Loop

        ' Rename the file
        file.name = newname
        set file=nothing
    else
        log "File does not exist"
    end if
    set fso=nothing

end if

Sub log(msg)
    wscript.echo msg
end sub

sub showhelp()
    dim dte
    dte = now()
    log "Description:  "
    log "   Renames a file, appending a customized date to the name.  customized "
    log "   date can include month, day, year, quarter, day of week, hours, "
    log "   minutes, seconds, week of year."
    log ""
    log "Usage: "
    log "   renamefile_wDate.vbs formatstring filename"
    log ""
    log "Parameters: "
    log "   formatstring:   "
    log "      The format of the date.  The following codes can"
    log "      be included in the format.  Any other value filename"
    log "      characters can also be included (eg ""_"", ""-"", etc...)"
    log "         YYYY - The calendar year (4 digits)"
    log "         YY   - The calendar year (2 digits)"
    log "         MM   - The calendar month (01-12)"
    log "         mmm  - The calendar month name (proper: Jan-Dec)"
    log "         MMM  - The calendar month name (uppercase: JAN-DEC)"
    log "         DD   - The calendar day (1-31)"
    log "         D    - The day of the week (M-F)"
    log "         ddd  - The day of the week (proper: Mon-Fri)"
    log "         DDD  - The day of the week (uppercase: MON-FRI)"
    log "         dw   - The numeric day of the week (1-7)"
    log "         hh   - The hour of the day (00-23)"
    log "         mm   - The minute of the hour (01-60)"
    log "         ss   - The seconds (01-60)"
    log "         www  - The week of the year (W01-W52)"
    log "         QQ   - The quarter (Q1-Q4)"
    log ""
    log "           Format Examples: TODAY=" & dte
    log "               MMM mmm MM : " & getdate("MMM mmm MM", dte)
    log "               YYYY YY : " & getdate("YYYY YY", dte)
    log "               DDD ddd dw DD D : " & getdate("DDD ddd dw DD D", dte)
    log "               hh mm ss : " & getdate("hh mm ss", dte)
    log "               www QQ : " & getdate("www QQ" , dte)
    log ""
    log "   filename - The file to be renamed"
    log ""
    log "Examples:"
    log "   renamefile_wDate.vbs _YYYY-MM-DD_hhmmss ""c:\temp\a.csv"""
    log "    - The file is renamed to c:\temp\a" & getdate("_YYYY-MM-DD_hhmmss", dte) & ".csv"
    log
    log "   renamefile_wDate.vbs -YYYYQQ ""c:\temp\a.csv"""
    log "    - The file is renamed to c:\temp\a" & getdate("-YYYYQQ", dte) & ".csv"
    log ""
    log "   renamefile_wDate.vbs _DDD ""c:\temp\a.csv"""
    log "    - The file is renamed to c:\temp\a" & getdate("_DDD", dte) & ".csv"
    
end sub


function getdate(format, dte)

    ' VARIABLE DECLARATIONS
    dim r, mon(11), dw(6)
    
    ' Set these to local language requirements.
    mon(0)="Jan" : mon(1) ="Feb" : mon(2) ="Mar"
    mon(3)="Apr" : mon(4) ="May" : mon(5) ="Jun"
    mon(6)="Jul" : mon(7) ="Aug" : mon(8) ="Sep"
    mon(9)="Oct" : mon(10)="Nov" : mon(11)="Dec"
    
    dw(0)="Mon":dw(1)="Tue":dw(2)="Wed":dw(3)="Thu":dw(4)="Fri":dw(5)="Sat":dw(6)="Sun"

    ' Calculate the date    
    r = replace(format, "YYYY", year(dte))
    r = replace(r, "YY", right(year(dte), 2))
    r = replace(r, "mmm", mon(month(dte)-1))
    r = replace(r, "MMM", ucase(mon(month(dte)-1)))
    r = replace(r, "MM", right(month(dte)+100, 2))
    r = replace(r, "ddd", dw(weekday(dte)-1))
    r = replace(r, "DDD", ucase(dw(weekday(dte)-1)))
    r = replace(r, "DD", right(day(dte)+100,2))
    r = replace(r, "dw", weekday(dte))
    r = replace(r, "hh", right(hour(dte)+100,2))
    r = replace(r, "mm", right(minute(dte)+100,2))
    r = replace(r, "ss", right(second(dte)+100,2))
    r = replace(r, "www", "W" & right(datepart("ww", dte)+100,2))
    r = replace(r, "QQ", "Q" & datepart("q", dte))
    r = replace(r, "D", ucase(left(dw(weekday(dte)-1),1)))
    
    getdate = r
    
end function