How to remove spaces from file names (in bulk)
Create a powershell file - *.ps1
extension
Write this code:
dir |
Where-Object { $_.name.Contains(" ") } |
Rename-Item -NewName { $_.name -replace " ","" }
save, then right click -> run with powershell
In Windows:
- Open a Command Prompt.
- Go to the folder with the cd command (eg.: cd "paht of your folder").
- Open a powershell by typing: powershell
- Then input this: get-childitem *.mp3 | foreach {rename-item $_ $_.name.replace(" ","")}
Here is a script that can efficiently bulk rename files, stripping all spaces from the name.
:renameNoSpace [/R] [FolderPath]
@echo off
setlocal disableDelayedExpansion
if /i "%~1"=="/R" (
set "forOption=%~1 %2"
set "inPath="
) else (
set "forOption="
if "%~1" neq "" (set "inPath=%~1\") else set "inPath="
)
for %forOption% %%F in ("%inPath%* *") do (
if /i "%~f0" neq "%%~fF" (
set "folder=%%~dpF"
set "file=%%~nxF"
setlocal enableDelayedExpansion
echo ren "!folder!!file!" "!file: =!"
ren "!folder!!file!" "!file: =!"
endlocal
)
)
Assume the script is called renameNoSpace.bat
renameNoSpace
: (no arguments) Renames files in the current directory
renameNoSpace /R
: Renames files in the folder tree rooted at the current directory
renameNoSpace myFolder
: Renames files in the "myFolder" directory found in the current directory.
renameNoSpace "c:\my folder\"
: Renames files in the specified path. Quotes are used because path contains a space.
renameNoSpace /R c:\
: Renames all files on the C: drive.