0

I have a large number of folders on a drive (+20.000) And I like to have them ordered by name. but some folders are using "." (points) instead of " " (Spaces).

So I would like to create a .bat file that I drop in that certain folder (which contains the +20k folders).

When executed it will change all de points to spaces in the names of the folders.

Some difficulties:

  • Not the files (obviously).
  • And not the folders underneath.

f.e.

BEFORE

  • Movies
    • Iron.Man.2.(2018)
    • Iron Man 1 (2018)
    • unharmed.file.mp3
    • change.bat

AFTER

  • Movies
    • Iron Man 1 (2018)
    • Iron Man 2 (2018)
    • unharmed.file.mp3
    • change.bat

Can somebody help me with this or point me in the right direction? Thanks.

2 Answers2

0
@echo off
    setlocal enableextensions disabledelayedexpansion

    Rem Check for presence of parameter
    if not exist "%~f1" goto :eof

    Rem For each folder under the indicated one
    for /d %%a in ("%~f1\*") do (
        Rem Retrieve folder name and extension
        set "folderName=%%~nxa"
        Rem Delayed expansion needed to access the folderName variable
        setlocal enabledelayedexpansion
        Rem Get the folderName value without dots and store it in the
        Rem for replaceable parameter
        for %%b in ("!folderName:.= !") do (
            Rem disable the activation of delayed expansion
            endlocal 
            Rem If the target name does not exist, do the rename
            if not exist "%%~dpa\%%~b" (
                echo ren "%%~fa" "%%~b"
            )
            Rem The variable will be used as a switch to determine
            Rem if the outer setlocal has been ended with the
            Rem inner endlocal
            set "folderName="
        )
        Rem If the last setlocal has not been ended, end it
        if defined folderName endlocal
    )

Without comments

@echo off
    setlocal enableextensions disabledelayedexpansion

    if not exist "%~f1" goto :eof

    for /d %%a in ("%~f1\*") do (
        set "folderName=%%~nxa"
        setlocal enabledelayedexpansion
        for %%b in ("!folderName:.= !") do (
            endlocal 
            if not exist "%%~dpa\%%~b" (
                echo ren "%%~fa" "%%~b"
            )
            set "folderName="
        )
        if defined folderName endlocal
    )
MC ND
  • 69,615
  • 8
  • 84
  • 126
0

Here's a simple solution, just run it in the directory you want to fix the names on.

@echo off
setlocal ENABLEDELAYEDEXPANSION
for /d %%a in (*) do (
    set dirName=%%a
    echo ren "%%a" "!dirName:.= !"
)
Scott C
  • 1,660
  • 1
  • 11
  • 18