C:\_G\WWW\~ELISANET\INFO\tscmd197.html
<https://www.salminet.fi/info/tscmd197.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Sun 23-Aug-2026 17:53:50

 
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.


197} A touching revisit with PowerShell-aided TOUCHE.CMD

  Date related tasks can also be approched using PowerShell-aided cmd.exe command-line. This entry expands on item#099.
  @echo off & setlocal enableextensions
  :: Set a test file's location
  set path_=C:\_D\TEST
  set file_=file.txt
  :: Just exit if the test file does not exist
  if not exist "%path_%\%file_%" (echo "%path_%\%file_%" not found & goto :EOF)
  :: Display the existing file's relevant information
  dir "%path_%\%file_%" | find "%file_%"
  ::
  :: Set the last modified date to your own choice
  powershell -command "(Get-Item '%path_%\%file_%').LastWriteTime = Get-Date '2026-07-23 14:30:00'"
  dir "%path_%\%file_%" | find "%file_%"
  ::
  :: Set the last modified datespam to the current date/time
  powershell -command "(Get-Item '%path_%\%file_%').LastWriteTime = Get-Date"
  dir "%path_%\%file_%" | find "%file_%"
  endlocal & goto :EOF

The output might be e.g.
C:\_M>C:\_D\TEST\CMDFAQ.CMD
  24.07.2026  17:46                16 file.txt
  23.07.2026  14:30                16 file.txt
  24.07.2026  17:52                16 file.txt

  @echo off & setlocal enableextensions
  rem C:\_D\TEST\TOUCHE.CMD
  ::
  :: Get the current datetime stamp
  call :GetDatepsTimeNow datetime_
  ::
  call :ProgramTitle
  if "%~1"=="" (call :ProgramHelp & goto :EOF)
  ::
  :: The file to be touched
  set target_=%~1
  echo "%~1"|findstr "[*?]">nul
  if %errorlevel% EQU 0 (echo Error: Do not use wildcards & goto :EOF)
  if not exist "%target_%" (echo Error: File "%target_%" not found & goto :EOF)
  ::
  :: The datetime to be used for touching
  if /i not "%~2"=="" set datetime_=%~2
  if /i not "%~3"=="" (echo Exiting: Invalid datetime formulation, forgot quotes? & goto :EOF)
  ::
  call :GetFileModifiedDateTimeSingle "%target_%" before_
  echo %before_% "%target_%"
  set /p ask_="Touch %target_% to %datetime_% [N/y]? "
  if /i not "%ask_%"=="y" goto :EOF
  ::
  :: Changing i.e. touching the datetime stamp of the target file
  powershell -command "(Get-Item '%target_%').LastWriteTime = Get-Date '%datetime_%'"
  ::
  call :GetFileModifiedDateTimeSingle "%target_%" after_
  echo %after_% "%target_%"
  ::
  endlocal & goto :EOF
 
  :ProgramTitle
  echo +------------------------------------------------------------+
  echo : TOUCHE.CMD a file's modified datetime utilizing PowerShell :
  echo : By Prof. Timo Salmi, Last modified Sun 23-Aug-2026 :
  echo +------------------------------------------------------------+
  echo.
  goto :EOF
 
  :ProgramHelp
  echo Usage: TOUCHE "Filename" ["yyyy-mm-dd hh:mn:ss"]
  echo If used, the datetime stamp must follow exactly that format
  echo Especially the latter quotes are important in that case
  echo The default timestamp is the current "%datetime_%"
  echo (Note: Unlike UNIX cannot be used to create a new empty file)
  :_eof_ProgramHelp
  goto :EOF
 
  :GetDatepsTimeNow
  rem The format follows set defdatetimeps_=2026-12-31 00:01:02
  setlocal
  call :GetPSdatetime PSddd PSyyyy PSMM PSdd PSHH PSmn PSss
  set return_=%PSyyyy%-%PSMM%-%PSdd% %PSHH%:%PSmn%:%PSss%
  endlocal & set "%1=%return_%" & goto :EOF
 
  :: The indentation is essential to avoid incorrect syntax on the divided line   
  :GetPSdatetime
  for /f "usebackq delims=" %%a in (`powershell -NoLogo -NoProfile -Command ^
    "Get-Date -Format 'ddd dd.MM.yyyy HH:mm:ss'"`) do (
      set "pstemptime=%%a")
  for /f "tokens=1-7 delims=.: " %%a in ("%pstemptime%") do (
    set PSwd=%%a
    set PSdd=%%b
    set PSMM=%%c
    set PSyyyy=%%d
    set PSHH=%%e
    set PSmn=%%f
    set PSss=%%g
    )
  set pstemptime=
  :_eof_GetPSdatetime
  goto :EOF
 
  :GetFileModifiedDateTimeSingle
  setlocal enableextensions
  set file_=%~1
  for /f "usebackq delims=" %%a in (`powershell -command ^
    "(Get-Item '%file_%').LastWriteTime.ToString('ddd yyyy-MM-dd HH:mm:ss')"`) do (
      set result_=%%a)
  endlocal & set "%2=%result_%" & goto :EOF

The output might be e.g.
  C:\_M>C:\_D\TEST\TOUCHE.CMD
  +------------------------------------------------------------+
  : TOUCHE.CMD a file's modified datetime utilizing PowerShell :
  : By Prof. Timo Salmi, Last modified Sun 23-Aug-2026 :
  +------------------------------------------------------------+
 
  Sun 2026-08-23 13:10:19 "test.txt"
  Touch test.txt to 2026-08-23 11:41:33 [N/y]? y
  Sun 2026-08-23 11:41:33 "test.txt"

For more PowerShell-aided cmd.exe command-line solutions see #powershell.

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd197.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd197
C:\_E\ARCZIP\COPYTONE.CMD /~info
https://www.salminet.fi/info/tscmd197.html
file:///c:/_g/www/~elisanet/info/tscmd197.html