View previous topic :: View next topic |
Author |
Message |
helb
Joined: 08 Oct 2014 Posts: 57
|
(Separately) 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
|
|
|
Back to top |
|
|
helb
Joined: 08 Oct 2014 Posts: 57
|
(Separately) Posted: Wed Apr 27, 2016 23:35 Post subject: |
|
|
Актуальная версия с фиксом для read-only атрибута:
Code: | <# Keep Timestamps [by helb] v2015-12-07
Сохраняет атрибуты указанных файлов и папок в CSV-файл или восстанавливает из него
Использование: “<list file (UTF-8)> [/r]” или “/l” (l=восстановить, r=рекурсивно)
Saves attributes 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
#>
$attributeFile = "%TEMP%\attributes-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}
}
$attributeFile = [environment]::ExpandEnvironmentVariables($attributeFile)
if ($restore){
if (!(test-path -literalPath $attributeFile)) { msgBx "No timestamp file." "Error"; return }
[array]$files = import-csv $attributeFile -delimiter "`t"
foreach ($entry in $files){
if (test-path -literalPath $entry.FullName){
$file = get-item -literalPath $entry.FullName -force
$file.IsReadOnly = $false
$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}
}
}
}
$files | select FullName, CreationTime, LastWriteTime, LastAccessTime, Attributes | export-csv $attributeFile -force -delimiter "`t" -encoding "UTF8"
}
|
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|