Page 1 of 1

file extensions

Posted: 04 Apr 2018 07:13
by joejames_786
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

Re: file extensions

Posted: 04 Apr 2018 07:40
by aGerman
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

Re: file extensions

Posted: 04 Apr 2018 08:02
by joejames_786
great steffen that saved my time

thankyou very much 8) :D

Re: file extensions

Posted: 04 Apr 2018 08:15
by batnoob
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
		)		
	)
	
	
)