Fedora Linux Support Community & Resources Center
  #1  
Old 15th August 2012, 04:31 PM
cmanL Offline
Registered User
 
Join Date: Jul 2012
Location: America
Posts: 76
macossafari
Processing Files In a Directory

I want to write a Bash shell script to process all the files of a certain type in a directory. Something like this pseudo code:

Code:
 
file = getFirstFile( ".txt" ); //get first text file in the current directory

while ( filesLeftInDirectory ) //while files of this type are left in the directory
{
    processFile ( file );  //process the file
    file = getNextFile ( file , ".txt"  ); //get the next file of the proper type
}
Can this be done in C/C++? I've never written Bash scripts so please keep it simple.
Reply With Quote
  #2  
Old 15th August 2012, 06:27 PM
marko's Avatar
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,449
linuxfirefox
Re: Processing Files In a Directory

Does it have to be bash?, perl could do that pretty basically with the below:

Code:
#!/usr/bin/perl -w
#
#
use strict;
use warnings;

sub processFile
{
        # need to put here the necessary per file processing
        print "@_\n";  # do a print to prove it's called correctly
}

my @txtfiles = <*.txt>;


foreach my $tfile ( @txtfiles )
{
        processFile($tfile);
}

exit 0;
You'd just need to add the details to "processFile" to do the processing
Reply With Quote
  #3  
Old 15th August 2012, 06:36 PM
Skull One's Avatar
Skull One Offline
Registered User
 
Join Date: Jun 2010
Location: Lost...
Posts: 552
linuxredhatmozilla
Re: Processing Files In a Directory

It is quite easy:
Code:
for i in *.txt; do

# do something, for instance:
echo "Found file $i"

done
__________________
:confused:
Reply With Quote
  #4  
Old 16th August 2012, 04:50 AM
ocratato Offline
Registered User
 
Join Date: Oct 2010
Location: Canberra
Posts: 550
linuxfirefox
Re: Processing Files In a Directory

Quote:
Originally Posted by cmanL View Post
I want to write a Bash shell script to process all the files of a certain type in a directory. Something like this pseudo code:
...
Can this be done in C/C++? I've never written Bash scripts so please keep it simple.
To do this in C check out the opendir and scandir functions. The info pages even include some nice examples. (Oh, and don't forget closedir() or you might run out of file handles.)

If you happen to be using the Qt libraries, they have a C++ class called QDir that you might use.

However, the bash script, shown previously, is the simplest approach. You can even do the processing part in a C/C++ program that would then only have to worry about the one file.
Reply With Quote
  #5  
Old 18th August 2012, 10:41 PM
cmanL Offline
Registered User
 
Join Date: Jul 2012
Location: America
Posts: 76
macossafari
Re: Processing Files In a Directory

Ok , thanks for your responses !
Reply With Quote
Reply

Tags
directory, files, processing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
livecd-creator, kickstart files, adding files to /home directory. I.E. software dedic Bastian123 Installation and Live Media 7 24th March 2011 11:00 AM
processing hidden files lothario Using Fedora 6 13th May 2010 12:38 PM
[SOLVED] How to use processing.org's Processing-1.0.9 as an Application rjames Installation and Live Media 4 3rd March 2010 09:31 PM
Processing multiple files in a directory PompeyBlue Using Fedora 3 22nd December 2008 10:07 PM


Current GMT-time: 18:17 (Thursday, 23-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat