Can someone write me a batch file to replace full stops in filenames, please?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Can someone write me a batch file to replace full stops in filenames, please?

#16 Post by Aacini » 24 Nov 2016 18:28

Kerr Avon wrote:... can I request someone to create two new batch files, please. Both should do largely the same thing, but the second one is more selective of the full stops it replaces. The two batch files I'd like would be:


1) A batch file that replaces all full stops (or periods, as American's say) in the filenames with a space, EXCEPT for the final full stop, since of course that final one marks the beginning of the file's extension.

2) The same as in number 1, except that it doesn't replace any full stop that's between two numerical characters, or that follows the letter 'v' or the word 'version'. This is so that the date or version information in the filename is not altered.


The Batch file below do previous points:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "digits=0123456789"
for %%a in (*.*) do (
   set "in=%%~Na"
   if "%1" neq "2" (
      set "out=!in:.= !."
   ) else (
      set "out="
      set "leftDigit="
      for /F "delims=" %%b in (^"!in:.^=^
% Do NOT remove this line %
!^") do (
         set "part=%%b"
         if defined leftDigit (
            for /F %%d in ("!part:~0,1!") do if "!digits:%%d=!" neq "%digits%" set "out=!out:~0,-1!."
            set "leftDigit="
         )
         set "char= "
         for /F %%d in ("!part:~-1!") do (
            if "%%d" equ "v" (set "char=."
            ) else if "!part:~-7!" equ "version" (set "char=."
            ) else if "!digits:%%d=!" neq "%digits%" set "leftDigit=1"
         )
         set "out=!out!%%b!char!"
      )
   )
   ECHO ren "%%a" "!out:~0,-1!%%~Xa"
)

The same Batch file do both processes: when executed with 2 as parameter, execute the 2) part; otherwise, execute the 1) part. For example:

Code: Select all

C:\Users\Antonio\Tests> test.bat
ren "catgen.v1.0.zip" "catgen v1 0.zip"
ren "win.dirreplac.v.3.rar" "win dirreplac v 3.rar"
ren "t.c.s.2012.08.05.zip" "t c s 2012 08 05.zip"
ren "win.n64check.version.4.25.zip" "win n64check version 4 25.zip"
ren "winut.tshv1.0.zip" "winut tshv1 0.zip"
ren "version.05.gfx.utilty.tsk.rar" "version 05 gfx utilty tsk.rar"

C:\Users\Antonio\Tests> test.bat 2
ren "catgen.v1.0.zip" "catgen v1.0.zip"
ren "win.dirreplac.v.3.rar" "win dirreplac v.3.rar"
ren "t.c.s.2012.08.05.zip" "t c s 2012.08.05.zip"
ren "win.n64check.version.4.25.zip" "win n64check version.4.25.zip"
ren "winut.tshv1.0.zip" "winut tshv1.0.zip"
ren "version.05.gfx.utilty.tsk.rar" "version.05 gfx utilty tsk.rar"

Antonio

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Re: Can someone write me a batch file to replace full stops in filenames, please?

#17 Post by Kerr Avon » 25 Nov 2016 07:28

Pieh-ejdsch, that works great, thanks.

Aacini, that also works great (with and without the '2' parameter), thanks.

Now I'm spoilt for choice as to which I will use :D

Post Reply