如何设置文件的LastWriteTime属性?
问题描述:
我想改变我生成这个脚本文件的创建日期:如何设置文件的LastWriteTime属性?
$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"
我坚持,我没有,我们怎么能做到这一点
谢谢
答
没有测试,但这种尝试在你的循环你叫新建项目后:
$file = Get-ChildItem $path$clientname$space$date$space$time.bak
$file.LastWriteTime = (Get-Date).AddDays(-1-$i)
如果你想看到的东西,你可以用FileInfo的对象做一个全面上市,尝试在您的PowerShell控制台中调用$file | gm
。您还可以查看docs on MSDN。
+0
非常感谢你的工作 –
答
for ($i=0; $i -lt 7;$i++)
{
$date = (Get-Date).AddDays(-1-$i)
$filedate = $date.ToString('yyyy-MM-dd')
$file = New-Item -ItemType File $path$clientname$space$filedate$space$time.bak
$file.LastWriteTime = $date
}
FYI http://pscx.codeplex.com有一个非常方便的Set-FileTime命令(别名触摸)。 –