首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell查找文件的文件LastWriteTime,并根据上次写入日期生成.txt文件

Powershell查找文件的文件LastWriteTime,并根据上次写入日期生成.txt文件
EN

Stack Overflow用户
提问于 2014-02-03 09:04:15
回答 2查看 908关注 0票数 0

在powershell中,我想从目录中的一个文件返回一个LastWriteTime值,然后用(原始文件name_LastWriteTime.txt)覆盖前几天的文件将一个文件写回那个目录。我想要YYMMDD格式的LastWriteTime

因此,一个示例是从c:\foo\originalfile.mdb读取LastWriteTime,然后生成一个名为c:\foo\orignalfile_LastWriteTime.txt (YYMMDD格式的LastWriteTime)的文件

你能建议一个简单的方法来做这件事吗?

EN

回答 2

Stack Overflow用户

发布于 2014-02-03 11:23:03

另一种选择:

代码语言:javascript
运行
复制
Get-Item c:\foo\originalfile.mdb | 
    New-Item -Path {$_.Fullname.Insert($_.FullName.length - 4, $_.LastWriteTime.ToString("_yyMMdd"))} -Type file -Force

这种方法利用了这样一个事实,即您可以对接受管道输入的任何参数使用scriptblock。在New-Item的情况下,Path参数接受管道输入。

票数 1
EN

Stack Overflow用户

发布于 2014-02-03 10:00:45

代码语言:javascript
运行
复制
# Specify the file name you are looking for.

$file = "originalfile.mdb"



# Create a date variable by getting the
# last write time and formatting it with Get-Date

$date = Get-Date (ls | ? {$_.Name -match $file}).LastWriteTime -Format yyMMdd



# Create a new file using the original name minus the file ext
# and add the date onto the end.

New-Item "$($file -replace '\..*$','')_$($date)" -Type File
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21518337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档