Back to Scripting | Rename/move file with date appended between filename and file extension


This VBscript moves a file to a different name (either in the same path or a different path, if specified) and inserts the current system date into the name before the period followed by the file extension. For instance, FILENAME.EXT will become FILENAME_YYYYMMDD.EXT, where YYYYMMDD is the current system date. The original path and filename plus an optional new path and filename without the date are passed in as parms to make this script generic enough that it can be called from variety of CMD files with different parameters.


RenameMyFile.cmd

REM This is an example of calling the VBscript below
REM d:\OldPath\MyFile.txt will become d:\OldPath\MyFile_19981023.txt, for instance
cscript RenameAppendDate.vbs d:\OldPath\MyFile.txt d:\NewPath\MyOldFile.txt
REM d:\OldPath\MyFile.txt will become d:\NewPath\MyOldFile_19981023.txt, for instance
cscript RenameAppendDate.vbs d:\OldPath\MyFile.txt d:\NewPath\MyOldFile.txt


RenameAppendDate.vbs

'SCRIPT:  	RenameAppendDate.vbs
'AUTHOR:  	D. Sisk
'DATE:    	1998/08/17
'DESC:    	Performs a DOS MOVE on a file, renaming it using the original name plus the 
'               current date OR using the specified new name plus the current date.  
'               New filename will look like FILENAME_YYYYMMDD.EXT.
'DEPENDENCIES:	DOS move.exe, WSH Cscript.exe host

'Constants
DblQuote = CHR(34)
LineFeed = CHR(10)

'Get the command-line arguments.  The first argument is required.  
'The second argument is optional and will be set the same as the first argument if not specified.
If Wscript.Arguments.Count = 0 Then 'Show interactive help text
   Wscript.Echo "PURPOSE: Renames/moves a file while appending the current date to the old, or a new, filename.  " & LineFeed &  _
                "OUTPUT: OriginalFileName_YYYYMMDD.xxx OR NewFileName_YYYYMMDD.xxx"  & Linefeed & _
                "SYNTAX:  cscript RenameAppendDate OriginalPathandFileName [NewPathandFileName]"
   Wscript.quit
End If
OriginalPathFileName = Wscript.Arguments(0)  'First argument is original filename and path
If Wscript.Arguments.Count = 2 Then
   NewBasePathFileName = Wscript.Arguments(1)   'Second argument, if specified, if new filename and path
Else
   NewBasePathFileName = OriginalPathFileName   'Second argument default to original filename and path if not specified
End If

'Get the current date and format it into a string that looks like "_YYYYMMDD"
DateString = "_" & year(date()) & right("0" & month(date()),2) & right("0" & day(date()),2)

'Create a Shell object
Set WshShell = Wscript.CreateObject("Wscript.Shell") ' Create a Shell object

'Construct the command string to MOVE the original file to the new filename plus the date string
CmdString = "CMD /C move " & OriginalPathFileName & " " & mid(NewBasePathFileName,1,instr(1,NewBasePathFileName,".")-1) _
 & DateString & right(NewBasePathFileName,len(NewBasePathFileName) - instr(1,NewBasePathFileName,".") + 1)

'Execute the MOVE command
ReturnCode = WshShell.Run(CmdString, 1, TRUE) '0 = String found, 1 = string not found

Wscript.quit  'Exit the script