英文:
How force timestamp of folders recursevely with Powershell?
问题
使用PowerShell,我使用以下脚本:
$item = Get-ChildItem -force -recurse | Where-Object {! $_.PSIsContainer}
foreach($object in $item)
{
$object.CreationTime=("31 December 2019 23:59:59")
$object.LastWriteTime=("31 December 2019 23:59:59")
$object.LastAccessTime=("31 December 2019 23:59:59")
}
它只会更改文件的时间戳,而不会影响文件夹。您可以如何修复它,以便文件夹的时间戳也会更新?
非常感谢。
英文:
With PowerShell i use this script:
$item = Get-ChildItem -force -recurse | Where-Object {! $_.PSIsContainer}
foreach($object in $item)
{
$object.CreationTime=("31 December 2019 23:59:59")
$object.LastWriteTime=("31 December 2019 23:59:59")
$object.LastAccessTime=("31 December 2019 23:59:59")
}
And it change timestamp for file only but not has effects on folders. How i can fix it, so which too timestamp of folders is update?
Thanks very much.
答案1
得分: 3
因为在您的代码中,您使用 | Where-Object {! $_.PSIsContainer}
进行目录过滤,所以该函数不会对文件夹执行任何操作。
您可以简化代码,使用类似以下的方式:
$date = Get-Date -Year 2019 -Month 12 -Day 31 -Hour 23 -Minute 59 -Second 59
Get-ChildItem -Path 'D:\Test' -Force -Recurse | ForEach-Object {
$_.CreationTime = $date
$_.LastWriteTime = $date
$_.LastAccessTime = $date
}
请注意,在这个示例中,根文件夹(例如 D:\Test
)本身不会受到影响,只有根文件夹内的所有文件和文件夹会获得新的时间戳。
关于您提到的 LastAccessTime
未被影响的问题:
当在资源管理器中列出目录时,Windows 会自动更新目录的 LastAccessTime
。这是由一个注册表设置处理的。
可以通过将此注册表项设置为 1
来关闭此功能(默认值为 0
)。
0
:列出目录时,NTFS 更新每个检测到的目录的最后访问时间戳,并在NTFS日志中记录每次更改。1
:列出目录时,NTFS 不会更新最后访问时间戳,也不会在NTFS日志中记录时间戳的更新。
要在目录上禁用自动更新 LastAccessTime
:
Set-ItemProperty -Path 'HKLM:SYSTEM\CurrentControlSet\Control\FileSystem' -Name NtfsDisableLastAccessUpdate -Value 1 -Type DWord
要(重新)启用默认行为:
Set-ItemProperty -Path 'HKLM:SYSTEM\CurrentControlSet\Control\FileSystem' -Name NtfsDisableLastAccessUpdate -Value 0 -Type DWord
NtfsDisableLastAccessUpdate 的可能值:
- 0 - 用户管理,启用最后访问时间更新
- 1 - 用户管理,禁用最后访问时间更新
- 2 - 系统管理,启用最后访问时间更新
- 3 - 系统管理,禁用最后访问时间更新
尽管在Windows 10中,这些值已更改为负值(十六进制):
- 0x80000000 - 用户管理,启用最后访问时间更新
- 0x80000001 - 用户管理,禁用最后访问时间更新
- 0x80000002 - 系统管理,启用最后访问时间更新
- 0x80000003 - 系统管理,禁用最后访问时间更新
希望这些解释能有所帮助。
英文:
Because in your code you are filtering out directories with | Where-Object {! $_.PSIsContainer}
, the function will not do anything on folders.
You can simplify the code using something like this:
$date = Get-Date -Year 2019 -Month 12 -Day 31 -Hour 23 -Minute 59 -Second 59
Get-ChildItem -Path 'D:\Test' -Force -Recurse | ForEach-Object {
$_.CreationTime = $date
$_.LastWriteTime = $date
$_.LastAccessTime = $date
}
Mind you, the rootfolder (in this example D:\Test
) itself wil not be affected, only all files and folders inside the rootfolder will get new timestamps.
<hr>
As for your question about LastAccessTime
not being affected:
Windows will automatically update the LastAccessTime on directories when listed in Explorer. This is handles by a registry setting.
It is possible to turn off that feature by setting this registry entry to 1
(Default value is 0
)
0
When listing directories, NTFS updates the last-access timestamp on each directory it detects, and it records each time change in the NTFS log.1
When listing directories, NTFS does not update the last-access timestamp, and it does not record time stamp updates in the NTFS log.
To disable the automatic update of the LastAccessTime on directories:
Set-ItemProperty -Path 'HKLM:SYSTEM\CurrentControlSet\Control\FileSystem' -Name NtfsDisableLastAccessUpdate -Value 1 -Type DWord
To (re)enable the default behaviour:
Set-ItemProperty -Path 'HKLM:SYSTEM\CurrentControlSet\Control\FileSystem' -Name NtfsDisableLastAccessUpdate -Value 0 -Type DWord
Possible values for NtfsDisableLastAccessUpdate
0 - User Managed, Last Access Updates Enabled
1 - User Managed, Last Access Updates Disabled
2 - System Managed, Last Access Updates Enabled
3 - System Managed, Last Access Updates Disabled
Although in Windows 10 these values have changed to negative values (in hex):
0x80000000 - User Managed, Last Access Updates Enabled
0x80000001 - User Managed, Last Access Updates Disabled
0x80000002 - System Managed, Last Access Updates Enabled
0x80000003 - System Managed, Last Access Updates Disabled
Hope that explains somewhat
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论