Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A program to scan, view, and move files in a directory. (Linux) 1

Status
Not open for further replies.

Eldaria

Programmer
Sep 20, 2001
123
NL
Hi there.

I have a little problem, that I think can easily be solved by making a small application.

I need to manually sort and move a whole bunch of files, with very much random names (2CFDgfjdwFDf43Fs.txt), in some directories.

The directories contains thousands of files each, so it is a heavy task. :-S

However, I thought, it could be automated a bit, or at least made a bit more efficiant than I'm doing it now.

At the moment, I'm opening each file with less.
Then I look through the file, and decide if it should go to one or the other folder.
Then I close the file, and manually move the file with mv.
And start over.

However, this is very inefficiant.
So I had an Idea.
A program that open each file in the current directory, with a certain extension.
Displays the file on the screen, higlights certain keywords that have been predefined either on the command line, or in a configurationfile.
Then I can scroll up and down in the file, with arrowkeys, or space key, just like less.
with Certain keys, I can then tell the program to move it to different folders, depending on what was predefined, again on command line or config file.
And then it opens the next file.

Now, I think this is a simple program, File access and keyboard input.
However, I know nothing or at least very little about C.
I have programmed VisualBasic on windows, but am only just started with Linux and with C.
So I thought I could get this problem solved AND learn from it at the same.

So If any of you C/C++ guys out there could write me a small app with comments, that I can, try out. It would be great. :)

Regards,.
Brian Levinsen
 
Use awk and the shell, perl or tcl and save yourself some headaches.
C is not a good solution here IMHO.
 
As I wrote I'm pretty much a newbie, so the only thing I recognized there was shell, and Perl. :-S

Knowing that the shell is where I type the commands, and perl beeing a programming language. :)

So the headache I think I will get anyhow.
But If you feel like explainig in more detail, then feel free. :)


Eldaria

That was my 25cent** of opinion.

** Inclusive Intrest, tax on interest, Genral tax, Enviromental tax, Tax, and tax on intrest, tax on fees, tax on tax, and other Various taxes and fees.
 
> A program that open each file in the current directory, with a certain extension.
Code:
for i in *.css ; do
  echo $i
done
[tt]foo.css
bar.css[/tt]

> higlights certain keywords that have been predefined either on the command line, or in a configurationfile
Say we want to find 'label2'
Code:
for i in *.css ; do
  grep -q 'label2' $i
  if [ $? == 0 ]; then
    # do something with matching file
    echo $i
  fi
done
Iterate over all the files we have, and process each one which contain the text we're looking for.
$? contains 0 if a match was found, or 1 if there was no match.


Here's an alternative way of going about it.
Code:
for i in `grep -l 'label2' *.css` ; do
  # do something with matching file
  echo $i
done
The grep command produces a list of all filenames which contain the matching text. The for loop then iterates over this list.

--
 
Good examples as always salem.

I get the feeling that the problem is that the OP needs
a bulletproof program written for him.
With all respect ,and IMO, this is not a valid use for the
forum. The OP needs a consultant.
 
*nods*
Yeah, this should probably be in the Unix Scripting Forum


--
 
Well it is not full Automation that I need.
I manually need to see each and every file, and then make a choice if it goes in one of two categories.

The files will contain certain keywords, followed by something that I manually need to make a choice upon.
However the keyword can be located anywhere in between different lines.

This is why I am currently using less to view the file, and then make the choice and move the file to one of the directories.

What I meant was a program that will open the files one after the other, show it to me and highlight the keywords. (To make it easier to look through the file.)
and then give me a choice of moving the file to one of two folders. as soon as I make the choice, the next file is opened.

I'm a private person sorting through my spam filter, that contains files for a couple of months, trying to teach spam assasin what is spam and what is not.
And it is a bit teadious to figure out the filename that is random, type it, and then open the file, read it, and then type the eintire string to move it to one of the folders.
From doing VB on Windows, I know that opening i a file, showing it and the give the user a choice, is not many lines of code.
I have from the little I have so far learned about C, lerant that the stdio.h is the one handeling input from user, so I can figure out how to do that.
But File handling I know nothing about.
So I'm not looking for a consultant as such, more looking for a program, becasue I want to leanr C, And I am learning it as such, but How can you learn it? By doing it? or by hiering a consultant to do it for you?
So this is why I was looking for a source with comments in it, that explains what the script does, and in such I could understand and learn, and even modify it my self.

I hope this has clarified, what I'm looking for, and why.






Eldaria

That was my 25cent** of opinion.

** Inclusive Intrest, tax on interest, Genral tax, Enviromental tax, Tax, and tax on intrest, tax on fees, tax on tax, and other Various taxes and fees.
 
Eldaria, I think the point people are trying to make is that asking for someone to write a programme for you (as you posted - "So If any of you C/C++ guys out there could write me a small app ") is not what these forums are for - they are helping people who have already written code, and are having problems with it.

If you are a newbie, then just search around on the net for some examples, and try them out.

For example, stdio.h handles file IO - opening files, reading them, writing them, closing (and hence moving files around). This page gives you the functions and examples how to use them :
You need to look into :

fopen()
fclose()
fread()
fwrite()
fscanf()

A quick C tutorial can be found here :
Once you have some code to work with, feel free to come back for help !





--------------------------------------------------
Free Database Connection Pooling Software
 
Eldaria,
Just as an afterthought.
Once had to co-admin some pretty heinous spam filtering software for a windows shop using the old .eml backed
exchange box format. To educate the filter when it missed
a message you would have to add the relevant header derived
info or some body text depending on the filter set.

My solution was similar to what you suggest except that
it had interactive and automated modes and was in a
scripting language.
In response to your request for a prototype...that would
still be a rather lengthy task.
My suggestion is to go to: and
do a search against spamfilter. There are several projects
there, in various languages, which should provide decent
example code.
 
You should ask on the Unix Scripting forum, as mentioned.

Here's a small example (written in Bash) that might "semi-automate" it, as you ask:

Code:
for i in *.ext; do
    less "$i"
    select dir in /maybe/it/goes/here /or/maybe/here; do
        mv "$i" $"dir"
    done
done
 
Here's a quick idea:
Code:
#include <string>
#include <fstream>

const string dir1 = "dir1"; //set this to the first dir
const string dir2 = "dir2"; //set this to the second
const string ext = ".foobar"; //set this to the externtion

int main()
{
  string file;
  system(((string)"ls *." + ext + " > blah").c_str());
  ifstream fin;
  fin.open(blah);
  while(getline(fin,file))
  {
      system(((string)"less " + file).c_str());
      cout << "Goes in 1 or 2";
      int i;
      cin >> i;
      if( i == 1 )
         system(((string)"mv " + file + " " + dir1).c_str());
       else
          system(((string)"mv " + file + " " + dir2).c_str())
   }
   fin.close();
   system("rm blah");
   return 0;
}

I know it's overkill to compile a program for this, but this should work. In theory, it'll display a file in less, you'll look at it, when done looking you press 'q', then it'll ask if you want it in directory 1 or 2, you tell it which and it puts it there. Haven't tested it at all -- have not even compiled it... but something like this should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top