如何设置文件的 LastWriteTime 属性? [英] How to set LastWriteTime property of a file?

查看:55
本文介绍了如何设置文件的 LastWriteTime 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$clientname = Read-Host "输入客户端名称"$path = Read-Host "输入 .bak 文件的完整路径"$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 文件 $path$clientname$space$date$space$time.bak}

所以它给了我这些文件:

Mode LastWriteTime Length Name---- ------------- ------ -----a--- 16/08/2013 16:55 0 客户端 2013-08-15 01-00-00.bak-a--- 16/08/2013 16:55 0 客户端 2013-08-14 01-00-00.bak-a--- 16/08/2013 16:55 0 客户端 2013-08-13 01-00-00.bak-a--- 16/08/2013 16:55 0 客户端 2013-08-12 01-00-00.bak-a--- 16/08/2013 16:55 0 客户端 2013-08-11 01-00-00.bak-a--- 16/08/2013 16:55 0 客户端 2013-08-10 01-00-00.bak-a--- 16/08/2013 16:55 0 客户端 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 对象执行的操作的完整列表,请尝试调用 $file |gm 在您的 powershell 控制台中.您还可以查看 MSDN 上的文档.>

I would like to change the creation date of the files that I generate with this script :

$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

}

So it gives me theses files :

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  

I want to modify the LastWriteTime property of each files, I want it to be the same as the date in the file name.

Example for this file "client 2013-08-15 01-00-00.bak" the LastWriteTime will be "15/08/2013 01:00"

I'm stuck and I do not how can we do that

Thank you

解决方案

Not tested, but try this in your loop after you call New-Item:

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

If you want to see a full listing of things you can do with FileInfo objects, try calling $file | gm in your powershell console. You could also view the docs on MSDN.

这篇关于如何设置文件的 LastWriteTime 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