43

I'm trying to rename all the files inside a folder (all .exe files). I want to replace all the spaces with underscores, e.g. qwe qwe qwe asd.exe to qwe_qwe_qwe_asd.exe.

I need to do this using the command line. I tried a lot of possible solutions I found on internet and even on this site, but I can't make it work.

I also need to do this in "one single line" / "one command", but I'll accept all the working answers.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58

8 Answers8

69

A one liner

cmd /e:on /v:on /c "for %f in ("* *.exe") do (set "n=%~nxf" & set "n=!n: =_!" & ren "%~ff" "!n!" )"

Spawn a cmd instance, with extensions and delayed expansion enabled, and for each exe file with spaces in name, replace spaces with underscores and rename the file with the new name

MC ND
  • 69,615
  • 8
  • 84
  • 126
24

Adapted from here:

https://stackoverflow.com/a/16129486/2000557

@echo off
Setlocal enabledelayedexpansion

Set "Pattern= "
Set "Replace=_"

For %%a in (*.exe) Do (
    Set "File=%%~a"
    Ren "%%a" "!File:%Pattern%=%Replace%!"
)

Pause&Exit

Create a batch file (*.bat) with the above contents. Place that batch file in the folder with all the .exe's and it will replace the spaces with underscores when you run it.

Community
  • 1
  • 1
Gray
  • 7,050
  • 2
  • 29
  • 52
12

Simple as:

set filename=qwe qwe qwe asd.exe
set filename=%filename: =_%
n8b
  • 129
  • 1
  • 2
8

Using forfiles:

forfiles /m *.exe /C "cmd /e:on /v:on /c set \"Phile=@file\" & if @ISDIR==FALSE ren @file !Phile: =_!"

Add /s after forfiles to recurse through subfolders.

Jimadine
  • 998
  • 13
  • 26
  • 2
    In case you want to change spaces to dashes or dashes to underscores the bit that controls the "turn this into that" logic is `Phile: =_!`. Whatever comes before the `=` is what will be replaced. The thing that goes after the `=` is what it will be replaced with. – Daniel Tonon Dec 01 '16 at 01:11
4

Based on @Gray answer, I have extending it to replace filenames recursively in all subdirectories.

File 1: replace.bat

setlocal enabledelayedexpansion

set "pattern= "
set "replace=_"

for %%I in (*.ext) do (
    set "file=%%~I"
    ren "%%I" "!file:%pattern%=%replace%!"
)

File 2: recursive.bat

for /d /r . %%D in (*) do (
    copy replace.bat "%%D\replace.bat"
    cd "%%D"
    replace.bat
    del replace.bat
    cd..
)

Files

  • replace.bat contains script to replace space with underscore
  • recursive.bat contains script to do recursion in all subdirectories

How to use?

  • Save both replace.bat and recursive.bat in same directory.
  • Replace .ext with desired file extension to match (like .mp4) in replace.bat.
  • Double click (run) ONLY recursive.bat file.
Hamza Rashid
  • 1,329
  • 15
  • 22
  • this only works if I manually copy the replace.bat in all subfolders. Which is quite a pain. What should i add to manually copy the replace.bat to all subfolders? – guthik Jul 26 '18 at 14:33
  • please follow **how to use** section – Hamza Rashid Aug 01 '18 at 11:05
  • Mhm, I see so you assume theres only one subdirectory. In my case the directory contains many subdirectories each with a subdirectory with a file I want to rename. Do I still have to copy both the replace and recursive bat files to each of them? – guthik Aug 01 '18 at 11:17
  • Place `replace.bat` and `recursive.bat` in parent directory, it'll work on all child directories. – Hamza Rashid Aug 02 '18 at 13:45
1

set data=%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2% set data=%data: =0%

ricarela
  • 30
  • 3
1

Save the following 2 commands in a .bat file. It will replace " " with "_" in all files and folders, recursively, starting from the folder where the file is stored.

forfiles /s /m *.* /C "cmd /e:on /v:on /c set \"Phile=@file\" & if @ISDIR==FALSE ren @file !Phile: =_!"
forfiles /s /C "cmd /e:on /v:on /c set \"Phile=@file\" & if @ISDIR==TRUE ren @file !Phile: =_!"

Note: First line is doing this for files, and second one is doing this for folders. Each line can be used separately.

Adrien Blanquer
  • 2,041
  • 1
  • 19
  • 31
Alex
  • 11
  • 1
  • This is the only answer that I tried that worked for me; Works on files AND directories; Only needs 1 file, not 2. Not sure why it had zero upvotes or comments...? – Realto619 May 15 '22 at 17:41
0

Minor tweak to Hamza Rashid's answer. Specifically his recursive.bat script.

recursive.bat

set orig=%cd%

for /d /r . %%D in (*) do (
    copy replace.bat "%%D\replace.bat"
    cd "%%D"
    replace.bat
    del replace.bat
    cd %orig%
)

replace.bat stays the same and the instructions stay the same.

DuckCowMooQuack
  • 105
  • 1
  • 12