Page 1 of 1

how to rename files name using batch?

Posted: 02 Mar 2011 21:14
by skunkhead
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 :)

Re: how to rename files name using batch?

Posted: 03 Mar 2011 10:28
by !k

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"
)

Re: how to rename files name using batch?

Posted: 03 Mar 2011 11:22
by aGerman
!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

Re: how to rename files name using batch?

Posted: 03 Mar 2011 12:48
by !k
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"
)