You could use the following commented batch code:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" goto :EOF
for /F delims^=?*^ eol^= %%I in ("%~1") do if "%~1" == "%%I" goto NoPattern
for %%I in (%1) do set "FilePath=%%~dpI" & goto ProcessFiles
echo ERROR: There is no file matched by: "%~1"
goto :EOF
:NoPattern
rem Get full name of directory or file.
for %%I in (%1) do set "FilePath=%%~fI"
if not "%FilePath:~-1%" == "\" set "FilePath=%FilePath%\"
rem References the passed argument string an existing directory?
if exist "%FilePath%" goto ProcessFiles
rem References the passed argument string an existing file?
if exist %1 set "FilePath=%~dp1" & goto ProcessFiles
echo ERROR: Could not find: "%~1"
goto :EOF
:ProcessFiles
rem Escape each backslash in file path with one more backslash.
set "EscapedFilePath=%FilePath:\=\\%"
for /F "eol=| delims=" %%I in ('dir %1 /A-D-H /B 2^>nul') do (
rem Get name of file without path.
set "FileName=%%I"
setlocal EnableDelayedExpansion
set "EscapedName=!FileName:'=\'!"
set "FileDateTime="
rem Get creation date and time of file in format YYYYMMDDHHMMSS.
for /F "usebackq tokens=2 delims==." %%J in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='!EscapedFilePath!!EscapedName!'" GET CreationDate /VALUE 2^>nul`) do set "FileDateTime=%%J"
if defined FileDateTime (
rem The first eight characters are the file creation date.
set "FileDate=!FileDateTime:~0,8!"
rem The next six characters are the file creation time.
set "FileTime=!FileDateTime:~8,6!"
rem Get the first sixteen characters of the current file name.
set "FileNameStart=!FileName:~0,16!"
rem Rename the current file only if its name does not already start with the
rem creation date and time to avoid renaming the same files again and again.
if not "!FileNameStart!" == "!FileDate!-!FileTime!-" (
if not exist "!FilePath!!FileDate!-!FileTime!-!FileName!" (
ren "%FilePath%!FileName!" "!FileDate!-!FileTime!-!FileName!" 2>nul
if errorlevel 1 echo ERROR: Failed to rename file: "%FilePath%!FileName!"
) else (
echo ERROR: Cannot rename file: "%FilePath%!FileName!"
echo There is already a file with name: "!FileDate!-!FileTime!-!FileName!"
)
)
) else (
echo ERROR: Failed to get file time for: "%FilePath%!FileName!"
echo It is necessary to change manually the file name.
)
endlocal
)
endlocal
NOTE: This batch file cannot rename files containing one or more commas in file name!
%%~tV
expands to last modification file time in a region and language dependent format without second. But wanted is the creation file time with the second.
WMIC - the Windows Management Instrumentation Command-line utility - can be used to get creation file time with second in a region and language independent date/time format. The disadvantage of usage of WMIC is the speed. This command is very slow. For more details about WMIC usage for file times read my answer on Find out if file is older than 4 hours in batch file.
Use LastModified
instead of CreationDate
if you want last modification file time instead of creation file time.
What is the difference between creation and last modified date?
The last modified date is the date on which the content of the file was modified last time.
The creation date is the date on which the file was first time created in the directory it is currently stored.
For example if a file was created new yesterday, it has same creation and last modified date because the file was created in the directory and the content was last modified at the same time. If the file is copied today to a different directory, the last modified date is still the same date (yesterday), but the creation date of the copy is today.
Most people are interested only in last modified date and not in file creation date because important is when the file content was modified the last time and not when the file was created in the current directory.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... for an explanation of %1
and %~1
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
ren /?
set /?
setlocal /?
wmic /?
wmic datafile /?
wmic datafile get /?