Connecting Tech Pros Worldwide Forums | Help | Site Map

Renaming all files in a directory

Robizzle
Guest
 
Posts: n/a
#1: Nov 22 '05
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


mlimber
Guest
 
Posts: n/a
#2: Nov 22 '05

re: Renaming all files in a directory


Robizzle wrote:[color=blue]
> 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[/color]

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

Axter
Guest
 
Posts: n/a
#3: Nov 22 '05

re: Renaming all files in a directory


Robizzle wrote:[color=blue]
> 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[/color]

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

int2str@gmail.com
Guest
 
Posts: n/a
#4: Nov 22 '05

re: Renaming all files in a directory


Off topic, but...

mlimber wrote:[color=blue]
> Robizzle wrote:
> foreach f (*.jpg)
> mv $f `echo $f | cut -d. -f1`.jpeg
> end[/color]

Man you're complicated :D.

In windows:
ren *.jpg *.jpeg

In Linux:
rename jpg jpeg *.jpeg

:D

pedagani@gmail.com
Guest
 
Posts: n/a
#5: Nov 22 '05

re: Renaming all files in a directory


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 ?

Neil Cerutti
Guest
 
Posts: n/a
#6: Nov 22 '05

re: Renaming all files in a directory


On 2005-11-18, int2str@gmail.com <int2str@gmail.com> wrote:[color=blue]
> Off topic, but...
>
> mlimber wrote:[color=green]
>> Robizzle wrote:
>> foreach f (*.jpg)
>> mv $f `echo $f | cut -d. -f1`.jpeg
>> end[/color]
>
> Man you're complicated :D.
>
> In windows:
> ren *.jpg *.jpeg[/color]

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

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

--
Neil Cerutti
Closed Thread