【问题标题】:Recursive "touch" on fileserver文件服务器上的递归“触摸”
【发布时间】:2013-02-01 13:47:56
【问题描述】:
我想通过管理我的 Windows 文件服务器来完成一些事情:
我想将我服务器上所有文件夹(只是文件夹和子文件夹,而不是其中的文件)的“上次修改”日期更改为与最近的“创建”(或者可能是“上次修改”)相同") 文件夹中的日期文件。 (在许多情况下,文件夹上的日期比其中的最新文件新得多。)
我想递归地执行此操作,从最深的子文件夹到根目录。我也想在不手动输入任何日期和时间的情况下执行此操作。
我敢肯定,结合脚本和“触摸”的 Windows 端口,我也许可以做到这一点。你有什么建议吗?我也许可以做到这一点。你有什么建议吗?
这个封闭的话题似乎很接近,但我不确定如何只触摸文件夹而不触摸里面的文件,或者如何获取最新文件的日期。 Recursive touch to fix syncing between computers
【问题讨论】:
标签:
windows
date
recursion
directory
last-modified
【解决方案1】:
我认为您可以在 PowerShell 中执行此操作。我只是试着把一些东西放在一起,它似乎工作正常。您可以在 PowerShell 中使用 Set-DirectoryMaxTime(".\Directory") 调用它,它会在该目录下的每个目录上递归操作。
function Set-DirectoryMaxTime([System.IO.DirectoryInfo]$directory)
{
# Grab a list of all the files in the directory
$files = Get-ChildItem -File $directory
# Get the current CreationTime of the directory we are looking at
$maxdate = Get-Date $directory.CreationTime
# Find the most recently edited file's LastWriteTime
foreach($file in $files)
{
if($file.LastWriteTime -gt $maxdate) { $maxdate = $file.LastWriteTime }
}
# This needs to be in a try/catch block because there is a reasonable chance of it failing
# if a folder is currently in use
try
{
# Give the directory a LastWriteTime equal to the newest file's LastWriteTime
$directory.LastWriteTime = $maxdate
} catch {
# One of the directories could not be updated
Write-Host "Could not update directory: $directory"
}
# Get all the subdirectories of this directory
$subdirectories = Get-ChildItem -Directory $directory
# Jump into each of the subdirectories and do the same thing to each of their CreationTimes
foreach($subdirectory in $subdirectories)
{
Set-DirectoryMaxTime($subdirectory)
}
}