how to rename files name using batch?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
skunkhead
Posts: 1
Joined: 02 Mar 2011 21:08

how to rename files name using batch?

#1 Post by skunkhead » 02 Mar 2011 21:14

Hi,

i have some files in a folder. I need to create a .bat file which will rename all the files to different name. The files name will be in a format like "xxx20100918xxx.doc" and I need to rename it to 20100918.doc.

is it possible to do it in batch?Any suggestions :)

Thanks,
Skunkhead :)

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: how to rename files name using batch?

#2 Post by !k » 03 Mar 2011 10:28

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /b ??????????????.doc') do (
set "name=%%~nf"
ren "%%f" "!name:~3,8!%%~xf"
)
Last edited by !k on 03 Mar 2011 11:55, edited 1 time in total.

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

Re: how to rename files name using batch?

#3 Post by aGerman » 03 Mar 2011 11:22

!k is right in case the file name has always 14 characters and the date starts after the 3rd. Otherwise you should define a rule how to extract the date and tell us what these triple x's are for.

Regards
aGerman

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: how to rename files name using batch?

#4 Post by !k » 03 Mar 2011 12:48

ver.2

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "year=2010"
for /f "delims=" %%f in ('dir /b *%year%*.doc ^|^
  findstr /rc:".*[^0-9]%year%[0-9][0-9][0-9][0-9][^0-9].*"'
  ) do (
  set "name=%%~nf" &set "newname="
  for /l %%c in (0,1,255) do (
    set "char=!name:~%%c,1!"
    if "!char!" GEQ "0" if "!char!" LEQ "9" set "newname=!newname!!char!"
  )
  ren "%%f" "!newname!%%~xf"
)

Post Reply