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

@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: =!