我想运行一个 Windows 脚本,该脚本应该将一个或多个文件夹的日期时间更改为最近的日期时间。它应在每晚午夜通过任务调度程序运行。我需要获取当天且不更早的所有文件夹。
来源:
脚本运行于 2024-03-07 23:59: 后的目标
有什么想法如何做到这一点吗? 谢谢和亲切的问候 消音器
目前我无法获取今天的文件夹:D
尽管您在评论中指出批处理解决方案是首选,但我会建议您参考这个SO答案。引用原作者的话:
我看不到更改日期的简单方法。
重命名或更改属性都不会影响修改日期。
目前,唯一的方法似乎是重命名目录,创建一个新目录,并将内容从旧目录移动到新目录。
这只是一个令人讨厌的解决方案。
因为这是一个令人讨厌的解决方案,而且既然你提到任何脚本都可以,所以我正在使用 Powershell。
# path to check for directories
$parentDirectory = "M:\Test\"
<# date used is in YYYY-MM-DD format
the date below is for testing purposes, use it instead of the original
to see it in action
# $cutoffDate = "2024-01-01"
#>
$cutoffDate = Get-Date -Format "yyyy-MM-dd"
# test if the path is valid
if(-not (Test-Path -Path $parentDirectory) ) {
Write-Host Path does not exist
Exit
}
# loop through all the first-level directories
# keep only those with desired date
Get-ChildItem -Path $parentDirectory -Directory |
Foreach-Object {
# get the last modified date
$directoryModDate = $(Get-Item $_.FullName).lastwritetime
# convert it to YYYY-MM-DD
$directoryModDate = Get-Date $directoryModDate -Format "yyyy-MM-dd"
# compare it to our cutoff date
# if it's older, skip it
if($directoryModDate -lt $cutoffDate) {
# skip it. uncomment the following line to see the skipped ones
# Write-Host Skipping $directoryModDate " --- " $cutoffDate
return
}
# not earlier than the cutoff date, we can work with it
# uncomment the following line to see the picked up date
# Write-Host $directoryModDate
$(Get-Item $_.FullName).lastwritetime = $(Get-Date ($cutoffDate + " 23:59:00"));
}
限制
它不是递归的。这意味着它只会处理一级目录,而不会检查其内容。原因是——这并不在你最初的要求中
<dir_name> -match "Folder\..*"
M:\Test\
中任何具有等于今天日期的 Date modified
的目录都将其 lastwritetime
更改为 <today> 23:59:00
。这一遗漏的原因与前一个相同 - 它不在您最初的请求中
Date modified
是否已正确更改。