Quickly remove special characters from file names


As of June 24th 2010 a new version (2.2 Beta) is available available. It has the following new features:

  • Special characters could be removed from directory names as well.
  • It could be run in recursive mode that will allow renaming of all files and/or directories in all the sub-directories.
  • Removing all the dots in the file names but the last one that indicates the file extension.
  • The underscore is no longer considered a special character and it is not removed from the file names.


A few years ago I wrote a small application to remove all special characters from the file names of all the files in a directory.

Very often I would get a bunch of files that needed to be posted on a website and most of them would contain all kinds of special characters. I got fed up doing it manually- file by file, so I wrote this small app.

This is a Windows application written in C++ and works with win 98 and up. Of course Linux does not need anything like that, since you can do this with a quick one line shell command.

It only works with ASCII file names (sorry if you use any other language than English). I could have just as easily wrote it for UNICODE, but I had no need for that.

So, I decided to share it with anyone who wants to use it:

New: RenameFiles ver.2.2 b
RenameFiles ver.1.0.

It is just a simple executable and does not need any installation. Keeping it simple is the key here. It also creates a log file in the same directory that the executable is in. The log file keeps track of the original file names and the new file names, so you can always find out what was done.

Thursday, March 19th, 2009 C++, Programming

35 Comments to Quickly remove special characters from file names

  1. What command do you use for removing special characters in Linux? I have a script file I have created that removes and logs anything I need removed from all files in a Directory. It is crude but works great; however, I am always discovering more sophisticated methods and an curious to see what you do in linux.

  2. gray on March 21st, 2009
  3. In Linux (as you probably know) there are a lot of stream and text editing commands like: sed, tr, awk, perl and many others.
    So if you wanted to replace all the special characters of a string with “_” (underscore), you could do:

    sed ’s/[^A-Za-z0-9_.]/_/g’

    Or if you wanted to delete all characters that are not alphanumeric you could do:

    tr -dc ‘[:alnum:]‘

    As I mentioned, there are many other commands for this in Linux.
    Now if you wanted to remove all the special characters of all the files in a directory you could run this simple script:

    for file in *
    do
    mv “$file” $(echo “$file” | sed ’s/[^A-Za-z0-9_.]/_/g’)
    done

  4. dimitar on March 22nd, 2009
  5. Perfect! Much better than I was using :) I was using sed with mv statements but identifying each special character unwanted.

  6. gray on March 23rd, 2009
  7. Hi, i tried your tool it looks like exactly what i need however is there a way to make it work on folder names too and in subdirectories?

    basically i need to remove all special charectors in my my music folder or my mp3 player crashes when loading the folder. thanks

  8. Amin on October 11th, 2009
  9. Hi Amin,

    I made a quick change to the program so it does a recursive renaming of all the files and directories under the folder that you choose. This will do what you needed. Keep in mind that I did this real quick and I have not had time to fully test it yet.
    I will come up with a new version shortly that will be fully tested and will give the user the option to choose whether to do a recursive renaming or not or to touch directories or just files. I will publish it in a new post.
    For now here is the modified application for your needs:

    Rename Files Recursively

  10. dimitar on October 12th, 2009
  11. THANK YOU!! This is AWESOME!!! I cannot thank you enough!!

  12. Rita on November 3rd, 2009
  13. This is a great tool. Is there any chance the selection of characters to be replaced can be tweaked on my end?
    I am trying to upload a ton of folder to SharePoint and need to remove all of the “.” and “&” but want to keep the spaces. When I run the tool, it removes all of the “&” but I am still stuck with the “.” from “Co.” in many of the docs names.
    Thanks
    Stevens

  14. Stevens on November 17th, 2009
  15. @Stevens

    Unfortunately you cannot tweak it on your end. It is a compiled C++ code.
    The only way you can do this is if you decompile, change the code and recompile but that is the long way to go about it.

    One of these coming days/weeks I will add to the program the ability to select whether you want to preserve the spaces or replace them with under scores.
    I just have to find a little spear time…
    This will solve your problem with the spaces.

    About the “.” – Sorry if I am answering your question with a question but why do you want to remove them?
    They are not really considered special characters in the file/directory names.
    Since you will be doing this for a SharePoint application I assume this is a Windows server. Windows does not care how many dots you have, it just takes whatever is after the last one to determine the file type.

  16. dimitar on November 18th, 2009
  17. the dots or periods need to be removed in order to upload the documents to Windows SharePoint. I’m guessing it is because of the MS SQL database that runs SharePoint. I have tested it on a document and it fails when the dot is in the name. example:
    “SweetWater Brewing Co..docx” – Fails
    “SweetWater Brewing Co.docx” – Uploads

    Thanks for the quick response!

  18. Stevens on November 19th, 2009
  19. @Stevens

    I modified the program to do what you needed. It will check for any special characters including “.” in the file/directory names disregarding the last “.” and anything after it. For example a file Co.$.R.dat will be renamed to CoR.dat.
    I am assuming that the part after the last “.” is correct in your file names.
    If you want to preserve the spaces, make sure to uncheck “Clear White Spaces” box.

    Please note: I have not had the time to do an extensive testing. Please run against test data before using it on production data. Use at your own risk.

    Here is the modified program.

  20. dimitar on November 20th, 2009
  21. That was perfect!! I was able to easily rename the folders, sub-folders,and file names to import them to SharePoint and MS OfficeLive. Thank you, you Rock!
    -Stevens

  22. Stevens on December 9th, 2009
  23. Thanks for posting this tool . . . it really helped me when I needed it.

  24. Wayne on December 30th, 2009
  25. I’m sad it not process unicode filename as the special characters I want to remove are unicodes one ! (japenese charset that old apps can’t process)

  26. rag on February 20th, 2010
  27. Hi

    I am not a programmer and do not necessarily understand all aspects.

    I tried your little program and it solves a big problem I have at present. I have many hundreds of file to upload to a sharepoint site with chracters that are not allowed.

    However after processing the files name look a real mess as some have several charaters removed. For some reason the file order changes totally. The first part is normally numeric. Is it possible to leave a space rather than just removing the characters.

    Thanks

  28. Peter Roffey on March 29th, 2010
  29. @Peter,

    Did you check out the program I posted in my comment above from November the 20th, 2009?

    Another person asked me to make some changes because he was using it to upload files to a SharePoint server as well.

    What you are noticing about the change of the order of the files is a normal behavior. Your files are normally ordered alphabetically. So when you take special characters out they will be reordered in the directory. Even if you replace the special characters with spaces you will still get the files reordered.

    For example if you have 2 files, one is called w2#b.txt and the other one is called w2$a.txt, windows will order them with the w2#b.txt file before the w2$a.txt file.

    Now, if you remove the special characters, then the w2a.txt file will be before the w2b.txt file. This is the way it should be, unless you somehow change the alphabet :-) Likewise, if I were to replace the special characters with blank spaces, you will end up with the same result: the w2 a.txt file will be before the w2 b.txt file.

    I hope this is clear enough and makes sense.

  30. dimitar on March 30th, 2010
  31. Hi
    I have tried running your program to change my filenames – but it just keeps giving me an error

    ————–ERROR——————Current directory: E:\Folder\Music\pop\boFile Who Do You Love?.mp3 not found, cause = 3

    Windows will not let me rename or delete this file.

    Any help would be appreciated.

  32. Jasper on April 16th, 2010
  33. @ Jasper,

    If Windows does not let you rename or delete it most likely it is in use by some process.

    Use the Windows Process Explorer to find out what process it is:

    http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

  34. dimitar on April 17th, 2010
  35. Hi,

    The file is not in use by any process, I have even tried deleting the file in safe mode.

    When I attempt to delete the file I get the error message:-

    Cannot delete Who Do You Love?: The filename, directory name, or volume label syntax is incorrect.

    If I try to rename the file I get the error message:-

    Cannot rename file: Select only one file to rename, or use MS-DOS wildcards (for example, *.txt) t rename a group of files with similar names.

    Since I am only selecting one file, the only file in the directory, I am unsure what I can do here – as typing an ‘*’into the filename is not allowed by windows.

    I have unsuccessfully tried various file deletion programs, such as malwarebytes file Assassin, which tells me the file has been successfully deleted, even though it has not.

  36. Jasper on April 18th, 2010
  37. @ Jasper,

    This discussion is beyond the scope of the program I posted to mass rename files.

    What the Windows message suggested is not putting * in the file name. Rather opening a command prompt then cd into the directory containing the file and then execute:

    del *.mp3

    Keep in mind that the above command will delete all the mp3 files in the directory.

    If you want to be more specific you can do:

    del Who*.mp3

    This will delete any file that starts with “Who” and ends with “.mp3″

  38. dimitar on April 18th, 2010
  39. I have previously tried lots of different ways of renaming or deleting this file using a command prompt – always with the same error message – Cannot delete Who Do You Love?: The filename, directory name, or volume label syntax is incorrect.

    It seems the question mark in the filename has thrown a spanner in the works and now I can’t rename or delete the file – even using a program like yours which claims to “Quickly remove special characters from file names”

    If this was a question of just one file – I would probably ignore it – but I have another directory with many many more files – all with the same problem – a special character in the filename prevents windows from recognising the file as a file – this in turn stops windows error checking tool checking the disk for errors.

    Thanks anyway for your help – I am sure I will find a solution eventually.

    Sincerely Jasper

  40. Jasper on April 18th, 2010
  41. @ Jasper,

    The program does what it claims. It quickly removes special characters from file names and directories. It is designed to save you time if you have a lot of files you need to go through.

    It does not however fix any issues you may have with your PC. If you cannot rename the file(s) manually (even in safe mode) this program will not be able to do it either.

  42. dimitar on April 18th, 2010
  43. I do not have any issue with my pc – My problem is that some of my filenames have special characters – and so cannot be renamed or deleted by windows.

    I therefore looked for a program that claimed it could rename files that had special characters in them, which seemed like an intelligent thing to do.

    I can only imagine that what I see as a ‘?’ in the filename is actually a non ASCII character, something Chinese or whatever – and so it is not recognised by either windows or your program – I therefore am unable to rename, access or delete the file.

    Anyway, as I said before – thanks anyway for your help.

    Jasper

  44. Jasper on April 18th, 2010
  45. @ Jasper…

    My program only reads ASCII, if I ever have some time left I will add support for Unicode…

    In your case there must be something more than just a non ASCII character, otherwise I would think that the wild card (*) should have taken care of it.

    Have you tried booting into Linux (from a Live CD), then mounting the partition and deleting the files?

  46. dimitar on April 18th, 2010
  47. Wow. What a timesaving tool! Thanks for sharing. We appreciate it. I’m preparing to move a document library stored on a server over to SharePoint and this will really help!

  48. cindyfulcher on April 26th, 2010
  49. Hi,
    I tried your RenameFiles2.1b.exe program in filenames using characters of languages like German Turkish or French or Greek (mp3 files). The program when tried to rename such files it gives an error cause = 2. Is there any chance to modify the program just to change the non ASCII unicode characters to normal characters? Could that be possible also for directory names?
    If I understood well the programm in case of a pathname including a directory name in Greek for example it cannot operate. Is that correct?

    Thanks
    Spyros

  50. Spyros on April 30th, 2010
  51. @ Spiros,

    You are correct. The program only supports ASCII.

    In order for the program to support multiple languages, I have to use Unicode instead of ACII. I cannot promise when I will have the time to do this.

    Keep in mind that this will take more time than it might look like at first. I have to identify what a is “special character” what is not for each language/alphabet.

  52. dimitar on April 30th, 2010
  53. Thanks for the prompt reply.
    Is there any chance by defining in your program an external configuration file, every interested user to define the special characters and their replacement. So the program would be configurable!

  54. Spyros on May 4th, 2010
  55. @ Spyros,

    Having an external config file is not a problem and that is a good idea.
    The program will still have to be changed to utilize Unicode characters though.

    I cannot promise when I will be able to get to it…

  56. dimitar on May 10th, 2010
  57. Thanks – a very useful tool!!
    Did 95% of the job cleaning up mp3 filenames. Only the non-ASCII characters failed. I’ll do them manually until a prog upgrade is availible.
    Thanks again:)

  58. glen on May 20th, 2010
  59. This is a great tool! Is there a way for it not to remove underscores (_) or other characters that are allowed for SharePoint?

  60. Eric on June 21st, 2010
  61. @Eric,

    I published a new version of the application- 2.2b. It will work for you. Check the post above.

  62. dimitar on June 24th, 2010
  63. thank you !
    i made an app which at some point created some files in xp with the character “:” in them; files of 0K in size that could not be deleted, but could be copied along with everything else making more trouble for me.
    your app just vanished those files, not renamed them, and i’m more than happy with that.
    just for reference, those files were created from jvm; you may think it should be impossible to create a file that cannot be accesed, deleted but can be copied and spread like a plague, yet windows is something of a pandora’s box
    thanks yet again :)

  64. Alin on July 20th, 2010
  65. Do you have a program to undo this based on log file? It worked as intended however it renamed some important files that need their original names to function. Would go through and do it manually but its 300+ files i need renamed back.

    Thanks.

  66. John on July 28th, 2010
  67. Nvm i fixed it myself using this mirc script:

    alias test2 {
    var %i = 1, %oldf
    while (%i <= $finddir(j:\,*,0)) {
    %f = $finddir(j:\,*,%i)
    %oldf = $read(c:\properlog.txt,w,* $+ $mid(%f,4) $+ *)
    %oldf2 = $read(c:\properlog.txt,$calc($readn – 2))
    if (%oldf2 && $right(%oldf2,1) == \) {
    rename %f " $+ %oldf2 $+ "
    ;echo -a " $+ %oldf2 $+ "
    }
    inc %i
    }
    }

  68. John on July 28th, 2010
  69. @John,

    That is the thing with programs like this… you have to be extra careful to make sure you do not rename things that should not be renamed. Extra caution is required especially when using the recursive function. That is why I did the log file! BTW… I have renamed the wrong files myself before… It happens.

    Also, your choice of scripting language surprised me. I do not think that mIRC would have been my first choice. But… as long as it works…

  70. dimitar on July 29th, 2010

Leave a comment

Search

 

Archive

July 2010
M T W T F S S
« Jun    
 1234
567891011
12131415161718
19202122232425
262728293031  

Other