How to replace SPACE from Directory/Sub-Directories
Posted: 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...
---------------------------------------------------------------------------------------------------------------------
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...