How to replace SPACE from Directory/Sub-Directories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pawan26sas
Posts: 20
Joined: 17 Jul 2012 02:21

How to replace SPACE from Directory/Sub-Directories

#1 Post by pawan26sas » 23 Jul 2012 03:55

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...

pawan26sas
Posts: 20
Joined: 17 Jul 2012 02:21

Re: How to replace SPACE from Directory/Sub-Directories

#2 Post by pawan26sas » 23 Jul 2012 04:30

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 .. :(

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: How to replace SPACE from Directory/Sub-Directories

#3 Post by Squashman » 23 Jul 2012 06:19

Remove ~n

pawan26sas
Posts: 20
Joined: 17 Jul 2012 02:21

Re: How to replace SPACE from Directory/Sub-Directories

#4 Post by pawan26sas » 23 Jul 2012 06:28

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to replace SPACE from Directory/Sub-Directories

#5 Post by Ed Dyreen » 23 Jul 2012 07:04

'
pawan26sas wrote:but now it is giving me an error -- the syntax of command is incorrect.
Where ?

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.

>
How about

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. . .

pawan26sas
Posts: 20
Joined: 17 Jul 2012 02:21

Re: How to replace SPACE from Directory/Sub-Directories

#6 Post by pawan26sas » 23 Jul 2012 07:13

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!
)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to replace SPACE from Directory/Sub-Directories

#7 Post by Ed Dyreen » 23 Jul 2012 07:29

'
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. . .
Anyways, I don't like to be ignored like that so I'll just leave you in your quest, good luck :wink:

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: How to replace SPACE from Directory/Sub-Directories

#8 Post by Squashman » 23 Jul 2012 08:42

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>

pawan26sas
Posts: 20
Joined: 17 Jul 2012 02:21

Re: How to replace SPACE from Directory/Sub-Directories

#9 Post by pawan26sas » 24 Jul 2012 00:08

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....

Post Reply