如何使用Powershell移动和重命名文件以包含日期?

时间:2015-06-02 16:10:19

标签: powershell

param([String]$fileName)
$Target = "C:\Users\pb\Desktop\destination"

if (Test-Path $fileName)
{
Copy-Item $fileName $Target
} Else
{
"File does not exist"
}

1 个答案:

答案 0 :(得分:0)

你可以这样做:

param([String]$fileName)

if (Test-Path $fileName)
{
  $file   = [io.path]::GetFileNameWithoutExtension($fileName)
  $ext    = [io.path]::GetExtension($fileName)
  $Target = "C:\Users\pb\Desktop\destination" + $file + $(get-date -f yyyy-MM-dd) + $ext
  Move-Item $fileName $Target
} 
Else {
  Write-Host "File does not exist!"
}