file extensions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joejames_786
Posts: 17
Joined: 12 Feb 2017 05:11

file extensions

#1 Post by joejames_786 » 04 Apr 2018 07:13

hi

can someone help me solve this problem

I have 1000's of files in folders / sub-folders
they are either jpeg or png or xlsx or docx or doc files.

somehow their names were changed by someone accidentally trying to do something in that system.

ie 20180329jpeg (which should be 20180329.jpeg)
detailsdocx (which should be details.docx)

anynamepng (which should be anyname.png)

this problem applies to all files without a . for the extn

please help..

thanx

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

Re: file extensions

#2 Post by aGerman » 04 Apr 2018 07:40

That may work for you. But try on some examples first before you damage your files even more.

Code: Select all

@echo off &setlocal DisableDelayedExpansion

set "ext3=png xls doc"
set "ext4=jpeg xlsx docx"

set "found="
for /f "delims=" %%i in ('dir /a-d /b /s') do (
  set "p=%%~i"
  set "f=%%~nxi"
  setlocal EnableDelayedExpansion
  for %%j in (%ext4%) do if not defined found if /i "!f:~-4!"=="%%j" if "!f:~-5,1!" neq "." (
    ren "!p!" "!f:~0,-4!.!f:~-4!"
    set "found=1"
  )

  for %%j in (%ext3%) do if not defined found if /i "!f:~-3!"=="%%j" if "!f:~-4,1!" neq "." (
    ren "!p!" "!f:~0,-3!.!f:~-3!"
    set "found=1"
  )
  endlocal
)
Steffen

joejames_786
Posts: 17
Joined: 12 Feb 2017 05:11

Re: file extensions

#3 Post by joejames_786 » 04 Apr 2018 08:02

great steffen that saved my time

thankyou very much 8) :D

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: file extensions

#4 Post by batnoob » 04 Apr 2018 08:15

I spent the last hour and a half coming up with a code that is essentially the same as @aGerman's to answer the question, then i refreshed the page.
anyway, here is the code (it looks almost the exact same)

Code: Select all

@echo off
setlocal enabledelayedexpansion
cd .

set "list=jpeg docx png xlsx doc"
for /f "tokens=*" %%a in ('dir /b /o:n /a-d *.') do (
	set a=%%a
	for %%b in (%list%) do (
		if "!a:~-4!" == "%%b" (
			Ren !a! !a:~0,-4!.!a:~-4!
			
		) else if "!a:~-3!" == "%%b" (
			Ren !a! !a:~0,-3!.!a:~-3!
			
		) else (
			REM
		)		
	)
	
	
)


Post Reply