views:

366

answers:

2

Hi all,

I want to process a batch operation in a big directory. Actually I have the batch script for that process. But here , I have a problem. Some of the directory names, files names contain " " (space character). So in the batch operation this names passed as 2 arguments . and those lines will not work. So Iwant to rename " " with "_" to overcome this problem .

Example:

process /MyDirectory/Ola and Me/Private/TopSecretPictures/

this gives error. the below one works fine

process /MyDirectory/Ola and Me/Private/TopSecretPictures

My aim is: convert | Ola and Me |>> |Ola_And_Me recursively

:)

thanks in advance ..

+1  A: 

You can do this in a batch file if you use a feature called "delayed exapansion" that isn't on by default. To switch it on, you need to start cmd.exe with the /v switch:

cmd.exe /v

Once this is on, the following batch script will replace all spaces in %%i with underscores, and spit the result out:

for /f "usebackq tokens=*" %%i in (`dir /b`) do (
    set S=%%i
    set T=!S: =_!
    echo !T!
)

Vauge description...Excluding the for loop itself, the interesting parts of this are:

  • String substitution using the %var:str1=str2% syntax
  • Delayed expansion using !var! instead of %var%

First: delayed expansion... without this, the command interpreter (for whatever reason Microsoft decided to code it as) will evaluate all the parameters first, and then run the script: so this version of the script does NOT work:

for /f "usebackq tokens=*" %%i in (`dir /b`) do (
    set S=%%i
    set T=%S: =_%
    echo %T%
)

With this version the variable 'T' is set to the last value of the for loop before the contents of the (...) block actually execute. Which makes no sense to me. So with delayed execution enabled, we can use the delayed execution variable marks, i.e., !var! rather than %var%. Which gives us the right result.

The other clever bit then is the set T=!S: =_! (which basically says set T to S, replacing every '' ' in S with ''*). Without delayed expansion, this would be written set T=%S: =_%.

Chris J
+2  A: 

The following script renames all files and directories recursively, starting at a given directory, converting spaces to underscores.

spaces_to_underscores.bat source:

@echo off
setlocal

for /r "%~1" %%t in (.) do (
   for /f "usebackq tokens=*" %%f in (`dir /b/a-d "%%~t" 2^>nul:`) do (
      call :proc "%%~f" "%%~t"
   )
   for /f "usebackq tokens=*" %%d in (`dir /b/ad "%%~t" 2^>nul:`) do (
      call :proc "%%~d" "%%~t"
   )
)
exit /b 0

:proc
   set fn=%~1
   if "%fn: =_%"=="%fn%" exit /b 0
   set fn=%~2\%fn: =_%
   move "%~2\%~1" "%fn%" >nul:
exit /b 0

Usage:

spaces_to_underscores "My Directory"

Given this directory structure

My Directory
    Ola and Me
        Private
            TopSecretPictures

it will rename the folder "Ola and Me" to "Ola_and_Me", and also rename any files such as "Photo 001.jpg" to "Photo_001.jpg". The starting directory "My Directory" will not be renamed.

WARNING: Do not run this script on standard windows directories, such as "C:\Documents and Settings" or "C:\Program Files" or "My Documents" or "Application Data". There is no "undo" functionality here. Make sure you have a backup.

system PAUSE