By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,497 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,497 IT Pros & Developers. It's quick & easy.

Renaming all files in a directory

P: n/a
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

Nov 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Robizzle wrote:
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

Nov 22 '05 #2

P: n/a
Robizzle wrote:
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

Nov 22 '05 #3

P: n/a
Off topic, but...

mlimber wrote:
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

Nov 22 '05 #4

P: n/a
http://msdn.microsoft.com/library/de..._CFileFind.asp

is an MFC choice if u've Visual studio.
I wonder if there are such classes for linux ?

Nov 22 '05 #5

P: n/a
On 2005-11-18, in*****@gmail.com <in*****@gmail.com> wrote:
Off topic, but...

mlimber wrote:
Robizzle wrote:
foreach f (*.jpg)
mv $f `echo $f | cut -d. -f1`.jpeg
end
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'.

--
Neil Cerutti
Nov 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.