Solved Trim off characters from middle of filename

August 9, 2011 at 22:20:37
Specs: Windows XP
Hello,

I currently have a folder which contains files such as:

dog rex -2009.pdf
vacation.Summer.jpg

My objective: trim off all characters between the first 2 characters of the filename and the last character, so the result would be like this:

do9.pdf
var.jpg

If possible, I would like to run this script from outside the directory where the files are contained.

I would really appreciate any help.

Thank you


See More: Trim off characters from middle of filename

Report •


#1
August 10, 2011 at 03:25:03
✔ Best Answer
Test this. Change the Pushd path to suit your file setup and remove the REM command when you are satisfied the script will do what you want.

Good luck.

@echo off
cls
setlocal enabledelayedexpansion

pushd %temp%\trial\||echo Pushd failed - job terminated.&&exit /b

for /f "tokens=*" %%1 in ('dir /a-d /b') do (
    set file=%%~n1&set ext=%%~x1
    echo %%1 being renamed !file:~0,2!!file:~-1!!ext!
    REM ren "%%1" "!file:~0,2!!file:~-1!!ext!"
    )


Please come back & tell us if your problem is resolved.


Report •

#2
August 10, 2011 at 07:24:46
This worked! Thanks a million!

Report •

#3
August 10, 2011 at 15:10:40
Thank you for coming back to report your success.

W.


Please come back & tell us if your problem is resolved.


Report •

Related Solutions


Ask Question