Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch File to replace underscores with spaces in a filename

I am trying to replace underscores in some file names with spaces, for example:

this_is_a_file.pdf

becomes:

this is a file.pdf

In Windows using a batch file.

I have found a similar question, but it replaces spaces with nothing: How to remove spaces from file names (in bulk)

Can it be easily translated to do what I want?

like image 824
Carolined Avatar asked Nov 19 '25 01:11

Carolined


1 Answers

Use %file:_= % to represent %file% with underscores replaced with spaces. Unfortunately this won't work on a for variable so if you're looping over files you have to use an intermediate variable.

@echo off
setlocal enabledelayedexpansion
for %%a in (*_*) do (
  set file=%%a
  ren "!file!" "!file:_= !"
)
like image 141
Neil Avatar answered Nov 21 '25 15:11

Neil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!