1.

Solve : I am looking to change filenames sequentially in a given directory?

Answer»

I used to know how to do this back in my MS-DOS days (when Windows 3.x was a big thing) but I haven't used batch files since switching to Windows 95 (it's HARD to believe that it's only been 15 years since then; seems a lot longer ago than that! ).

I want the batch FILE to find files with a certain extension, say like filename.jpg, and rename only those like:
filename1.jpg
filename2.jpg
etcetera...

It's weird, to me, that I cannot remember how I used to do that...I haven't even thought about batch files is so long, just never had any call to use them in that interval I guess (that's my excuse, any way lol)?

Thanks y'all!I have used a product called Better File Rename for tasks like this. Many different renaming options and sequential is an option. Software is not free THOUGH, but its cheap. I dont know of a way to do it in batch, but I could see doing it in another language with a counter++ like PERL or C++ and rename wildcard.jpg (*.jpg) with counter_variable_count.jpg from one directory to another directory as 1.jpg, 2.jpg and so on, where as the *.jpg function will only change JPG's. Instead of going through all the coding though, I just spent the $20 I think it was for the Better File Rename software.Code: [Select]@echo off
setlocal enabledelayedexpansion
cd c:\test\
dir /b *.jpg > c:\test\jpgseq.txt
set /a cnt=0
for /f "delims=" %%i in (jpgseq.txt) do (
set /a cnt=!cnt! + 1


copy "%%i" c:\tmp\filename!cnt!.jpg
)
cd c:\tmp
echo Display new filename
dir /b filename*.jpgOutput:

c:\test>seqjpg.bat
1 file(s) copied. ** (below)
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
Display new filename
filename1.jpg
filename2.jpg
filename3.jpg
filename4.jpg
filename5.jpg
filename6.jpg

c:\test>

c:\test>dir /b *.jpg
200167-010-Model.jpg **
200169-008M-Model.jpg
chuck.jpg
dance.jpg
I'm Sorry.jpg
irishdog.jpg

c:\test>
@johnpesky

Thanks for the help!

One ?, what does the "setlocal enabledelayedexpansion" do? I have never come ACROSS such a critter, 'course the 'new' winDOS has probably added &/or changed since the days when I 'played' with MS-DOS!

Thanks again ... I'll give the bat a SHOT! @DaveLembke

I've used several different file-rename / batch file-rename programs but none are just plainly and simply a sequential file-renamer. I rarely use anything like that but just every now and again I need one ... not worth spending $ for a program to do what I want on the rare occasions I do need one! :/

Microsoft needs to incorporate a batch file-renamer into it's file manager! I wouldn't think that it'd take too much code to add / change the existing file rename utility Windows Explorer has?

Thanks for the reply!Quote from: johnpesky on December 29, 2010, 07:34:38 PM


[whatever]


A good copying job, Billrich.
Quote from: QBall on December 30, 2010, 10:15:40 PM
Microsoft needs to incorporate a batch file-renamer into it's file manager! I wouldn't think that it'd take too much code to add / change the existing file rename utility Windows Explorer has?

http://blogs.msdn.com/b/oldnewthing/archive/2010/05/31/10017567.aspx
Quote from: QBall on December 30, 2010, 10:07:41 PM
Thanks for the help!
what does the "setlocal enabledelayedexpansion" do?

I'd love to see Billrich's answer to this question. The short answer is that it "enables delayed expansion" which you can look up on Google.


Code: [Select]import java.io.*;
public class SequentialRename {
public static void main (String[] args){
if ( args.length != 1 ){
System.out.println("Usage: java SequentialRename [extension]");
System.exit(1);
}
String extension = args[0];
String[] directory = (new File(".")).list();
int counter = 1;
for( String name : directory ){
if ( name.endsWith( extension) && (new File(name)).isFile() ){
String newName = name.replaceAll( "\\."+extension+"$" , "" ) + counter + "." + extension;
System.out.println( "old name: " + name + " , new name: " + newName );
( new File( name ) ).renameTo ( new File( newName ) );
counter++;
}
}
}
}

run using Java,
Code: [Select]java SequentialRename jpg

[recovering disk space - old attachment deleted by admin]would that not also replace files that happen to have only a name part that end with the extension? Such as, in your example, files like testjpg and filejpg and so forth? (probably an easy fix, and it's probably not something that would be encountered frequently)


Also- honest question- why did you select the screen name "JavaHater", yet most of the code you've posted here has been exclusively in java?

hi, thanks for your comments

Quote from: BC_Programmer on December 31, 2010, 01:57:53 AM
would that not also replace files that happen to have only a name part that end with the extension? Such as, in your example, files like testjpg and filejpg and so forth? (probably an easy fix, and it's probably not something that would be encountered frequently)
No, it will only replace those that are actual files, and those that have a dot "." , as in ".jpg". (the string for the regular expression actually is "\.jpg$" when combined).



Discussion

No Comment Found