How to replace SPACE from Directory/Sub-Directories
Moderator: DosItHelp
-
- Posts: 20
- Joined: 17 Jul 2012 02:21
How to replace SPACE from Directory/Sub-Directories
I am very New to Batch Script --
---------------------------------------------------------------------------------------------------------------------
As this below script is working fine for file-names -- (replacing space with underscore; recursively)
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /a "*.txt"') DO (
SET Var=%%~na
SET Var=!Var: =_!
REN "%%a" "!Var!.txt"
)
I want the same kind of script for directory/Sub-Directories (recursively) as well ..
---------------------------------------------------------------------------------------------------------------------
If a single script solve both (files and directories) the purpose then it would be great.
---------------------------------------------------------------------------------------------------------------------
This below script is working for directory, but only for current directory not for sub-directories (recursively) ---
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%~na
SET Var=!Var: =_!
REN "%%a" "!Var!"
)
---------------------------------------------------------------------------------------------------------------------
One more script is I am having --
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%A in ('dir /b /s /ad') do (
set f=%%~A
set f=!f: =_! &rem replace space with underscore
if "%%~A" NEQ "!f!" echo ren "%%~A" !f!
)
This script is printing the commands for rename all the directories and sub-directories (recursively)
but when I am removing echo before ren ... and then after running- -
It is giving me an error - The Syntx of the command is incorrect.
---------------------------------------------------------------------------------------------------------------------
..
Please Help me in this way .... Lots of Thanks in Advance...
---------------------------------------------------------------------------------------------------------------------
As this below script is working fine for file-names -- (replacing space with underscore; recursively)
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /a "*.txt"') DO (
SET Var=%%~na
SET Var=!Var: =_!
REN "%%a" "!Var!.txt"
)
I want the same kind of script for directory/Sub-Directories (recursively) as well ..
---------------------------------------------------------------------------------------------------------------------
If a single script solve both (files and directories) the purpose then it would be great.
---------------------------------------------------------------------------------------------------------------------
This below script is working for directory, but only for current directory not for sub-directories (recursively) ---
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%~na
SET Var=!Var: =_!
REN "%%a" "!Var!"
)
---------------------------------------------------------------------------------------------------------------------
One more script is I am having --
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%A in ('dir /b /s /ad') do (
set f=%%~A
set f=!f: =_! &rem replace space with underscore
if "%%~A" NEQ "!f!" echo ren "%%~A" !f!
)
This script is printing the commands for rename all the directories and sub-directories (recursively)
but when I am removing echo before ren ... and then after running- -
It is giving me an error - The Syntx of the command is incorrect.
---------------------------------------------------------------------------------------------------------------------
..
Please Help me in this way .... Lots of Thanks in Advance...
-
- Posts: 20
- Joined: 17 Jul 2012 02:21
Re: How to replace SPACE from Directory/Sub-Directories
Please have a look and reply --
I am unknown to it.. .I have searched on google as well ... but not able to find a solution ..
I am unknown to it.. .I have searched on google as well ... but not able to find a solution ..

