Page 1 of 1

How to replace SPACE from Directory/Sub-Directories

Posted: 23 Jul 2012 03:55
by pawan26sas
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...

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

Posted: 23 Jul 2012 04:30
by pawan26sas
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 .. :(

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

Posted: 23 Jul 2012 06:19
by Squashman
Remove ~n

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

Posted: 23 Jul 2012 06:28
by pawan26sas
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.

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

Posted: 23 Jul 2012 07:04
by Ed Dyreen
'
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. . .

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

Posted: 23 Jul 2012 07:13
by pawan26sas
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!
)

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

Posted: 23 Jul 2012 07:29
by Ed Dyreen
'
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:

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

Posted: 23 Jul 2012 08:42
by Squashman

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>

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

Posted: 24 Jul 2012 00:08
by pawan26sas
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....