Skip to content

Instantly share code, notes, and snippets.

@derofim
Created April 23, 2017 15:41
Show Gist options
  • Save derofim/f9335dffac113f7c2b75123cfb9b410a to your computer and use it in GitHub Desktop.
Save derofim/f9335dffac113f7c2b75123cfb9b410a to your computer and use it in GitHub Desktop.
shorten long file names windows

Script tested on Windows 10 64bit.

  1. Install git bash from https://git-for-windows.github.io/
    Git allows to use linux commands on windows.
  2. Open Git Bash Command Line
    Simply right-click on a folder in Windows Explorer to access the BASH or GUI.
    https://git-for-windows.github.io/img/gw1.png
  3. You can use "mv" command to rename "VeryLongFileName.ext" to "shortFileName.ext" like this:
    mv "VeryLongFileName.ext" "shortFileName.ext"
  4. If you need to rename a lot of files in folder use shorten.sh below.
    Save shorten.sh to folder where you need to rename files.
    Open git bash in folder that contains shorten.sh.
    You can change maxLen in shorten.sh from 100 to desired shortened file length.
    Run in bash terminal "sh shorten.sh".
    It will also output resulting file names like:
    Renamed ./Sonocaine - Quad City Berlin/SON_Quad_City_Berlin_002 - Large Main Road Junction, Cars And Heavy Trucks Passing Slowly, Ambulance Siren At Top, Occasional Car Horns, Big Rattling Truck At Tail (Neue Nationalgalerie) - Front Omni.wav to ./Sonocaine - Quad City Berlin/SON_Quad_City_Berlin_002 - Large Main Road Junction, Cars And Heavy T.wav

You can also try to use windows-style renaming approach (if you OS version supports it):
https://superuser.com/questions/638123/how-to-rename-a-too-long-file-name Note: To rename a file with long file name you can try to rename it by using the short (8+3) windows name. This doesnt work with filenames which start with #+some numbers. Also "dir /x" doesn't seem to work in Win10. https://answers.microsoft.com/en-us/windows/forum/windows8_1-files/file-name-too-long-unable-to-rename-or-delete-the/bbc7419e-23fe-4e09-a381-720eeec9670f

#/bin/bash
#Shorten long file names in folder (c) Denis Trofimov hhttps://github.com/derofim
maxLen="100" # Used to find files with desired length to rename
find . -maxdepth 10 -type f -regextype posix-extended -regex ".{$maxLen,}" |
while read filename
do
extension=".${filename##*.}"
baseName="${filename%.*}"
mv -n "$filename" "${baseName:0:$maxLen}$extension"
echo "Renamed $filename to ${baseName:0:$maxLen}$extension"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment