Script Center > Gallery > Storage > Rename a File Using a Customized Date
TechNet Script Center logo

Welcome to the TechNet Script Center Gallery!

Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.

Rename a File Using a Customized Date

(Community)
Rate it:
 
 
 
 
 
Script Code
VBScript
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
Platforms
Windows Server 2008 R2 No
Windows Server 2008 No
Windows Server 2003 No
Windows 7 No
Windows Vista No
Windows XP No
Windows 2000 No
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.