<# Fix timestamps by mask [by helb] v2015-12-07
Takes filenames of files you need to fix from specified list, gets respective source filenames by replacing mask match (and optionally path) and restores timestamps from them.
e.g. somefile1.png > somefile-stitched.png
Command line usage: powershell <this script pathname> <listfile> -mask '<regexp of what to replace>' [-subst '<substitute text>'] [-mask2 '<>'] [-subst2 '<>'] [-pfrom """<path part to replace>""" -pto """<path substitute>"""]
Example: powershell fix-timestamps-mask.ps1 %temp%\list.tmp -mask '\.m4a$|\.aac$' -srcmask '.mp3' -pfrom """B\:Sync\Converted\""" -pto """D:\"""
Any ‘$’ characters in path should be escaped with ‘`’ (grave accent/back tick): “`$”
Total Commander list parameter: %UL
Берет имена файлов, которым требуется исправить даты, из указанного списка, получает имена соответствующих им исходных файлов заменой по маске (и опционально пути) и восстанавливает дату из них.
Использование: powershell <путь скрипта> <файл-список> -mask '<regexp того что требуется заменить>' [-subst '<на что заменить>'] [-mask2 '<>'] [-subst2 '<>'] [-pfrom """<часть пути для замены>""" -pto """<на что заменить (путь)>"""]
Любые символы «$» в пути требуется заменить на «`$»
Параметр листа в TC: %UL
#>
Param
(
[parameter(Position=0)][String[]]$list,
[String[]]$mask,
[String[]]$mask2,
[String[]]$subst,
[String[]]$subst2,
[String[]]$pfrom,
[String[]]$pto
)
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
function setTimestamp($path, $time) {
$file = get-item -LiteralPath $path -force
$ro = $file.IsReadOnly
$file.IsReadOnly = $false
$file.LastWriteTime = $time
if ($ro) { $file.IsReadOnly = $true }
}
function msgBx($text, $title, $btns = 0) {
return [System.Windows.Forms.MessageBox]::Show($text, $title, $btns)
}
#msgBx ($mask +" • "+ $srcmask +"`n"+ $pfrom +"`n"+ $pto +"`n"+ $list) "titl"
if (!$list) { msgBx "No list specified." "Error"; return }
if (!$mask) { msgBx "No mask specified." "Error"; return }
$list = [environment]::ExpandEnvironmentVariables($list)
if (!(test-path -literalPath $list)) { msgBx "List file not found." "Warning"; return }
else {
(get-content $list | where-object { (test-path -literalPath $_) }) | foreach {
if ($_ -match $mask) {
if ($matches.count -gt 1) {
$srcname = [string]([io.fileinfo]$_).name -replace $matches[1], $subst
} else {
$srcname = [string]([io.fileinfo]$_).name -replace $matches[0], $subst
}
if ($mask2 -and $srcname -match $mask2) {
if ($matches.count -gt 1) {
$srcname = $srcname -replace $matches[1], $subst2
} else {
$srcname = $srcname -replace $matches[0], $subst2
}
}
$pathname = [string]([io.fileinfo]$_).directory + "\"
if ($pfrom -and $pto) {
$pathname = $pathname.replace($pfrom, $pto)
}
$srcname = $pathname + $srcname
if (test-path -literalPath $srcname) {
setTimestamp $_ (get-item -literalPath $srcname -force).LastWriteTime
}
}
}
}
|