<http://www.netikka.net/tsneti/info/tscmd178.htm>
Copyright © 2003-2012 by Prof. Timo Salmi  
Last modified Mon 30-Apr-2012 01:43:41

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



178} How does one plug files' modified date/time into a set of file names?

First you may wish to revisit Item #55 and maybe also Item #12 and Item #107.

Consider the following set of files (the file names must not contain exclamation marks " ! ")
 Directory of C:\_M

28.03.2009  15:28           112,990 Buoy.JPG
07.11.2009  09:41           170,977 Car.JPG
21.03.2009  14:09           161,703 Copy of Signpost.JPG
05.09.2009  15:28           264,249 Field.JPG
21.03.2009  14:09           161,703 Signpost.JPG
               5 File(s)        871,622 bytes

  @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 (VBScript) aided command line 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.
  Right(0 & DatePart("m",FDate), 2)
could be written as
  Right(0 & Month(FDate), 2)