在powershell中,我想从目录中的一个文件返回一个LastWriteTime值,然后用(原始文件name_LastWriteTime.txt)覆盖前几天的文件将一个文件写回那个目录。我想要YYMMDD格式的LastWriteTime
因此,一个示例是从c:\foo\originalfile.mdb读取LastWriteTime,然后生成一个名为c:\foo\orignalfile_LastWriteTime.txt (YYMMDD格式的LastWriteTime)的文件
你能建议一个简单的方法来做这件事吗?
发布于 2014-02-03 11:23:03
另一种选择:
Get-Item c:\foo\originalfile.mdb |
New-Item -Path {$_.Fullname.Insert($_.FullName.length - 4, $_.LastWriteTime.ToString("_yyMMdd"))} -Type file -Force
这种方法利用了这样一个事实,即您可以对接受管道输入的任何参数使用scriptblock。在New-Item
的情况下,Path
参数接受管道输入。
发布于 2014-02-03 10:00:45
# 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
https://stackoverflow.com/questions/21518337
复制相似问题