-aided
cmd.exe command-line. This entry expands on
.
@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.