<http://www.netikka.net/tsneti/info/tscmd099.htm>
Copyright © 2003-2012 by Prof. Timo Salmi  
Last modified Thu 29-Nov-2012 19:20:18

 
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.



99} How do I "touch" a file to update its last modified datestamp?

With pure script:
  @echo off & setlocal enableextensions
  if [%1]==[] (
    echo Usage: %~0 ["FileName"]
    echo Wildcards allowed
    goto :EOF
    )
  for %%f in ("%~1") do copy /b /v %%f +,,> nul
  dir "%~1"
  endlocal & goto :EOF
The date/time stamps of folders are not changed.

A touch UNIX port could be used for a more generic handling of the files' date stamps. For example to have 14.7.2008 13:01 for MyFile.txt one might have
  touch 071413012008 MyFile.txt
If MyFile.txt does not exist, an empty file is created (unless a -c option is used).

Consider a related, somewhat complicated, but instructive task of taking and setting a file's modified datestamp from another file's stamp.
  @echo off & setlocal enableextensions
 
  if "%~2"==""   (call :ProgramHelp & goto _out)
  if "%~1"=="?"  (call :ProgramHelp & goto _out)
  if "%~1"=="/?" (call :ProgramHelp & goto _out)
 
  :: Assign a value for the temporary folder variable temp_
  call :AssignTemp temp_
  :: Other assignments
  set vbs1_=%temp_%\tmp1$$$.vbs
 
  :: Do the files exist
  call :FileNotExist "%~1" notfound_
  if defined notfound_ goto _out
  call :FileNotExist "%~2" notfound_
  if defined notfound_ goto _out
 
  :: Check that an essential auxiliary program is available
  call :IsProgramAtPath "unxtouch.exe" uniqfound_
  if not defined uniqfound_ (
    echo The UNIX-like utility touch.exe from UnxUtils.zip is not avalable
    echo Or has NOT been renamed unxtouch.exe as required by this script
    echo For UnxUtils.zip see e.g. http://www.netikka.net/tsneti/info/tscmd.php#gawk
    goto :_out)
 
  call :GetFileModifiedDate "%~2" fileYYYY fileMM fileDD fileHH fileMN fileSS
 
  :: Remove the rems for debugging
  rem call :ShowVarValue fileYYYY
  rem call :ShowVarValue fileMM
  rem call :ShowVarValue fileDD
  rem call :ShowVarValue fileHH
  rem call :ShowVarValue fileMN
  rem call :ShowVarValue fileSS
  ::
  set UNIXdateStamp=%fileMM%%fileDD%%fileHH%%fileMN%%fileYYYY%.%fileSS%
  rem call :ShowVarValue UNIXdateStamp
 
  :: Touch the first file
  call :AskQuestionWithYdefault "Change %~1 datestamp [Y,n]?" doit_
  if "%doit_%"=="n" goto :_out
  set tz_=%TZ%
  set TZ=Europe/Helsinki
  unxtouch "%~1" -t%UNIXdateStamp%
  set TZ=%tz_%
  dir "%~1"|findstr /i "%~nx1"
 
  :_out
  call :CleanUp
  endlocal & goto :EOF
 
  :: ===================================================================
  :GetFileModifiedDate
  setlocal
  set skip=
  >"%vbs1_%" findstr "'%skip%VBS1" "%~f0"
  for /f "usebackq tokens=1-6" %%a in (`
  cscript //nologo "%vbs1_%" "%~1"`) do (
  set retYyyy=%%a
  set retMM=%%b
  set retDD=%%c
  set retHH=%%d
  set retMN=%%e
  set retSS=%%f)
  endlocal & set "%2=%retYyyy%" & set "%3=%retMM%" & set "%4=%retDD%" & set "%5=%retHH%" & set "%6=%retMN%" & set "%7=%retSS%" & goto :EOF
 
  :AssignTemp
  setlocal
  set return_=%temp%
  if defined mytemp if exist "%mytemp%\" set return_=%mytemp%
  endlocal & set "%1=%return_%" & goto :EOF
 
  :CleanUp
  for %%f in ("%vbs1_%") do if exist %%f del %%f
  goto :EOF
 
  :FileNotExist
  setlocal
  set return_=
  if not exist "%~1" (
    echo Exiting: File "%~1" not found
    set return_=true)
  if exist "%~1\" (
    echo Exiting: "%~1" is a folder
    set return_=true)
  endlocal & set "%2=%return_%" & goto :EOF
 
  :IsProgramAtPath SearchFor found_
  setlocal enableextensions disabledelayedexpansion
  set found_=
  for %%f in ("%~1") do set found_="%%~$PATH:f"
  if exist "%found_%\" set found_=
  if exist "%~1" if not exist "%~1\" set found_="%~1"
  if [%found_%]==[""] set found_=
  endlocal & set "%~2=%found_%" & goto :EOF
 
  :AskQuestionWithYdefault
  setlocal enableextensions
  :_asktheyquestionagain
  set return_=
  set ask_=
  set /p ask_="%~1"
  if "%ask_%"=="" set return_=y
  if /i "%ask_%"=="y" set return_=y
  if /i "%ask_%"=="n" set return_=n
  if not defined return_ goto _asktheyquestionagain
  endlocal & set "%2=%return_%" & goto :EOF
 
  :ProgramHelp
  echo.
  echo Usage: "%~f0" [FileToBeTouched] [DateStampFrom]
  goto :EOF
 
  :: For debugging, show verbally the value of an environment variable
  :ShowVarValue TheNameOfTheVariable
  setlocal
  set par1_=%~1
  call set value_=%%%par1_%%%
  echo The value of %~1 is %value_%
  endlocal & goto :EOF
 
  ' ====================================================================
  ' A Visual Basic Script to get a file's datestamp
  '

  Set arg = WScript.Arguments 'VBS1
  Set fso = CreateObject("Scripting.FileSystemObject") 'VBS1
  Filename = arg(0) 'VBS1
  Set f = fso.GetFile(Filename) 'VBS1
  WScript.StdOut.Write DatePart("yyyy",f.DateLastModified) & " " 'VBS1
  WScript.StdOut.Write Right(0 & DatePart("m",f.DateLastModified), 2) & " " 'VBS1
  WScript.StdOut.Write Right(0 & DatePart("d",f.DateLastModified), 2) & " " 'VBS1
  WScript.StdOut.Write Right(0 & DatePart("h",f.DateLastModified), 2) & " " 'VBS1
  WScript.StdOut.Write Right(0 & DatePart("n",f.DateLastModified), 2) & " " 'VBS1
  WScript.StdOut.Write Right(0 & DatePart("s",f.DateLastModified), 2) & " " 'VBS1

Also see item #134.