How to replace names recursively through a Windows batch operation

I want to process a batch operation in a large directory. Actually I have a script package for this process. But here I have a problem. Some directory names, file names contain "(space). Thus, in a batch operation, these names are passed as 2 arguments. And these lines will not work. Therefore, I want to rename" "with" _ "to overcome this problem.

Example:

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

this gives an error. below works fine

process / MyDirectory / Ola and Me / Private / TopSecretPictures

My goal: convert | Ola and Me | → | Ola_And_Me recursively

:)

thanks in advance.

+3
2

script , , .

spaces_to_underscores.bat :

@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

:

spaces_to_underscores "My Directory"

My Directory
    Ola and Me
        Private
            TopSecretPictures

"Ola and Me" "Ola_and_Me", , "Photo 001.jpg" "Photo_001.jpg". "My Directory" .

: script Windows, "C:\Documents and Settings" "C:\Program Files" "My Documents" "Application Data". . , .

+6

, exapansion, . , cmd.exe /v:

cmd.exe /v

, script %% :

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

*** Vauge... *** for, :

  • %var:str1=str2%
  • !var! %var%

-: ... , ( - Microsoft ) , script: script :

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

'T' for , (...). . , , ..! Var! % var%. .

set T=!S: =_! ( , T to S, " S " _"). set T=%S: =_%.

+2

Source: https://habr.com/ru/post/1720902/


All Articles