1

I am trying to replace spaces in image files and with dashes (-), for example:

this is a file.png

becomes

this-is-a-file.png
ryanjohnsond
  • 393
  • 2
  • 8
  • 19

2 Answers2

4
@echo off

setlocal disableDelayedExpansion

set "root_dir=c:\somewhere"
pushd "%root_dir%"
for %%a in ("* *.png") do (
    setlocal enableDelayedExpansion
    set "f_name=%%~nxa"
    set "cf_name=!f_name: =-!"
    ren "%%~nxa" "!cf_name!"
    endlocal
)
endlocal

Check also this (which in your case will not apply)

Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
3

This code snippet will do

cmd /e:on /v:on /c "for %f in ("* *.txt") do (set "n=%~nxf" & set "n=!n: =-!" & ren "%~ff" "!n!" )"

Input: New Text Document.txt

Output: New-Text-Document.txt

Give credit to This Post

Community
  • 1
  • 1
Rahul
  • 76,197
  • 13
  • 71
  • 125