0

This is my first question in this beautiful website. As you probably read in the title I would like to rename a variable number of files, with a sequence of numbers in cmd and a batch file, the sequence is increasing and it is like this (1, 2, 3, 4, 5, 6, 7, 8, 9, 10...). For example:

Test.txt it should become 1.txt

Another.txt should become 2.txt

And so on, all automatically.

My idea was to set up a variable like set /a number=1 and add +1 like this set number="%number%+1" to it through a loop and rename each time, but it isn't possible since when I rename files with ren command it renames all at once.

Can anyone help me providing a cmd and a batch file version?

Thanks in advance

AleP _C.P.
  • 13
  • 1
  • 4
  • 2
    You really have not provided a [mcve] of your code. Please also take the [tour] and read [ask] a question. – Squashman Sep 17 '20 at 16:35
  • Welcome to SO, AleP _C.P. Questions without code should go to https://superuser.com/ – lit Sep 17 '20 at 16:48
  • 1
    If you're using a looping mechanism, for your incrementing, you must already know that a loop would iterate the files one at a time, not 'all at once'. What you need to look into is a [tag:for-loop], and there are tens of tousands of examples of those under the [[tag:batch-file]] tag alone! Please use the search facility and adapt some code, before [editing your question](https://stackoverflow.com/posts/63942028/edit) to bring it on topic. – Compo Sep 17 '20 at 16:52
  • I guess you're using `ren *.* %number%.*`, right? – aschipfl Sep 17 '20 at 17:45
  • Sorry for not providing all this important informations, thanks for all, this is my first question and I never wrote anything in this website. I will carefully read all the documentation. – AleP _C.P. Sep 19 '20 at 21:27
  • My preferred way of doing is this to install `git` and the use [this bash command](https://stackoverflow.com/a/10780250/1176573) to rename. Much cleaner. – Rajesh Swarnkar Aug 12 '22 at 15:09

1 Answers1

5

This should work for you.

set /a Index=1

setlocal enabledelayedexpansion

for /r %%i in (*.txt) do ( 
    rename "%%i" "!Index!.txt"
    set /a Index+=1
)

Create a batch file with above code and run it in folder where your .txt files are available.

If you want to append "0" to make it 2 digits. you can try adding if else statement as below.

set /a Index=1

setlocal enabledelayedexpansion

for /r %%i in (*.txt) do ( 
    rem if number is less than 10, append 9 to file name
    if !Index! lss 10 (
        rename "%%i" 0"!Index!.txt"
    ) else (
        rename "%%i" "!Index!.txt"
    )
    
    set /a Index+=1
)
Mova
  • 928
  • 1
  • 6
  • 23
  • Here, output comes as 1,2,3,4,5,6,7,8,9,10,11,12....' Can it be 01,02,03,04,05... in 2 digits?' – foodiepanda Jul 17 '21 at 05:43
  • 1
    @foodiepanda Unfortunately, I was not active here for while. Not sure if you still want to make it 2 digits number or not. I have updated my answer in case if you still need it. Thanks! – Mova Sep 10 '21 at 08:41
  • Thanks for updating answer, I do not need it now, but future visitors could' – foodiepanda Sep 30 '21 at 12:28