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

36 thoughts on “Windows Batch Script to remove spaces from filenames”

  1. In the code where it says "setfilename" remove the underscore before the exclamation point like this:

    set filename=!filename:.=!
    set filename=!filename: =!

  2. 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!
    )

  3. 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"
    )

  4. 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"
    )

  5. 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"
    )

  6. 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: =_!

  7. 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

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

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

  10. Thanks. Was looking around for a solution to this for a while. Worked like a charm πŸ™‚

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

Comments are closed.