Consider the following set of files (the file names must not contain
exclamation marks "
@echo off & setlocal enableextensions
enabledelayedexpansion
rem Insert e.g. a set of *.jpg files' dates into their names
rem
rem Customise the source and target paths as appropriate
set sourceDir=C:\_M
set targetDir=C:\_M\TEMP
::
if not exist "%sourceDir%\" (
echo Exiting: Folder "%sourceDir%" not found
goto :EOF)
if not exist "%targetDir%\" (
echo Exiting: Folder "%targetDir%" not found
goto :EOF)
::
rem This is essential
pushd "%sourceDir%"
::
rem The following date format is assumed DD.MM.YYYY
rem If this does not tally with your locale's, customize fdd, fmm, fyyyy
for /f "usebackq delims=" %%f in (`
dir /b "%sourceDir%\*.jpg"`
) do (
set fullname=%%~ff
set basename=%%~nf
set extensio=%%~xf
set fdatetim=%%~tf
set fdd=
!fdatetim:~0,2
!
set fmm=
!fdatetim:~3,2
!
set fyyyy=
!fdatetim:~6,4
!
rem When you are sure that this is what you want, remove the security "echo"
rem Alternatively redirect the output to another script, and the run that
echo copy /-y "
!fullname
!" "%targetDir%\
!basename
!!fyyyy
!!fmm
!!fdd
!!extensio
!"
)
popd
endlocal & goto :EOF
The output will be
C:\_D\TEST>CMDFAQ
copy /-y "C:\_M\Buoy.JPG" "C:\_M\TEMP\Buoy20090328.JPG"
copy /-y "C:\_M\Car.JPG" "C:\_M\TEMP\Car20091107.JPG"
copy /-y "C:\_M\Copy of Signpost.JPG" "C:\_M\TEMP\Copy of Signpost20090321.JPG"
copy /-y "C:\_M\Field.JPG" "C:\_M\TEMP\Field20090905.JPG"
copy /-y "C:\_M\Signpost.JPG" "C:\_M\TEMP\Signpost20090321.JPG"
If you want a locale-independent solution that is possible with a
Visual Basic Script (
@echo off & setlocal enableextensions enabledelayedexpansion
rem Insert e.g. a set of *.jpg files' dates and times into their names
rem
rem Customise the source and target paths as appropriate
set sourceDir=C:\_M
set targetDir=C:\_M\TEMP
::
if not exist "%sourceDir%\" (
echo Exiting: Folder "%sourceDir%" not found
goto :EOF)
if not exist "%targetDir%\" (
echo Exiting: Folder "%targetDir%" not found
goto :EOF)
::
rem Build a Visual Basic Script for getting a file's datestamp
set skip=
set vbs_=%temp%\tmp$$$.vbs
if defined mytemp set vbs_=%mytemp%\tmp$$$.vbs
findstr "'%skip%
VBS1" "%~f0" > "%vbs_%"
rem If you want also the times included change VBS1 to VBS in the above
::
rem This is essential
pushd "%sourceDir%"
::
rem Process each file
for /f "usebackq delims=" %%f in (`
dir /b "%sourceDir%\*.jpg"`
) do (
set fullname=%%~ff
set basename=%%~nf
set extensio=%%~xf
for /f "usebackq delims=" %%a in (`
cscript //nologo "%vbs_%" "!fullname!"`) do set fdatetim=%%a
rem When you are sure that this is what you want, remove the security "echo"
rem Alternatively redirect the output to another script, and the run that
echo
copy /-y "!fullname!" "%targetDir%\!basename!!fdatetim!!extensio!"
)
popd
::
rem Clean up
for %%f in ("%vbs_%") do if exist %%f del %%f
endlocal & goto :EOF
'
'................................................................
'The Visual Basic Script
Const ForReading = 1, ForWriting = 2, ForAppending = 8 '
VBS1
Dim arg, FDate, FileName, FSO, f '
VBS1
Set WshShell = WScript.CreateObject("WScript.shell") '
VBS1
Set FSO = CreateObject("Scripting.FileSystemObject") '
VBS1
Set arg = WScript.Arguments '
VBS1
FileName = arg(0) '
VBS1
Set f = FSO.GetFile(FileName) '
VBS1
FDate = f.DateLastModified '
VBS1
Wscript.StdOut.Write DatePart("yyyy",FDate) '
VBS1
Wscript.StdOut.Write Right(0 & DatePart("m",FDate), 2) '
VBS1
Wscript.StdOut.Write Right(0 & DatePart("d",FDate), 2) '
VBS1
Wscript.StdOut.Write Right(0 & DatePart("h",FDate), 2) '
VBS2
Wscript.StdOut.Write Right(0 & DatePart("n",FDate), 2) '
VBS2
Wscript.StdOut.Write Right(0 & DatePart("s",FDate), 2) '
VBS2
'Comment: The other alternatives for setting FDate
'f.DateCreated f.DateLastAccessed
Naturally, the expressions could be formulated slightly differently
and more concisely such as e.g.