-2

How to replace spaces with dashes of several thousand folders in bulk in Windows server 2008?.

Currently:

My folder

All folders need to become:

My-folder

Thanks

Tarek Ezzat
  • 535
  • 1
  • 5
  • 8

2 Answers2

3

This works here.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D /R %%i IN (*) DO (
    SET "n=%%~nxi"
    REN "%%i" "!n: =-!"
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

Use this batch file:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D /R %%i IN (*.*) DO (
    SET "n=%%~nxi"
    SET n=!n: =-!
    IF NOT "!n!" == "%%~nxi" ECHO MOVE "%%~i" "%%~dpi!n!"
)

Check results and if everything looks OK, remove ECHO before MOVE.

EDIT: Interactive version:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET k=
FOR /D /R %%i IN (*.*) DO (
    SET "n=%%~nxi"
    SET n=!n: =-!
    IF NOT "!n!" == "%%~nxi" (
        ECHO "%%~i" =^> "!n!"
        IF /I NOT "!k!"=="A" SET /P k=[Y]es/[N]o/[A]ll]/[C]ancel?
        IF /I "!k!"=="C" GOTO :END
        IF /I "!k!"=="Y" MOVE "%%~i" "%%~dpi!n!"
        IF /I "!k!"=="A" MOVE "%%~i" "%%~dpi!n!"
    )
)
:END
PAUSE

Test this batch. It will ask before any rename (unless you enter A), so you can preview command and check result.

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46