Renaming all files in a directory

R

Robizzle

I'm trying to write a simple console app that will rename all the files
in a directory according to any rules supplied by a user during
runtime. For example rename all *.jpg to *.jpeg. My problem is
getting a list of all the files that are in the current directory. The
only way I could figure out how to do it is to loop through all the
possible file names and checking if each one exists, if it does then
i'd push it's name into a queue for later processing.

But it seems like there has to be a better way. Are there any standard
functions that will return a list of all the file names that are in a
directory, or allow me to access files one by one until there are no
more?

I'm planning to mainly use this program in windows, however, if it is a
huge hassle, I could teach myself shell scripts and go that route as
the files I need to rename are very small and I have access to several
linux machines.

Thanks in advance for any help, I've tried several searches and don't
see anything relavant.

-Rob
 
M

mlimber

Robizzle said:
I'm trying to write a simple console app that will rename all the files
in a directory according to any rules supplied by a user during
runtime. For example rename all *.jpg to *.jpeg. My problem is
getting a list of all the files that are in the current directory. The
only way I could figure out how to do it is to loop through all the
possible file names and checking if each one exists, if it does then
i'd push it's name into a queue for later processing.

But it seems like there has to be a better way. Are there any standard
functions that will return a list of all the file names that are in a
directory, or allow me to access files one by one until there are no
more?

I'm planning to mainly use this program in windows, however, if it is a
huge hassle, I could teach myself shell scripts and go that route as
the files I need to rename are very small and I have access to several
linux machines.

Thanks in advance for any help, I've tried several searches and don't
see anything relavant.

-Rob

If you use csh in unix or cygwin, you can do something like this from
the command prompt:

foreach f (*.jpg)
mv $f `echo $f | cut -d. -f1`.jpeg
end

It assumes there are no dots in the filenames.

As for a C++ solution, you might consider the Boost.Filesystem library:

http://boost.org/libs/filesystem/doc/index.htm

Cheers! --M
 
A

Axter

Robizzle said:
I'm trying to write a simple console app that will rename all the files
in a directory according to any rules supplied by a user during
runtime. For example rename all *.jpg to *.jpeg. My problem is
getting a list of all the files that are in the current directory. The
only way I could figure out how to do it is to loop through all the
possible file names and checking if each one exists, if it does then
i'd push it's name into a queue for later processing.

But it seems like there has to be a better way. Are there any standard
functions that will return a list of all the file names that are in a
directory, or allow me to access files one by one until there are no
more?

I'm planning to mainly use this program in windows, however, if it is a
huge hassle, I could teach myself shell scripts and go that route as
the files I need to rename are very small and I have access to several
linux machines.

Thanks in advance for any help, I've tried several searches and don't
see anything relavant.

-Rob

You can try using the code in the following link:
http://code.axter.com/findfilecontainer.h

The above code is portable in Win32 and/or POSIX system. Both UNIX and
Linux support POSIX.
The commented section has example code.

For less portable methods, you can use the following Win32 code:
http://code.axter.com/FindFilesInPath.h

http://code.axter.com/findfiles_with_callback.h
http://code.axter.com/findfiles_with_callback.cpp
 
I

int2str

Off topic, but...
Robizzle wrote:
foreach f (*.jpg)
mv $f `echo $f | cut -d. -f1`.jpeg
end

Man you're complicated :D.

In windows:
ren *.jpg *.jpeg

In Linux:
rename jpg jpeg *.jpeg

:D
 
N

Neil Cerutti

Off topic, but...


Man you're complicated :D.

In windows:
ren *.jpg *.jpeg

Thanks for that tip. The current "RENAME /?" text doesn't
indicate that any such thing is possible. I guess it's a hidden
feature.
In Linux:
rename jpg jpeg *.jpeg

Never heard of that command. I'd have automatically used 'find'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top