Skip to content

Instantly share code, notes, and snippets.

@lukasgabriel
Created July 20, 2021 11:19
Show Gist options
  • Save lukasgabriel/c09b0acbfa2f6c59b79c4c177dbfd3c0 to your computer and use it in GitHub Desktop.
Save lukasgabriel/c09b0acbfa2f6c59b79c4c177dbfd3c0 to your computer and use it in GitHub Desktop.
# sortfilenames.py
# Run this command first to get the LongName.txt: dir Y:\ /s /b | sort /+143 /r > LongName.txt
from pathlib import WindowsPath
paths = []
# Make sure to (re-)save the text file as UTF-8 !
with open("LongName.txt", "r", encoding="utf-8") as infile:
for line in infile:
paths.append(WindowsPath(line))
with open("LongName_sorted.txt", "w") as outfile:
for path in paths:
for component in path.parts:
if len(component) > 143:
outfile.write(f"{component.__str__()}\n ---> {path.__str__()}\n\n")
# Prints the offending path or filename first, then its full location underneath.
@totalpackage327
Copy link

totalpackage327 commented Aug 19, 2021

Lukasgabriel,

I am not a programmer and I don't write scripts. However, I desperately need to find a way to locate file names with 143+ characters on my Synology NAS.

I've created the longname.txt file but running your script just generates errors.

Can you provide me with some guidance concerning running your script?

Thanks.

@lukasgabriel
Copy link
Author

lukasgabriel commented Aug 20, 2021

Sure thing!

A few questions first:
What OS are you running & which errors are you getting when running the script?

The command used to generate the LongName.txt should be run on a Windows machine that has access to the NAS (via a network folder for example)
The python script which does additional sorting should also be run on the same machine, in the folder that has the LongName.txt, with Python (ideally 3.8+) installed.

You also need to swap the path (Y:) in my example with the path of the mapped network folder on your NAS.
For example, if you had connected your Network folder to the Drive letter ("D:") and you wanted to search the entire folder, you'd just use this command:
dir D:\ /s /b | sort /+143 /r > LongName.txt
You don't need to create an empty LongName.txt beforehand. However, you might need to change the encoding of the file to UTF-8.

Lastly, my script just builds upon other solutions, which already might be enough to solve your problem.
I wrote the Python script because, for some reason, the original solutions spat out many paths that were in fact not over 143 characters, so my script just works through those and sorts them once again.
You can try these solutions and, if you experience the same issue, come back to my script :)

Here they are:
This one (I think) can be run directly on the NAS or via SSH.

There's also a few great answers for different OSes in this thread, including some that don't require any scripting and stuff.

More here and here.

Hope some of these help :) @totalpackage327

@totalpackage327
Copy link

totalpackage327 commented Aug 20, 2021 via email

@totalpackage327
Copy link

Lukas,

Thank you for your help. All is good now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment