如何设置文件的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"

我被卡住了,我不知道我们怎么能做到

感谢

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

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

如果您想查看可以使用FileInfo对象执行的操作的完整列表,请尝试在powershell控制台中调用$file | gm。您也可以在MSDN上查看这些文档。

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
}

最新更新