helb
|
Posted: Mon Nov 03, 2014 20:52 Post subject: Сохранение/восстановление свойств файлов (время/атрибуты) |
|
|
Скрипт на powershell для сохранения в неизменном виде свойств файлов. Сохраняет временны́е (создание, модификация, доступ) и прочие атрибуты выделенных файлов и папок в CSV-файл (в том числе рекурсивно) из которого впоследствии, после манипуляций, может их восстановить. Создаем три команды: две на сохранение, одну на восстановление. При желании можно закомментировать (“#”) или удалить ненужные свойства в двух местах в скрипте.
Запускать командой вида “powershell <имя.ps1> <параметры>”. PS должен быть установлен (актуально только для Win XP), и должно быть разрешено выполнение скриптов (единовременная команда “Set-ExecutionPolicy RemoteSigned” в среде PS)
keep-timestamps.ps1:
Code: |
<# Keep Timestamps by helb
Сохраняет атрибуты указанных файлов и папок в CSV-файл или восстанавливает из него
Использование: “<list file (UTF-8)> [/r]” или “/l” (l=восстановить, r=рекурсивно)
Saves timestamps of listed items to a CSV file or restores saved ones
Usage: “<list file (UTF-8)> [/r]” or “/l” (l=load(restore), r=recursively)
Total Commander list parameter: %UL
#>
$timestampFile = "%TEMP%\timestamps-B3B2CFDF-AE75-4112-B6C4-239982E09E7D.tmp"
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
function msgBx($text, $title, $btns = 0) {
return [System.Windows.Forms.MessageBox]::Show($text, $title, $btns)
}
if ($args.length -lt 1) { msgBx "No parameters." "Error"; return }
if ($args[0] -eq "/l") {$restore = $true}
else {
$list = [environment]::ExpandEnvironmentVariables($args[0])
if ($args[1] -and $args[1] -eq "/r") {$recurse = $true}
}
$timestampFile = [environment]::ExpandEnvironmentVariables($timestampFile)
if ($restore){
if (!(test-path -literalPath $timestampFile)) { msgBx "No timestamp file." "Error"; return }
$files = import-csv $timestampFile -delimiter "`t"
foreach ($entry in $files){
if (test-path -literalPath $entry.FullName){
$file = get-item -literalPath $entry.FullName -force
$file.CreationTime = $entry.CreationTime
$file.LastWriteTime = $entry.LastWriteTime
$file.LastAccessTime = $entry.LastAccessTime
$file.Attributes = $entry.Attributes
}
}
}
elseif (!(test-path -literalPath $list)) { msgBx "List file not found." "Warning"; return }
else {
$files = @()
$filelist = get-content $list
foreach ($name in $filelist) {
if (test-path -literalPath $name) {
$files += get-item -literalPath $name -force
if ($recurse) {
$tmpfiles = get-childItem -literalPath $name -recurse -force #| sort-object FullName
if (!($tmpfiles.FullName -and $tmpfiles.FullName -eq $name)) {$files += $tmpfiles}
}
# else { }
}
}
$files | select FullName, CreationTime, LastWriteTime, LastAccessTime, Attributes | export-csv $timestampFile -force -delimiter "`t" -encoding "UTF8"
}
pause
|
|
|