Solved Remove 33 characters from each filename

June 24, 2011 at 10:57:56
Specs: Windows 7
Hi,

DB backup stores the backup by adding date/time stampo after the DB name, so each db would be named as DBName_backup_2011_06_24_000101_0168612.bak.

I do have a script that trims the characters from the beginning of the filename, but couldnt get one to trim the 33 characters from the end.

Any help would be appreciated. Thanks in advance.

Thanks
Mush


See More: Remove 33 characters from each filename

Report •


#1
June 24, 2011 at 21:01:58
✔ Best Answer
Try this - totally untested. Delete REM if satisfied with output. Amend the pushd path to suit.

@echo off
cls
setlocal enabledelayedexpansion


pushd c:\trial\||echo Pushd failed - job terminated.&&exit/b

for /f "tokens=*" %%A in ('dir /b /a-d') do (
    set oldname=%%A
        for /f "tokens=1-8 delims=_." %%1 in ("!oldname!") do (
            set newname=%%1.bak
            )
    echo oldname=!oldname!   newname=!newname!

    REM ren "!oldname!" "!newname!"
)

popd



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


Report •

#2
June 28, 2011 at 12:58:04
Hey Wahine!

Thanks for the script and it worked perfectly !!

Only one issue need to be resolved for me.
The DBNames can also have more than one _ in them. In that case, how do we modify the script?


Report •

#3
June 28, 2011 at 15:49:53
Hey Mush.

The DBNames can also have more than one _ in them

You didn't mention that in your original post, the DBName was posted as one without spaces or other separators. I fell right into the trap LOL.

Here's another untested script for you to try, again you must supply the correct paths and remove the REM if you are happy with the outcome. Good hunting.

@echo off
cls
setlocal enabledelayedexpansion


pushd c:\trial\||echo Pushd failed - job terminated&&exit/b

for /f "tokens=*" %%1 in ('dir /b *.bak') do (
    set oldname=%%1
    set newname=!oldname:~0,-37!
    echo Oldname = !oldname!
    echo Newname = !newname!.bak
    echo.&echo.
    REM ren "!oldname!" "!newname!.bak"
    
)

echo.&echo.
dir c:\trial\
popd


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


Report •

Related Solutions

#4
June 28, 2011 at 19:48:53
It worked as cool as it could be.
Thanks a Lot. !!!

Report •

#5
June 29, 2011 at 04:09:51
You're welcome Mush, thanks for coming back to advise of your success.


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


Report •

Ask Question