473,388 Members | 1,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Renaming all files in a directory

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
5 6442
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
1
by: Don Leverton | last post by:
Hi Folks, I have been given a CD with approx 130 .xls files (bean-counters!) that I would like to import and merge to ONE table (tblTradeshow). The XL files are *similarly*, but not...
5
by: Zachariah | last post by:
I have code that renames files in a folder. Here it is: Dim strPath As String Dim objFSO As New Scripting.FileSystemObject Dim objFolder Dim objFile Dim strOldPath As String Dim strNewPath As...
1
by: boops boops | last post by:
Perhaps this is obvious to everyone but me, but how do you rename an existing project when using the .NET IDE? It allows you to save the current form as something else, but not the whole project....
3
by: | last post by:
I am a novice in C# and need help. I want to write a simple program to read a bunch of files from a specified directory and rename those files in a sequential fashion., changing a bunch of image...
3
by: Daves | last post by:
why the heck can't I use Directory.Move() to rename a directory (containing files) to a entry 2 levels below e.g. uploads/tempfiles -> uploads/tempfiles/48/003files if I do this in one line...
4
by: Martin Simard | last post by:
Hi all, Is there a way to rename the App_Code.dll generated from the code that belongs to the App_Code folder? We are developping several web applications that we are merging in a single...
1
by: GeoDW | last post by:
Hell All, I have looked around and not found the solution I am looking for within the old threads. Here is my problem: I have a directory full of .img and .rrd files that have long filenames...
0
by: brendan | last post by:
I'm trying to write a code that batch renames photos. In the end each photo should be named: AABBB1111YYYYMM222 where AABBB1111 defines a given place. (i.e. CAMTL = montreal and four digits...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.