72

How do I delete certain characters or replace certain characters with other characters by some batch file execution, for filenames of all files in a Windows folder in one go, is there a DOS command for that?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
S2S2
  • 8,322
  • 5
  • 37
  • 65

5 Answers5

119

Use PowerShell to do anything smarter for a DOS prompt. Here, I've shown how to batch rename all the files and directories in the current directory that contain spaces by replacing them with _ underscores.

Dir |
Rename-Item -NewName { $_.Name -replace " ","_" }

EDIT :
Optionally, the Where-Object command can be used to filter out ineligible objects for the successive cmdlet (command-let). The following are some examples to illustrate the flexibility it can afford you:

  • To skip any document files

    Dir |
    Where-Object { $_.Name -notmatch "\.(doc|xls|ppt)x?$" } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    
  • To process only directories (pre-3.0 version)

    Dir |
    Where-Object { $_.Mode -match "^d" } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    

    PowerShell v3.0 introduced new Dir flags. You can also use Dir -Directory there.

  • To skip any files already containing an underscore (or some other character)

    Dir |
    Where-Object { -not $_.Name.Contains("_") } |
    Rename-Item -NewName { $_.Name -replace " ","_" }
    
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • what is usage of `Where-Object`. [aphoe answer](http://stackoverflow.com/a/23555996/1275574) do same without `Where-Object` – AminM Dec 21 '14 at 20:20
  • 1
    @JesonPark Thanks for pointing this out. I've added some better examples on `Where-Object` usage. – Ravi K Thapliyal Jan 12 '15 at 06:05
  • 3
    Rename-Item : Source and destination path must be different. error :/ – MERT DOĞAN Mar 05 '15 at 23:46
  • @MERTDOĞAN Rename-Item silently ignores a rename request if the destination path and name remains the same. For e.g., if no spaces to replace were found. It does not throw any errors. If you give us the exact PS commands you used, perhaps we can resolve your issue. – Ravi K Thapliyal Mar 06 '15 at 07:26
  • Did not work for me. I think because I wanted to replace dots. In that case, [this answer](https://stackoverflow.com/a/43090430/2958399) helped me. – Sip Sep 29 '17 at 07:25
  • @MERTDOĞAN See this answer `Get-ChildItem -Recurse | Where-Object {$_.Name -match ' '} | Rename-Item -NewName { $_.Name -replace ' ','' }` found on https://stackoverflow.com/a/8520172 – Modular Aug 04 '19 at 22:03
  • @AminM `Where-Object` is used to filter contents of the dir based on some criteria. – Tropilio Nov 18 '20 at 16:53
  • How can I replace the first _ in a filename? – Bataklik Aug 26 '21 at 13:39
36

A one-liner command in Windows PowerShell to delete or rename certain characters will be as below. (here the whitespace is being replaced with underscore)

Dir | Rename-Item –NewName { $_.name –replace " ","_" }
Community
  • 1
  • 1
aphoe
  • 2,586
  • 1
  • 27
  • 31
22

The PowerShell answers are good, but the Rename-Item command doesn't work in the same target directory unless ALL of your files have the unwanted character in them (fails if it finds duplicates).

If you're like me and had a mix of good names and bad names, try this script instead (will replace spaces with an underscore):

Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace(" ", "_") }
Dustin Malone
  • 342
  • 2
  • 8
  • This solution worked for me (as accepted answer would throw errors, I presume due to a few files not containing the character to be replaced, as you mentioned). +1, I'd be great to have a bit more explanation about the `-Recurse` flag and the `Move-Item`command in this context. – j4v1 Dec 27 '17 at 20:26
  • Also best solution for me. Does easily throw a "is used by another process" error. Closing all my other windows fixed this. Thanks Dustin :) – MartinJH May 28 '18 at 12:56
9

This batch file can help, but it has some limitations. The filename characters = and % cannot be replaced (going from memory here) and an ^ in the filenames might be a problem too.

In this portion %newname: =_% on every line in the lower block it replaces the character after : with the character after = so as it stands the bunch of characters are going to be replaced with an underscore.

Remove the echo to activate the ren command as it will merely print the commands to the console window until you do.

It will only process the current folder, unless you add /s to the DIR command portion and then it will process all folders under the current one too.

To delete a certain character, remove the character from after the = sign. In %newname:z=% an entry like this would remove all z characters (case insensitive).

@echo off
for /f "delims=" %%a in ('dir /a:-d /o:n /b') do call :next "%%a"
pause
GOTO:EOF
:next
set "newname=%~nx1"

set "newname=%newname: =_%"
set "newname=%newname:)=_%"
set "newname=%newname:(=_%"
set "newname=%newname:&=_%"
set "newname=%newname:^=_%"
set "newname=%newname:$=_%"
set "newname=%newname:#=_%"
set "newname=%newname:@=_%"
set "newname=%newname:!=_%"
set "newname=%newname:-=_%"
set "newname=%newname:+=_%"
set "newname=%newname:}=_%"
set "newname=%newname:{=_%"
set "newname=%newname:]=_%"
set "newname=%newname:[=_%"
set "newname=%newname:;=_%"
set "newname=%newname:'=_%"
set "newname=%newname:`=_%"
set "newname=%newname:,=_%"

echo ren %1 "%newname%
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • The edit queue is full so I put the note here: Note that the `dir /a:-d` is to list ONLY files, if you want to process all (files and directories) would be `dir /a` and for process ONLY directories `dir /a:d` Also is missing closing " at the end, that does not affect the code, but my TOC. if you want for example to remove dots in directory names should be: set "newname=%newname:.=%" – DefToneR Jun 24 '22 at 13:40
-3

I would recommend the rename command for this. Type ren /? at the command line for more help.

Endoro
  • 37,015
  • 8
  • 50
  • 63