Hi,
I have these files in a folder ab and I want to replace the spaces in the file name to _.
XXXX_acc-er-in.pdf
xxxx_can mess comp-en-us.pdf
dddd_ee_le_ang-er-in.pdf
cccc_Rel File ope-in-er.pdf
xxx_etiti File ope-in-er.pdf
So it would be
XXXX_acc-er-in.pdf
xxxx_can_mess_comp-en-us.pdf
dddd_ee_le_ang-er-in.pdf
cccc_Rel_File_ope-in-er.pdf
xxx_etiti File_ope-in-er.pdf
Need help.
Can anyone please see, what's wrong in this? Ren is showing incorrect syntax.
Set "Pattern= "
Set "Replace=_"
For /f "delims=" %%a in ('dir /b /s %allTxtFiles%\*.txt') Do (
Set "File=%%~a"
echo "!File:%Pattern%=%Replace%!"
echo "%%a"
Ren "%%a" "!File:%Pattern%=%Replace%!"
)
How to replace space in file names
Moderator: DosItHelp
Re: How to replace space in file names
How do SETLOCAL and ENABLEDELAYEDEXPANSION work?
Code: Select all
@echo off
Set "Pattern= "
Set "Replace=_"
Setlocal EnableDelayedExpansion
For /f "delims=" %%a in ('dir /b /s "*.txt"') Do (
Set "File=%%~nxa"
echo Ren "%%a" "!File:%Pattern%=%Replace%!"
)
pause
-
- Posts: 51
- Joined: 10 Jan 2018 15:21
Re: How to replace space in file names
Thank you a lot.
-
- Posts: 14
- Joined: 16 Oct 2017 20:15
Re: How to replace space in file names
A way to replace those characters in variables is like so:
From there you could have a for command rename all those files.
Code: Select all
set a=%a: =_%