1

Hi am not able to rename a file using the below code. Please help me on this

    $date = Get-Date -format "yyyyMMdd"
    $path='D:\Users\user\Desktop\Working\'
    $fn = $path+'xxx_'+$date+'.txt'
    $tn = $path+'yyy'+$date+'.dat'
    Rename-Item -Path $fn -NewName $tn

Am getting the below error.

Rename-Item : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again. At line:1 char:1 + Rename-Item -Path $fn -NewName $tn + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand

4
  • the file exist? Commented Jun 6, 2018 at 13:38
  • Right before the rename, do this Write-Host ('$fn = {0} - $tn = {1}' -f $fn, $tn) Commented Jun 6, 2018 at 13:44
  • Please update your question with the result of the instruction in EBGreen's comment. Commented Jun 6, 2018 at 15:02
  • 1
    Try Remove-Variable path prior to defining path, in case something is casting $path to something besides a [string]. I had some code in my profile caused some similar issues by not cleaning up the $path variable. Using alternative methods of building the string like LotPings solution work by verifying that $path is of the expected type before use. (LotPings answer even has some extra validation to validate the path prior to use) Commented Jun 6, 2018 at 19:55

1 Answer 1

2

I'd use Join-Path and Test-Path

$date = Get-Date -format "yyyyMMdd"
$path='D:\Users\user\Desktop\Working\'
$fn = Join-Path $path ("xxx_{0}.txt" -f $date)
$tn = Join-Path $path ("yyy_{0}.txt" -f $date)
If ((Test-Path $fn) -and !(Test-Path $tn)){
    Rename-Item -Path $fn -NewName $tn
} else {
    "{0} exists is {1}, `n{2} not exists is {3}" -f $fn,(Test-Path $fn),$tn,(!(Test-Path $tn))
}

There is no output on a successful rename, on error the output is like:

D:\Users\user\Desktop\Working\xxx_20180606.txt exists is False,
D:\Users\user\Desktop\Working\yyy_20180606.txt not exists is False
Sign up to request clarification or add additional context in comments.

18 Comments

Not working how? There is an error? The file is accidentally being copied to Pluto? Wombats keep licking the spaces between your toes and it tickles? There is even a minimun limit on comment size so that people don't bother with worthless comments like "Not working".
@pfx See BenH's comment, my method of building the file name with the -format operator prevents mistreating $fn as a string array.
I lost track of the conversation. $fn is saying it is a string and the -Path property of Rename-Item takes a string. I don't understand what the issue is.
@EBGreen I think the assumpltion that the + was causing the $fn to become a string array. As we don't know the exact content of $path was and if probaply strange contents got lost pasting here it's IMO useless to speculate further without any input from OP.
@EBGreen My issue has to be something in my profile hence why I couldn't figure out why it was being treated as an array, but I got the same behavior as the asker but -noprofile fixes it. Still not sure what module is the offender.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.