473,322 Members | 1,718 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,322 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 6440
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.