Finding and renaming long file names with R

I have never given it a though, but there is apparently a limit to the length of file names that can be handled by Windows Explorer of Windows XP. I found out when trying to copy my Zotero folder to a computer with Windows XP. I was getting error message of folders that could not be copied, suggesting that my HD was full. I checked a few of these folders and they all contained one or more files with very long file names (>250 characters).

It looked like all these files were non-essential parts of copies of web pages I am keeping in my Zotero database, so I decided to delete them. But with 2000+ folders in my database (each attachment in the Zotero database is kept in a separate folder), I wasn’t going to do this by hand.

I don’t know much about command line scripting, so that was not really an option. Luckily R is actually pretty handy when it comes to dealing with files and folders. It only takes a few lines of code to find and select all files with names longer then certain limit. From there it is easy to delete, move or rename them. In the example below, I am selecting all files with names (including path) longer then 170 characters. After selecting I quickly checked if there is any relevant file (there wasn’t), so I deleted them all.

loc.dir <- paste(getwd(), "/Literature/ZoteroDB/storage", sep="")
refs <- list.files(path=loc.dir, all.files=TRUE, recursive=TRUE, full.names=TRUE)
refs.length <- as.vector(sapply(refs, function(x){nchar(x)}))
refs.loc <- refs[which(refs.length>=170)]

And indeed, after deleting these files, I was able to copy the Zotero database without problem to my other computer :-).

About these ads

3 thoughts on “Finding and renaming long file names with R

  1. mati

    Hi there,

    I faced the same problem with Zotero and was quite happy to find your script. Unfortunately, it doesn’t run with my files. Probably this malfunction is related to some missing parts at the end of the script becuase R reports something like “incomplete last line”. Do you have any idea?

    Thak you for your help,

  2. garrydam

    Nice post.Another software alternative that works is “LongPathTool” for error messages: Cannot read from source file or disk, there has been a sharing violation, cannot delete file or folder, the file name you specified is not valid or too long, the source or destination file may be in use and many other file managing errors.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s