delete text in filename after character

Microsoft Windows 7 professional 64-bit
August 8, 2010 at 13:19:26
Specs: Windows 7, C2D E6700
I need a batch file that will rename all files after a particular character

Example:

 Filename-1_08_07_2010.dvr-ms
 ABC123_08_07_2010.dvr-ms

renamed to:
Filename-1.dvr-ms
ABC123.dvr-ms

Basically, as soon as the filename contains an Underscore, all text in the filename from that point on should be removed. I messed with the rename command but it does not do what I want, it leaves the files the same.

See More: delete text in filename after character

Report •


#1
August 8, 2010 at 15:57:00

 @echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b ^| find "_"') do (
  set N=%%~Na
  call :sub1
  echo ren %%a !N!%%~Xa
)

goto :eof

:sub1

for /L %%i in (1 1 22) do (
  set S=!N:~%%i^,1!
    if !S! equ _ (
      set N=!N:~0,%%i!
      goto :done
    )
)
:done

goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#2
August 9, 2010 at 10:10:07
That worked perfectly, thanks a million!

Report •

#3
August 10, 2010 at 01:30:56
edit: nevermind, found it! just had to experiment with the file

Report •
Related Solutions


Ask Question