I wanted the 'touch' feature of cloning / duplicating the file dates from another file, natively, and be usable from a batch file.
So 'drag and drop' video file on to batch file, FFMPEG runs, then 'Date Created' and 'Date Modified' from the input file gets copied to the output file.
This seemed simple at first until you find batch files are terrible at handling unicode file names, in-line PowerShell messes up with file name symbols, and double escaping them is a nightmare.
My solution was make the 'touch' part a seperate PowerShell script which I called 'CLONE-FILE-DATE.ps1' and it contains:
param
(
[Parameter(Mandatory=$true)][string]$SourcePath,
[Parameter(Mandatory=$true)][string]$TargetPath
)
(GI -LiteralPath $TargetPath).CreationTime = (GI -LiteralPath $SourcePath).CreationTime
(GI -LiteralPath $TargetPath).LastWriteTime = (GI -LiteralPath $SourcePath).LastWriteTime
Then here is example usage within my 'CONVERT.BAT' batch file:
%~dp0\ffmpeg -i "%~1" ACTION "%~1-output.mp4"
CHCP 65001 > nul && PowerShell -ExecutionPolicy ByPass -File "%~dp0\CLONE-FILE-DATE.PS1" "%~1" "%~1-output.mp4"
I think the PowerShell is readable, so will just explain the batch speak:
%~dp0 is the current directory of the batch file.
%~1 is the path of the file dropped onto the batch without quotes.
CHCP 65001 > nul sets characters to UTF-8 and swallows the output.
-ExecutionPolicy ByPass allows you to run PowerShell without needing to modify the global policy, which is there to prevent people accidentally running scripts.
http://stackoverflow.com/questions/210201/how-to-create-empty-text-file-from-a-batch-file mentions a load, including
REM.>a
– barlop – 2015-06-17T00:30:53.803The four alternatives mentioned above, plus four more not mentioned here, can be found in the answer to a similar question: "Windows Recursive Touch Command"
– JdeBP – 2011-02-28T19:58:25.003https://www.npmjs.com/package/touch-for-windows works for me – Piotr Migdal – 2020-01-29T19:46:17.670