Thursday, April 22, 2010

Windows Batch Script to remove spaces from filenames

Part of managing Moodle( for me) includes using a Windows Streaming Server to host large media files.
On several occasions, files have been sent with non-web-friendly filenames with spaces et al to be uploaded.

This script fixes that.

This is a windows batch script, which needs to be executed in the folder containing the files, and will replace the spaces and dots in the filenames. Simply copy the script below, name it remove_spaces.bat and execute in the folder needed.

Pretty handy stuff.

Credit to http://www.computing.net/answers/programming/batch-script-to-replace-filenames-h/16301.html

@echo off
setlocal enabledelayedexpansion
for %%j in (*.*) do (
set filename=%%~nj
set filename=!filename:.=_!
set filename=!filename: =_!
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
)

**Updated on 05-07-2012
To remove underscores, use the following in place of lines 5 and 6
 ...
set filename=!filename:.=!
set filename=!filename: =!
...
 -n

38 comments:

  1. Note that this will not work over a network share. Either login via RDP or remote command via pstools or the like.

    ReplyDelete
  2. Went around a few sites, but this one worked. Thanks.

    ReplyDelete
  3. Great, thank-you.

    ReplyDelete
  4. GENIUS! Save me a few hours of renaming. Thanks!

    ReplyDelete
  5. is there a way I could modify this to remove underscores_ as well?

    ReplyDelete
    Replies
    1. In the code where it says "setfilename" remove the underscore before the exclamation point like this:
      ---
      set filename=!filename:.=!
      set filename=!filename: =!
      ---

      Delete
    2. Thank for the update, I'll update the original post.

      Delete
    3. & to remove the character )

      Delete
    4. How I remove characters ( and )?

      Delete
  6. wonderful script work fine! thank you sire !

    ReplyDelete
  7. Thanks for the script.

    ReplyDelete
  8. Awesome! Thanks so much. Youu probably just changed my life! ;-)

    ReplyDelete
  9. thank you! this works great!

    ReplyDelete
  10. Replies
    1. To make the program recursive simply add /r after the for command. Be aware though that any folders that have spaces in the names will be skipped over.

      example:

      @echo off
      setlocal enabledelayedexpansion
      for /r %%j in (*.*) do (
      set filename=%%~nj
      set filename=!filename:.=_!
      set filename=!filename: =_!
      if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
      )

      Delete
  11. if i want this to be done for the entire tree , i mean the sub directories too ?
    please reply ....

    ReplyDelete
    Replies
    1. To make the program recursive simply add /r after the for command. Be aware though that any folders that have spaces in the names will be skipped over.

      example:

      @echo off
      setlocal enabledelayedexpansion
      for /r %%j in (*.*) do (
      set filename=%%~nj
      set filename=!filename:.=_!
      set filename=!filename: =_!
      if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
      )

      Delete
  12. great, it works well

    ReplyDelete
  13. i have used the following code to rename the file name which have a word xxx. But it is saying syntax error kindly suggest me.

    @echo off
    setlocal enabledelayedexpansion
    for %%f in ("*xxx*.*") do (
    set oldname=%%f
    set newname=!oldname:*xxx=Blue!
    ren !oldname! !newname!
    )

    ReplyDelete
  14. Thank you for the information.

    How do you keep the first 5 characters of a file name?

    ReplyDelete
  15. Using the blogger's original script, I modified it slightly to batch change folder names (removing spaces and adding underscores).

    @echo off
    setlocal enabledelayedexpansion enableextensions
    for /d %%j in (*) do (
    set foldername=%%~nj
    set foldername=!foldername:.=_!
    set foldername=!foldername: =_!
    if not "!foldername!"=="%%~nj" ren "%%j" "!foldername!%%~xj"
    )

    ReplyDelete
  16. Thank's a lot. The tool saved me alraedy a lot of time.
    Do you have any idea how to change the code to replace é by e?
    How to modify line 5 or 6:
    set filename=!filename:.=_!
    set filename=!filename: =_!

    ReplyDelete
  17. Great!!

    Thanks a lot !

    ReplyDelete
  18. A b s o l u t e l y perfect. Thé example of good functionality and simplicity. Saves me hours of work. Thanks and a free beer from Belgium !!!
    Filip

    ReplyDelete
  19. Extraordinary !! Thanks

    ReplyDelete
  20. i'm looking in a way to replace spaces with dots,
    would this also be possible ?

    ReplyDelete
  21. The scripts on this post are excellent! Saved me a lot of time from renaming files and directories containing spaces.

    ReplyDelete
  22. Can you post just the code to remove a part of folder name say [2014] and retain the remaining content?

    ReplyDelete
  23. Thanks. Was looking around for a solution to this for a while. Worked like a charm :)

    ReplyDelete
  24. hi can you please tell me how can I replace a '@' to new line?

    ReplyDelete
  25. Works great, use it often when clients send improperly named files, saves me a boatload of time renaming each one manually. Thanks

    ReplyDelete
  26. For those that are not DOS savy you can use the following free win util.

    http://www.1-4a.com/rename/

    (if link does not show go to 1-4a dot com and look for rename utility.

    Cheers :)

    ReplyDelete