none
replacing the space to _ in all files in a folder RRS feed

  • Question

  • Can anyone help me, why ren is giving syntax error? I have few files in allTxtFiles folder , of which, few file names have spaces. I want to replace those spaces with _. Not sure, why ren is not working, though the source and destination are in "".

    @echo off

    setlocal enableDelayedExpansion

    Set "Pattern= "
    Set "Replace=_"

    for /f "delims=" %%v in ('dir /b /s %allTxtFiles%\*.txt') do (
    set "file=%%~v" & rem fetch filename only
    echo "!File:%Pattern%=%Replace%!"
    echo "%%v"
    ren "%%v" "!File:%Pattern%=%Replace%!"

    pause
    )


    endlocal
    exit/B

    Wednesday, February 14, 2018 8:40 PM

Answers