Reputation: 9150
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.
Upvotes: 45
Views: 111980
Reputation: 105
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.
Upvotes: 0
Reputation: 11
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.
Upvotes: 1
Reputation: 1387
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 subdirectoriesHow to use?
replace.bat
and recursive.bat
in same directory..ext
with desired file extension to match (like .mp4
) in replace.bat
.recursive.bat
file.Upvotes: 4
Reputation: 129
Simple as:
set filename=qwe qwe qwe asd.exe
set filename=%filename: =_%
Upvotes: 12
Reputation: 28
set data=%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2% set data=%data: =0%
Upvotes: 1
Reputation: 1064
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.
Upvotes: 9
Reputation: 7130
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.
Upvotes: 24
Reputation: 70923
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
Upvotes: 72