command prompt rename files in sequential serie

Posted on 2014-08-15
Last Modified: 2014-08-17
Hello I want to order a serie of files I have  in c:\data


   and so on any number of files.
   This files could be hundred and there are names that doesn't exist in sequence like file003.pdf, file005.pdf, etc. Now in command prompt what script could be to order in sequence all that files and the numbers of the fies doesn't exist will be taken for next files in sequence for example file003.pdf doesn't exist then file004.pdf will take the name of file003.pdf in our example file007.pdf will take the name of file004.pdf, file009.pdf will take the name of file005.pdf and so on until the end of files .pdf only. The final result pf all process will be in other words:

file001.pdf - not touched because is ok in order
file002.pdf - not touched because is ok in order
file004.pdf - renamed to file003.pdf
file007.pdf - renamed to file004.pdf
file009.pdf - renamed to file005.pdf
file010.pdf - renamed to file006.pdf

 The final files ordered in resume:


   If anyone could help with a command .bat script.
Thank you
Question by:coerrace
    LVL 82

    Accepted Solution

    Try it with this; it's in test mode and will only display the "ren" commands it would normally run, so that you can test it. Remove the uppercase ECHO in line 24 to run it for real.
    @echo off
    setlocal enabledelayedexpansion
    REM *** Folder in which the files are:
    set SourceFolder=D:\Temp\FileRename
    REM *** File prefix until the numeric index:
    set FilePrefix=file
    REM *** File extension to search:
    set Extension=pdf
    REM *** Length of the numeric index:
    set IndexLength=3
    set ZeroString=0000000000
    call :GetStringLength PrefixLength "%FilePrefix%"
    set /a CurrentIndex = 1
    for /f "delims=" %%a in ('dir /b /a:-d /o:n "%SourceFolder%\%FilePrefix%*.%Extension%"') do (
    	set FileName=%%~na
    	set FileIndex=!FileName:~%PrefixLength%,%IndexLength%!
    	call :GetFormattedIndex FormattedIndex !CurrentIndex! %IndexLength%
    	echo Processing %%a; file index is '!FileIndex!', sequential index is '!FormattedIndex!' ...
    	if "!FileIndex!"=="!FormattedIndex!" (
    		echo ... indices match, no change.
    	) else (
    		echo ... file index not in sequential order, will rename.
    		ECHO ren "%SourceFolder%\%%a" "%FilePrefix%!FormattedIndex!.%Extension%"
    	set /a CurrentIndex += 1
    goto :eof
    :GetFormattedIndex <ReturnVariable> <Index> <Length>
    set __string=%ZeroString%%~2
    set %1=!__string:~-%3!
    goto :eof
    :GetStringLength <ReturnVariable> <String>
    set /a i = 0
    set __string=%~2
    if "!__string:~%i%,1!"=="" (
    	set %1=%i%
    	goto :eof
    ) else (
    	set /a i += 1
    	goto :StringLengthLoop
    goto :eof

    Open in new window


    Author Closing Comment

    Works perfect including if you put  IndexLength longer than 3
    Amazing code!!!
    LVL 38

    Expert Comment

    Hi coerrace

    You may have been left wondering why you received an email notification of a comment submitted before the one by oBdA.  I started writing a batch file yesterday and had posted a "working on this" comment, but something came up and I had to leave what I was doing, so I deleted the comment and wasn't able to finish what I had started.  oBdA's batch file is along the same lines as I was heading, but is much better.


    Author Comment

    Oh I see honestly the first thing at the morning was see the oBdA I tested but never saw a working on this, but is ok.
    Thank you for all

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now