-
- Posts: 20
- Joined: 17 Jul 2012 02:21
Re: How to replace SPACE from Directory/Sub-Directories
As you said - I removed - ~n -- like this...
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%a
SET Var=!Var:_= !
REN "%%a" "!Var!"
)
but now it is giving me an error -- the syntax of command is incorrect.
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%a
SET Var=!Var:_= !
REN "%%a" "!Var!"
)
but now it is giving me an error -- the syntax of command is incorrect.
Re: How to replace SPACE from Directory/Sub-Directories
'
Change '@echo off' to '@echo on' and use code tags like
Anyways, the syntax of your command is incorrect !
How about
Where ?pawan26sas wrote:but now it is giving me an error -- the syntax of command is incorrect.
Change '@echo off' to '@echo on' and use code tags like
Code: Select all
input
Code: Select all
output
Anyways, the syntax of your command is incorrect !
Code: Select all
>md dir
>ren dir di\r
De syntaxis van de opdracht is onjuist.
>
Code: Select all
@echo off &setlocal enableDelayedExpansion
for /d /r %%? in (
"*"
) do (
set "$=%%?" &set "$=!$: =_!" &set "$="!$:\=","!"" &set "$=!$:_= !"
for %%? in ( !$! ) do set "$=%%~?"
echo.ren "%%?" "!$!"
)
pause
exit
Code: Select all
ren "F:\ADMIN\REPAIR\...\Nieuwe map" "Nieuwe map"
ren "F:\ADMIN\REPAIR\...\Nieuwe map\Nieuwe_map" "Nieuwe map"
Druk op een toets om door te gaan. . .
-
- Posts: 20
- Joined: 17 Jul 2012 02:21
Re: How to replace SPACE from Directory/Sub-Directories
Nope ...
Its not working...
neither -
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%a
SET Var=!Var: =_!
REN "%%a" "!Var!"
)
nor
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%A in ('dir /b /s /ad') do (
set f=%%~A
set f=!f: =_! &rem replace space with underscore
if "%%~A" NEQ "!f!" ren "%%~A" !f!
)

Its not working...
neither -
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%a
SET Var=!Var: =_!
REN "%%a" "!Var!"
)
nor
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%A in ('dir /b /s /ad') do (
set f=%%~A
set f=!f: =_! &rem replace space with underscore
if "%%~A" NEQ "!f!" ren "%%~A" !f!
)
Re: How to replace SPACE from Directory/Sub-Directories
'
Hmm, ok I took the wrong code you posted. This one does replace the {SPACE} with an underscore.
Anyways, I don't like to be ignored like that so I'll just leave you in your quest, good luck 
Hmm, ok I took the wrong code you posted. This one does replace the {SPACE} with an underscore.
Code: Select all
@echo off &setlocal enableDelayedExpansion
for /d /r %%? in (
"*"
) do (
set "$=%%?" &set "$=!$: =_!" &set "$="!$:\=","!""
for %%? in ( !$! ) do set "$=%%~?"
echo.ren "%%?" "!$!"
)
pause
exit
Code: Select all
ren "F:\ADMIN\REPAIR\...\Nieuwe map" "Nieuwe_map"
ren "F:\ADMIN\REPAIR\...\Nieuwe_map\Nieuwe map" "Nieuwe_map"
Druk op een toets om door te gaan. . .

Re: How to replace SPACE from Directory/Sub-Directories
Code: Select all
@echo off
FOR /f "delims=" %%G IN ('dir /ad /b /s /o-n ^|sort /r') DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET Var=%%~nxG
SET Var=!Var: =_!
rename "%%~nxG" "!Var!"
popd
endlocal
)
Output - Running tree command before and after batch file.
Code: Select all
C:\batch files\Spaces>tree
Folder PATH listing for volume 7_10P
Volume serial number is 549E-2BEB
C:.
├───No_Space
├───Space 1
│ └───Space 11
│ └───No_Space
│ └───Space 111
└───Space 2
└───No_Space
└───Space 22
C:\batch files\Spaces>rem_spaces.bat
C:\batch files\Spaces>tree
Folder PATH listing for volume 7_10P
Volume serial number is 549E-2BEB
C:.
├───No_Space
├───Space_1
│ └───Space_11
│ └───No_Space
│ └───Space_111
└───Space_2
└───No_Space
└───Space_22
C:\batch files\Spaces>
-
- Posts: 20
- Joined: 17 Jul 2012 02:21
Re: How to replace SPACE from Directory/Sub-Directories
Hi "Squashman" and "Ed Dyreen"
Thanks to you both for your quick reply.
These both the script is working fine for me.
Again, Thanks a Lot....
Thanks to you both for your quick reply.
These both the script is working fine for me.
Again, Thanks a Lot....