TotalCommander plus PowerShell: redate file(s) according to a date pattern in FileName

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
User avatar
beb
Power Member
Power Member
Posts: 579
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

TotalCommander plus PowerShell: redate file(s) according to a date pattern in FileName

Post by *beb »

Example use case:

Let's say, I have a file (eg. a scan) that I named as '19700101_1212_scanned_20241202_2300.ext' (or '19700101 1212 scanned 20241202 2300.ext',
{or '\d\d\d\d\d\d\d\d[ _]\d\d no-matter-what.ext'}),
where:
'19700101_1212' (or '19700101 1212') reflects a timestamp of a document's contents (i.e. the document content was created on January 01, 1970, at 12:12).
'20241202_2300' (or '20241202 2300') reflects a timestamp of when I scanned it.

By design, a copy of a document will have its timestamp reflecting the time it was saved (scanned, etc) (here, December 02, 2024, at 23:00).

What if:
I wish such a document to be kept under its content's date.

1. Redating file(s) in the active pane under the root path BUT NOT in subfolders:

User button:

Code: Select all

TOTALCMD#BAR#DATA
em_redate_pattern

TOTALCMD64.EXE,41
PowerShell: Redate files by pattern

0
-1
User-command (usercmd.ini):

Code: Select all

[em_redate_pattern]
cmd=pwsh -c "%commander_path%\Plugins\app\PowerShell\redateFilesPattern.ps1"
2. Redating file(s) in the active pane under the root path AND recursively in any subfolder(s):

Code: Select all

TOTALCMD#BAR#DATA
em_redate_pattern_recursive

TOTALCMD64.EXE,41
PowerShell: Redate files by pattern (recursively)

0
-1
User-command (usercmd.ini):

Code: Select all

[em_redate_pattern_recursive]
cmd=pwsh -c "%commander_path%\Plugins\app\PowerShell\redateFilesPattern.ps1"
param='recursive'
PowerShell script (redateFilesPattern.ps1):

Code: Select all

$lap   = [system.diagnostics.stopwatch]::StartNew()
if (!($args)){
$files = Get-ChildItem -force -file         |Where {$_.Name -match '^\d{8}[ _]\d{4}'}}
else {
$files = Get-ChildItem -force -file -recurse|Where {$_.Name -match '^\d{8}[ _]\d{4}'}}

$files | foreach {$name = $_.Name
$year  = $name.substring(0,4)
$month = $name.substring(4,2)
$day   = $name.substring(6,2)
$hour  = $name.substring(9,2)
$min   = $name.substring(11,2)
$old   = "{0:yyyy}{0:MM}{0:dd}_{0:HH}{0:mm}" -f (Get-Item $_).LastWriteTime
$new   = $year+$month+$day+'_'+$hour+$min
if ($new -ne $old) {
Write-Host $old -f Blue
Write-Host $new -f Green
Write-Host -f Yellow $_.Name
(Get-Item $_).LastWriteTime = Get-Date ($year+'-'+$month+'-'+$day+' '+$hour+':'+$min)
}}
$lap.Stop();'';'';"Timer : {0:mm}:{0:ss}.{0:fff}" -f $lap.Elapsed;'';sleep -s 5
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10/15
Post Reply