none
Powershell - rename file extension with ~ in the name RRS feed

  • Question

  • I'm using this PS code to rename .XLS files to .xls:

    Text
    gci '\\server\d$\Share\Data\' -Recurse | where {$_.extension -clike '*.XLS'} | Rename-Item -NewName {($_.basename) + ($_.extension.tolower())}
    

    It's working for all cases except for the files with ~ in the name (like this one: spec~1.XLS). 

    Dont know why.



    • Edited by bckms Sunday, July 29, 2018 6:49 AM
    • Moved by Bill_Stewart Tuesday, December 11, 2018 9:26 PM Not a question
    Sunday, July 29, 2018 6:34 AM

All replies

  • Wierd, i tried it with the dir | ren command in powershell aswell but same problem

    This is dirty but should work in powershell:

    &cmd /c ren \\server\d$\Share\Data\*.XLS *.xls

    Sunday, July 29, 2018 9:33 AM
  • The "~" is a character used for the 8.3 name. Any name with this pattern ????~(number from 1 up). is an 8.3 name.

    What you are seeing indicates a corrupt file directory and possible disk issues.  Try doing a repair on the volume.

    Display the files like this and look at the names in the 8.3 column.

    Open a CMD prompt outside of PowerShell:

    type "dir <folder> /x"

    Look at the names:

    C:\Users\jvierra>dir /x
     Volume in drive C is Windows
     Volume Serial Number is F89A-041B
    
     Directory of C:\Users\jvierra
    
    07/10/2018  05:16 PM    <DIR>                       .
    07/10/2018  05:16 PM    <DIR>                       ..
    06/04/2018  03:10 PM    <DIR>          DOTNET~1     .dotnet
    07/10/2018  05:16 PM    <DIR>          VSCODE~1     .vscode
    07/09/2018  12:01 PM    <DIR>          3DOBJE~1     3D Objects
    01/22/2018  10:04 PM    <DIR>                       ansel
    07/09/2018  12:01 PM    <DIR>                       Contacts
    07/09/2018  12:01 PM    <DIR>                       Desktop
    07/13/2018  01:45 PM    <DIR>          DOCUME~1     Documents
    07/28/2018  10:45 AM    <DIR>          DOWNLO~1     Downloads
    07/28/2018  05:07 AM    <DIR>          FAVORI~1     Favorites
    01/17/2018  05:27 PM    <DIR>                       HP
    01/17/2018  07:22 PM    <DIR>                       Intel
    07/09/2018  12:01 PM    <DIR>                       Links
    07/09/2018  12:01 PM    <DIR>                       Music
    07/28/2018  08:16 AM    <DIR>                       OneDrive
    07/28/2018  08:16 AM    <DIR>          ONEDRI~1     OneDrive - DESIGNED SYSTEMS & SERVICES
    07/28/2018  10:40 AM    <DIR>                       Pictures
    01/14/2018  04:49 AM    <DIR>                       Roaming
    07/09/2018  12:01 PM    <DIR>          SAVEDG~1     Saved Games
    07/09/2018  12:01 PM    <DIR>                       Searches
    01/14/2018  05:24 AM    <DIR>                       source
    05/25/2017  03:59 AM            25,312              test.txt
    07/28/2018  08:16 AM    <DIR>                       Videos

    Note the 8.3 name and the full name are displayed.


    \_(ツ)_/

    Sunday, July 29, 2018 11:21 AM
  • In CMD its working: 

    ren d:\ *.XLS *.xls

    In PS no:

    PS C:\WINDOWS\system32> &cmd /c ren d:\ *.XLS *.xls
    The syntax of the command is incorrect.

    Thanks for help.

    Sunday, July 29, 2018 8:41 PM
  • You command is wrong which is why you are getting "The syntax of the command is incorrect.".

    cmd /c ren d:\*.XLS *.xls

    In Windows there is no way to return TXT separate from txt.  Case is ignored in Windows.

    To find all files with an uppercase extension:

    Get-ChildItem *.xls | Where{ $_.Extension -ceq '.XLS }


    \_(ツ)_/

    Sunday, July 29, 2018 8:57 PM