Tuesday, December 30, 2014

Convert long file names to short names windows 10?


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.


Answer



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.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...