Vibhav MS
Vibhav MS

Reputation: 163

Changing file extension case in Powershell

We have .txt files sent by users which are encrypted. We decrypt them and send it to a 3rd party system downstream as input. It had been working well but users started to send files are .TXT instead of .txt. It doesn't make any difference during decryption but it is affecting the downstream system. We are supposed to change the .TXT to .txt

I tried changing it this way

Copy-Item -Path $myOfile –Destination ([io.path]::ChangeExtension($myOfile, '.txt')) -Verbose

Here $myOfile is my file name and it named something like this 20160506_205400_Sender_header.TXT.GPG which we decrypt and it changes to 20160506_205400_Sender_header.TXT

I used the above command to change it to 20160506_205400_Sender_header.txt and it throws the below error

Copy-Item : Cannot overwrite the item C:\Sender\Submit\20160506_205400_Sender_header.TXT with itself.

It appears as if there is no distinction between .TXT and .txt. Is there a way to do it or a workaround?

Upvotes: 2

Views: 6141

Answers (1)

Frode F.
Frode F.

Reputation: 54881

Windows is not case-sensitive when it comes filepaths, so a copy-operation with the same destination and source will fail because you're reading the file your trying to replace.

Use Rename-Item to rename files. Ex:

Rename-Item -Path $myOfile -NewName ([io.path]::ChangeExtension($myOfile, '.txt')) -Verbose

Upvotes: 3

Related Questions