replace spaces in sub-directory file names with full stops

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
brian28100
Posts: 2
Joined: 12 Sep 2017 09:48

replace spaces in sub-directory file names with full stops

#1 Post by brian28100 » 12 Sep 2017 11:07

Hi All,
I've never created or used batch files before so be gentle with me?

I have a large hard drive containing around 400 sub-directories. Each sub-directory contains two or three files. It is these files I wish to modify. The current sub-directory files have spaces within their file names. The new system I'm moving the files to requires that all file names use full stops as separators rather than spaces. The code below works fine when the batch file is in the same directory as the files. I need it to process the sub-directories too. Any ideas anyone?

My testing directory is on the desktop and called "testing".

Code: Select all

@echo off
setlocal enabledelayedexpansion
    for %%a in ("* *") do (
     set "file=%%a"
     ren "%%a" "!file: =.!"
)
Last edited by brian28100 on 13 Sep 2017 00:22, edited 2 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: replace spaces in sub-directory file names with full stop

#2 Post by aGerman » 12 Sep 2017 11:22

Save this code in the top directory

Code: Select all

@echo off &setlocal DisableDelayedExpansion
for /d %%i in (*) do (
  pushd "%%i"
  for %%j in ("* *") do (
    set "file=%%j"
    setlocal EnableDelayedExpansion
    ren "!file!" "!file: =.!"
    endlocal
  )
  popd
)

Steffen

brian28100
Posts: 2
Joined: 12 Sep 2017 09:48

Re: replace spaces in sub-directory file names with full stops

#3 Post by brian28100 » 13 Sep 2017 00:29

Many thanks Steffen. It worked a treat!

Post Reply