使用 PowerShell 的文件名时间戳 [英] TimeStamp on file name using PowerShell
本文介绍了使用 PowerShell 的文件名时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串路径,
"C:\temp\mybackup.zip"
我想在该脚本中插入一个时间戳,例如,
I would like insert a timestamp in that script, for example,
"C:\temp\mybackup 2009-12-23.zip"
是否有一种在 PowerShell 中执行此操作的简单方法?
Is there an easy way to do this in PowerShell?
推荐答案
您可以使用子表达式在双引号字符串中插入任意 PowerShell 脚本代码,例如 $() 如下所示:
You can insert arbitrary PowerShell script code in a double-quoted string by using a subexpression, for example, $() like so:
"C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip"
如果您从其他地方获取路径 - 已经作为字符串:
And if you are getting the path from somewhere else - already as a string:
$dirName = [io.path]::GetDirectoryName($path)
$filename = [io.path]::GetFileNameWithoutExtension($path)
$ext = [io.path]::GetExtension($path)
$newPath = "$dirName\$filename $(get-date -f yyyy-MM-dd)$ext"
如果路径恰好来自 Get-ChildItem:
Get-ChildItem *.zip | Foreach {
"$($_.DirectoryName)\$($_.BaseName) $(get-date -f yyyy-MM-dd)$($_.extension)"}
这篇关于使用 PowerShell 的文件名时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文