0

I have several folders that have files with double file extensions along with regular file extensions. I need to create a batch script to search all the folders and remove the last extension with any files that have double extensions. None of the file extensions are consistent.

Here's an example

C:\test\regular.exe
C:\test\picture.jpg.doc
C:\newtest\document.doc.pdf

End Result I need

C:\test\regular.exe
C:\test\picture.jpg
C:\newtest\document.doc
Brian
  • 319
  • 1
  • 5
  • 13

2 Answers2

1

Try this and remove the echo, if the output is OK:

@echo off &setlocal
for /r \ %%i in (*) do (
    for %%j in ("%%~ni") do if "%%~xj" neq "" echo ren "%%~fi" "%%~nj"
)

Edit: added support for the entire HD.

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Ooh - so close! But the destination name should be `%%~ni` or `%%~nj` (which should be the same) - `%%~Fj` will produce a syntax error with `REN` (but should be OK with `MOVE`) – Magoo May 10 '13 at 17:13
  • 1
    @Peter - Oh, I always forget this `ren to full name syntax error` `:)`. – Endoro May 10 '13 at 17:21
1
@ECHO OFF
SETLOCAL

SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
 FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF NOT EXIST "%%~dpni" ECHO REN "%%~fi" "%%~ni"
 FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" ECHO CAN NOT REN "%%~fi" "%%~ni"
)

GOTO :EOF

This batch should accomplish the task.

For each file in the tree rooted at sourcedir, if the NAME of the file itself contains an 'extension' and the filename without the original extension does not exist, then rename the file. That way, if ...picture.jpg.doc is found, the rename should occur only if ...picture.jpg does not exist.

The command to rename is simply ECHOed. You'd need to remove the ECHO keyword to activate the rename - after verifying that's what you want to do.

I've added a second line to report that a rename could not be done because of an existing file.. This could be done very slightly better, but it will work.


Revised to modify name in case simple rename can not be done.

Caution - this version will rename immediately - there are no ECHOes to provide a list first because it's nonsense to provide such a list when renaming a file may produce different results on the main rename run.

@ECHO OFF
SETLOCAL

SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
 FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" (
  SET renreq=Y
  FOR %%a IN (new alt extra another 1 2 3 4 5 6 7 8 9) DO IF DEFINED renreq (
   IF NOT EXIST "%%~dpi%%~nn_%%a%%~xn" (
    REN "%%~fi" "%%~nn_%%a%%~xn"
    SET "renreq="
   )
  )
  IF DEFINED renreq ECHO CAN NOT REN "%%~fi"
 ) ELSE (
 REN "%%~fi" "%%~ni"
 )
)

GOTO :EOF

Reasonably obviously, the list of "extras" can be extended if required.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I have a few examples where it cannot rename the file because there is already a file that exists. In those cases is there a way to rename it to something else? Example: File filename.doc already exists and the batch file tries to rename filename.doc.sh is there a way to rename it like filename_new.doc? I tried a few things but my syntax isn't correct. – Brian May 10 '13 at 20:06