Batch file to rename files to date modified

March 13, 2011 at 20:35:03
Specs: Windows XP
I found this on PCMag, it is a batch file that will rename the file to the date modified, including month, date, year, hour, minute, and AM/PM. The problem is the files i'm renaming are pictures so there are usually 3 or 4 that were created in the same minute, so I need this batch to include the seconds when renaming, but I can't figure out how. This is what I'm using right now:

FOR %%V IN (%1) DO FOR /F "tokens=1-6 delims=/: " %%J IN ("%%~tV") DO IF EXIST %%J-%%K-%%L_%%M%%N%%O%%~xV (ECHO Cannot rename %%V) ELSE (Rename "%%V" %%J-%%K-%%L_%%M%%N%%O%%~xV)


See More: Batch file to rename files to date modified

Report •

March 14, 2011 at 09:01:39
CMD doesn't include seconds, so you're left with two alternatives. You can have the script add an unique number to the end of the file, or keep a guaranteed unique part of the name.

Alternatively, you can have your seconds, by switching to a different language, like VBScript:

Set fso = CreateObject("Scripting.FileSystemObject")
For Each arg In WScript.Arguments
  Set file = fso.GetFile(arg)
  newName = GetNewName(file.DateLastModified, fso.GetExtensionName(file))
  If Not fso.FileExists(file.ParentFolder & "\" & newName) Then
    file.Name = newName
    WScript.Echo "Unable to rename " & file
  End If
Next 'arg

Function GetNewName(modified, extension) 'As String
  GetNewName = DatePart("yyyy", modified) & "-" & DatePart("m", modified) _
    & "-" & DatePart("d", modified) & "_" & DatePart("h", modified) _
    & "-" & DatePart("n", modified) & "-" & DatePart("s", modified) _
    & "." & extension
End Function

How To Ask Questions The Smart Way

Report •
Related Solutions

Ask Question