I am trying to batch rename old log files, but the script only works for me when the script is stored in the same folder as the log files. Here is the code:
cls
$file = gci E:\logs |? {$_.extension -eq ".log" |% {rename-item $_ ($_.Name + ".old")}
When I run this script from E:\logs
, it works just fine. However, when I run this script from C:\Scripts, it gives me this error:
Rename-Item: Cannot rename because item at 'my.log.file.log' does not exist.
At C:\Scripts\rename-script.ps1:2 char:92
+ $file = gci E:\logs |? {$_.extension -eq ".log" |% {rename-item $_ ($_.Name + ".old")}
+ CategoryInfo : InvalidOperation (:) [Rename-Item], FSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
I also tried using the Move-Item
command with the -literalpath
switch but had the same result