gpt4 book ai didi

powershell - 如何设置文件的 LastWriteTime 属性?

转载 作者:行者123 更新时间:2023-12-01 08:31:48 31 4
gpt4 key购买 nike

我想更改使用此脚本生成的文件的创建日期:

$clientname = Read-Host "Enter the client name"
$path = Read-Host "Enter the complete path of .bak files"

$time = "01-00-00"
$space = " "


for ($i = 0; $i -lt 7;$i++)
{

$date = (Get-Date).AddDays(-1-$i).ToString('yyyy-MM-dd')

New-Item -ItemType file $path$clientname$space$date$space$time.bak

}

所以它给了我这些文件:
Mode                LastWriteTime     Length Name                                                          
---- ------------- ------ ----
-a--- 16/08/2013 16:55 0 client 2013-08-15 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-14 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-13 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-12 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-11 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-10 01-00-00.bak
-a--- 16/08/2013 16:55 0 client 2013-08-09 01-00-00.bak

我想修改每个文件的 LastWriteTime 属性,我希望它与文件名中的日期相同。

此文件的示例 "client 2013-08-15 01-00-00.bak" LastWriteTime 将为 "15/08/2013 01:00"
我被卡住了,我不知道我们怎么能做到这一点

谢谢

最佳答案

未测试,但在调用 New-Item 后在循环中尝试此操作:

$file = Get-ChildItem $path$clientname$space$date$space$time.bak
$file.LastWriteTime = (Get-Date).AddDays(-1-$i)

如果您想查看可以使用 FileInfo 对象执行的操作的完整列表,请尝试在 Powershell 控制台中调用 $file | gm。您还可以查看 docs on MSDN

关于powershell - 如何设置文件的 LastWriteTime 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18276452/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com