将变量传递给 powershell 中的 ren 命令
passing variables to ren command in powershell
有人可以告诉我这个语法有什么问题吗?我根本无法让它工作:
#there is file called MyLog.log inside'logoutput'
$file='myimage'
$LogsDestFolder='logoutput'
Write-Host "Renaming log for $file.bmp"
ren $LogsDestFolder\MyLog.log $LogsDestFolder$file.log
一直抱怨我无法在 'ren' 命令中传递变量。我试过用单引号或双引号引起来,但没有用。
继续获取:
ren : 无法重命名指定的目标,因为它表示路径或设备名称。
谢谢!
这里的问题很可能是您将路径传递给参数 -NewName
。您的命令目前翻译成这样:
Rename-Item -Path $LogsDestFolder\MyLog.log -NewName $LogsDestFolder$file.log
ren
= Rename-Item
可以从 Get-Alias ren
中看出。我 运行 时得到的实际错误是:
ren : Cannot rename because the target specified represents a path or device name.
如果您在 TechNet 中查看 Rename-Item
,您将在 -NewName
的描述下方看到
Specifies the new name of the item. Enter only a name, not a path and name. If you enter a path that is different from the path that is specified in the Path parameter, Rename-Item generates an error.
无需设置新名称的路径,因为您已经使用 -Path
设置了该路径。删除路径部分并保留文件名,如 Ryan 建议的那样。
Rename-Item -Path "$LogsDestFolder\MyLog.log" -NewName "$file.log"
#or
Ren "$LogsDestFolder\MyLog.log" "$file.log"
始终将路径放在引号中是个好主意。在这种情况下,您无论如何都需要防止解析错误。
有人可以告诉我这个语法有什么问题吗?我根本无法让它工作:
#there is file called MyLog.log inside'logoutput'
$file='myimage'
$LogsDestFolder='logoutput'
Write-Host "Renaming log for $file.bmp"
ren $LogsDestFolder\MyLog.log $LogsDestFolder$file.log
一直抱怨我无法在 'ren' 命令中传递变量。我试过用单引号或双引号引起来,但没有用。
继续获取:
ren : 无法重命名指定的目标,因为它表示路径或设备名称。
谢谢!
这里的问题很可能是您将路径传递给参数 -NewName
。您的命令目前翻译成这样:
Rename-Item -Path $LogsDestFolder\MyLog.log -NewName $LogsDestFolder$file.log
ren
= Rename-Item
可以从 Get-Alias ren
中看出。我 运行 时得到的实际错误是:
ren : Cannot rename because the target specified represents a path or device name.
如果您在 TechNet 中查看 Rename-Item
,您将在 -NewName
Specifies the new name of the item. Enter only a name, not a path and name. If you enter a path that is different from the path that is specified in the Path parameter, Rename-Item generates an error.
无需设置新名称的路径,因为您已经使用 -Path
设置了该路径。删除路径部分并保留文件名,如 Ryan 建议的那样。
Rename-Item -Path "$LogsDestFolder\MyLog.log" -NewName "$file.log"
#or
Ren "$LogsDestFolder\MyLog.log" "$file.log"
始终将路径放在引号中是个好主意。在这种情况下,您无论如何都需要防止解析错误。