【问题标题】:Rename-item thinks variable is a pathRename-item 认为变量是路径
【发布时间】:2014-07-08 10:08:53
【问题描述】:

我正在尝试重命名一个文件,但 powershell 认为我的变量是一个字符串并且失败了。

这是脚本:

$date=(get-date -Format d)
$time=(get-date -Format t)
$source = "D:\_qapi.log"
$newfilename = "$date"+"_"+"$time"+"_qapi[SERVERNAME].log"

Rename-Item $source -NewName $newfilename

这是错误:

Rename-Item : Cannot rename because the target specified represents a path or device name.

有人知道我可以解决这个问题吗?由于某种原因,powershell 将 $newfilename 中的 $date 变量视为路径。

【问题讨论】:

    标签: powershell rename rename-item-cmdlet


    【解决方案1】:

    日期时间字符串中的非法字符。

    这行得通:

    $date=(get-date -Format d) -replace("/")
    $time=(get-date -Format t) -replace(":")
    $source = "D:\_qapi.log"
    $newfilename = "$date"+"_"+"$time"+"_qapi[$env:Computername].log"
    
    Rename-Item $source -NewName $newfilename
    

    【讨论】:

      猜你喜欢
      • 2017-01-12
      • 2020-08-24
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      相关资源
      最近更新 更多