windows 如何以编程方式更改文件的创建、修改、访问日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/582553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:02:37  来源:igfitidea点击:

How do I programmatically change the create, modify, access date on a file?

windowscommand-line

提问by Keng

I need to change the modify date on a file on Windows so that it doesn't clutter up my sort order. How can I do that with a script (I may need to do that to the file in the future as well)?

我需要更改 Windows 上文件的修改日期,以免它弄乱我的排序顺序。我怎样才能用脚本来做到这一点(我将来可能也需要对文件这样做)?

BTW: I don't want to have to install applications to do this.

顺便说一句:我不想安装应用程序来做到这一点。

回答by Learning

If you have PowerShell:

如果您有 PowerShell:

$(Get-Item ).creationtime=$(Get-Date "mm/dd/yyyy hh:mm am/pm")
$(Get-Item ).lastaccesstime=$(Get-Date "mm/dd/yyyy hh:mm am/pm")
$(Get-Item ).lastwritetime=$(Get-Date "mm/dd/yyyy hh:mm am/pm")

Note that the correct date format string to use will depend on your localization, e.g. in the UK, the correct format string would be dd/mm/yyyy.

请注意,要使用的正确日期格式字符串将取决于您的本地化,例如在英国,正确的格式字符串为dd/mm/yyyy.

回答by Mark G

Using PowerShell, the command would be:

使用 PowerShell,命令将是:

PS C:\temp> (Get-Item Notes.txt).lastwritetime=$(Get-Date "1/2/2016 12:34 am")

PS C:\temp> (Get-Item Notes.txt).creationtime=$(Get-Date "1/2/2016 12:34 am")

PS C:\temp> (Get-Item Notes.txt).lastaccesstime=$(Get-Date "1/2/2016 12:34 am")

Enter image description here

Enter image description here

回答by user1016765

In PowerShell you can list the file and use that to set attributes on it in a single line.

在 PowerShell 中,您可以列出文件并使用它在一行中设置属性。

For example with wildcard:

例如使用通配符:

(ls yourF*).lastWriteTime = (get-date).AddDays(-60)

For a single file:

对于单个文件:

(ls yourFile.doc).creationTime = (get-date).AddDays(-60)

(The above commands assume you have cd'd to the file's directory.)

(以上命令假设您已 cd 到文件目录。)

回答by EBGreen

Here is a VBScript example of changing the modification date:

这是更改修改日期的 VBScript 示例:

Sub ChangeModifiedDate(strFolder, strFile, dteNew)

    Dim oShell
    Dim objFolder

    Set oShell = CreateObject("Shell.Application")
    Set oFolder = oShell.NameSpace(strFolder)
    oFolder.Items.Item(strFile).ModifyDate = dteNew
End Sub

回答by Ido Weinstein

Using Java you can do:

使用 Java,您可以执行以下操作:

File file = new File("someFile");
file.setLastModified(long time);