I have a requirement to remove the space from the folder name in a particular directory. e.g. Project-A is my directory under which have Phase1 Testing, Phase1 Prod, Phase1 UAT subdirectories. I want a batch script which will rename the subdirectories to Phase1Testing, Phase1Prod, Phase1UA names.
Asked
Active
Viewed 3,231 times
2
-
3This isn't a request service, I can guarantee that the answer exists both on this site and if searching using your search engine of choice. Please research, write and test your own code, posting here should that script then not work as expected; good luck. – Compo Sep 25 '17 at 18:30
-
Like he said Compo to you, just make some effort and some search and comes over here with your code if you are still stuck on it ! – Hackoo Sep 25 '17 at 18:39
-
Thanks to both of you after posting my question here I was working on the problem statement and later found a solution which I posted. – Nitin Sep 26 '17 at 08:48
2 Answers
1
you could try something like this:
@echo off
FOR /f "delims=" %%G IN ('dir /ad /b') DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET fname=%%~nxG
SET fname=!fname: =!
rename "%%~nxG" "!fname!"
popd
endlocal
)
You can find more information in: replace_spaces_with_dashes and in spaces_in_file_names
I hope this help you!

Dayana
- 1,500
- 1
- 16
- 29
-1
Thanks @Dayana for the help. Below batch script is working fine and removing the space from the current directory.
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%~na
SET Var=!Var: =!
REN "%%a" "!Var!"
)

Nitin
- 25
- 1
- 8