1

Is there a way to convert long file names into a truncated short form on pc? Is there a simple way to do this? I'm new to using the command prompt / PowerShell and don't fully understand what long scripts are doing and how to modify them.

I want to transfer all of my files to an external hard drive but many of the files are from a mac with long names and I receive an error when I try to transfer them.

4
  • Are you talking about using DOS filenames (8+3) or just shorter than they currently are. If so how would a script know what to name a file. You would need rules. Also what are the errors you are getting?, long filenames shouldn't be a problem on Windows 10 unless they are really long. Aug 13, 2016 at 5:15
  • I would like to rename them to the DOS filename (8+3). The error I receive when transferring is file name too long. They are really long file names over the Windows limit. Aug 13, 2016 at 14:48
  • It's really the long paths, not long names because all modern filesystems support filenames of 255 characters. Another possibility is names containing special characters like *:\/? which are allowed in POSIX but not in Win32 namespace. So the problem isn't that you need 8.3 DOS names but Windows compatible names
    – phuclv
    Dec 24, 2016 at 9:05
  • @ Animatoring, have you finally found a solution for creating 8+3 filenames along with long ones in Windows 10? I don't want to post a similar question. It would be a duplicate!
    – Apostolos
    Nov 24, 2019 at 16:06

2 Answers 2

2

Save the following into a file Set-DosFileName.ps1

[CmdletBinding(SupportsShouldProcess=$true)]
Param(
    [parameter(Mandatory=$true)]
    [string]$folder,
    [switch]$recurse
)

$fso = New-Object -ComObject Scripting.FileSystemObject

Get-ChildItem -Path $folder -File -Recurse:$recurse | ForEach-Object {

    $shortName = $fso.getfile($_.Fullname).ShortName
    if ($shortName -ne $_.Name)
    {
        $fullShortName = Join-Path $_.Directory -ChildPath $shortName
        Move-Item -LiteralPath $_.Fullname -Destination $fullShortName
    }
}

To use this open a PowerShell window and change into the directory where you saved the file:

cd "D:\folder where you saved the script"

then:

.\Set-DosFileName.ps1 -folder "D:\myfiles\Foo Bar" -whatif

The script should show how it would rename your files.

To include all files in subdirectories add the -recurse switch:

.\Set-DosFileName.ps1 -folder "D:\myfiles\Foo Bar" -whatif -recurse

If everything looks fine, remove the -whatif switch to actually rename the files. I would still keep a backup of the original files just in case anything goes wrong.

I haven't tested this with a large number of files, be aware that some file names may be pretty ugly.

4
  • Code fails:Get-ChildItem : A parameter cannot be found that matches parameter name 'File'.
    – Zack A
    Nov 18, 2022 at 2:23
  • @ZackA - You must be using a very old version of PowerShell Nov 24, 2022 at 20:46
  • I'm using Powershell 7.2.
    – Zack A
    Nov 27, 2022 at 17:11
  • In my Powershell 7.2 Get-ChildItem has a parameter -file, weird. Dec 1, 2022 at 10:17
2

Since Windows 10 Version 1607, the file path length limit is removed. To enable this, open regedit.exe, go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem and create a DOWRD 32Bit LongPathsEnabled and change the value to 1.

enter image description here

Now the 260 char issue is gone.

6
  • I tried this but I'm not having any luck unfortunately. If you go to rename a long file after doing the changes above it doesn't give the option on my computer. I created a reg_dword for LongPathsEnabled and set it to 1. Did I forget to do something? Thanks! Aug 16, 2016 at 23:02
  • ok, the app has to optin via an manifest entry. I haven't checked if explorer has the entry, but I think it should Aug 17, 2016 at 4:16
  • Thanks! I noticed I didn't have the most current version that had the ability to enable the long paths. I think it should work now. Aug 17, 2016 at 4:28
  • probably he has names with special characters like ?\/* which are not allowed in Windows
    – phuclv
    Dec 24, 2016 at 9:07
  • Really? Does this answer the question "Convert long file names to short names" ??? In fact, it is for the exact opposite: How to create even longer file names by extending their length !!!
    – Apostolos
    Nov 24, 2019 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .