powershell - 使用 PowerShell 在文件名上添加时间戳

标签 powershell

我有一个字符串路径,

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/

相关文章:

c# - Powershell 和 C# : add a parameter script in a pipeline

powershell - 格式化百分比小数位并删除尾随零

powershell - Invoke-WebRequest 未被识别为 cmdlet Windows 7 Powershell 的名称

powershell - Powershell正在复制文件夹的内容,但忽略了复制文件夹

powershell - 设置 TFS 构建版本号 PowerShell

powershell - 如何在 Powershell 的文件名中计算和插入 SHA?

powershell - PowerShell remove-item成功但抛出异常

regex - 主机文件的正则表达式的行为有所不同

regex - 在 PowerShell/PowerCLI 中将前缀附加到字符串

c# - powershell cmdlet 的动态输出类型?