Shorten File Name

June 11, 2009 at 08:11:32
Specs: Windows XP
Hi everybody,
I'm trying to make a script that eliminates the first "x" characters of all the files of a directory.

They all start with 0000 (i.e. 0000020090611.pdf) and I would like to get rid of it (i.e. 20090611.pdf).

Could you please give me an hint?
Thanks Alessandro

See More: Shorten File Name

Report •

June 11, 2009 at 19:01:46
They all start with 0000 (i.e. 0000020090611.pdf) and I would like to get rid of it (i.e. 20090611.pdf).

They all start with 4X0 but the example shown starts with 5X0.

Could you please give me an hint?
Hint: Use a For loop to rename files.

Report •

June 12, 2009 at 00:14:27
You are right I'll only have 4x0, my fault.

The issue I have is in the REN command: I am only able to rename 1 (or more) character with another one; I didn't succeed in renaming while removing chars...


Report •

June 12, 2009 at 00:53:03
Replace the path in the pushd command line to suit your setup. No guarantees. Care, if the pushd command fails the rest of the script will be executed on your default directory.

:: Code begins....
@echo off
setlocal enabledelayedexpansion

pushd path_to_files

for /f "delims=*" %%1 in ('dir /b 0000*.pdf') do (
    set filename=%%1
    ren %%1 !filename:~4!

:: Code ends....

Good luck.

Report •

Related Solutions

June 12, 2009 at 01:14:05
I would be very curious to know how many people out there (obviously much more expert than me!) can come out with an alternative batch solution to the problem; if you fancy I launch here a sort of very friendly "challenge" across this batch renaming session!

for my very first time I dare to reply by posting a possible solution; here is an alternative way I learnt from this great forum; it's a bit more general solution that can be adapted to multiple variations

:: start

@echo off
setlocal EnableDelayedExpansion

::rename files changing string "0000" with ""

for /f "delims=" %%I in ('dir /b 0000*.pdf ^| find "0000"') do (
set oldvar=%%I
set newvar=!oldvar:0000=!
call :ren
ren %oldvar% %newvar%
:: end

just put the batch in the same dir of the files to be renamed and the click over it...

hope it works and helps


Report •

Ask Question