我有一个字符串路径,
C:\temp\mybackup.zip
我想在该脚本中插入一个时间戳,例如,
C:\temp\mybackup 2009-12-23.zip
在 PowerShell 中是否有一种简单的方法可以做到这一点?
最佳答案
您可以使用子表达式在双引号字符串中插入任意 PowerShell 脚本代码,例如 $(),如下所示:
"C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip"
如果您从其他地方获取路径 - 已经作为字符串:
$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 - 使用 PowerShell 在文件名上添加时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1954203/