C programming question - The good stuff, how do I do it?
So I've got a lot of archived (rar) files with par2 recovery files associated with them, and my goal is to write a program that will do its work in the present working directory.
the par2 files are either labeled filename.volume.PAR2, filename.volume.par2, filename.par2, or filename.PAR2. The first run should check the entire contents of the directory to see which format has been used for the file exension, and then dump its results into an array of strings, or a file, or something for use later. The second run then needs to check the entire directory for the format of the rar files. The first archive is labeled as either filename.r00, filename.R00, or filename.part01.rar, and dump the contents to an array of strings or something for use later. with the formats for the extension collected, the next step should truncate the results to PAR2, par2, or both. Code:
if(par2 && PAR2 are present) The best way, since if repair is necessary, the status message is printed 6 lines from the end, and when repair is unnecessary, the message is printed at the very end of the output, seems to me to be an on-the-fly evaluation of the output from the par2 v command. Code:
if(Repair is required.) Code:
if(Repair is required. && par2 && PAR2) So, once the par2 files are verified and repaired if necessary, then it is time to extract from the primary archive, the contents, into a newly created directory that is named "filename". so, Code:
My questions are -- How do I, from a C program, grab only a single instance of a file extension from multiple entries from an overall filename which may contain any number of different characters and formats? IE - 2008.records.bak.PAR2 Home.films-09.par2 Extremely-long-list-of-ingredients.vol83-93.par2 END RESULT SHOULD BE - .PAR2 && .par2 IE 2 - huffinsmcpuffins.par2 twenty-8-steps_to.fall-asleep-tonight.par2 END RESULT SHOULD BE - .par2 && !.PAR2 My initial thought is that I could do ls *.par2 *.PAR2 > pars strip the filenames and only keep the file extensions then uniq the file to reduce it to either par2, PAR2, or both. How do I, from a C program, execute another program with a wildcard because the filename, at this stage, is unimportant for the par2 command, followed by one or both of 2 possible formats for the file extension. IE - with the previous results from either EX 1 or EX 2, do - (what will be done regardless) [what needs to be added] (par2 v *) [.par2] (par2 v *) [.PAR2] My initial thought is a loop that is iterated based on how many different formats of extensions there are. The trick would be to distinguish between either par2 or PAR2, if both weren't present, but at the moment I'm unsure as to how that might be achieved. Next up is searching for the rar extensions. I imagine that the solution to doing it with par2 extensions would be mostly the same for the rars, so that's not my primary concern. So, how do I strip the extensions from the output of a command like ls *.r00 *.R00 *.part01.rar, and then uniq what is left and use that for the creation of directories to extract the files in to? IE - test01.r00 test01.r01 test01.r02 test01.r03 test02.R00 test02.R01 test02.R02 test1-334-dd-4565.part01.rar test1-334-dd-4565.part02.rar test1-334-dd-4565.part03.rar END RESULT IS - mkdir test01 mkdir test02 mkdir test1-334-dd-4565 If I can get these questions answered, I can figure the rest out. It's not a matter so much as, "how do I do X", but moreso a matter of, "I've got the general idea, but some of it isn't quite clear to me. I mean, I could make it work for 20% of the cases, but not 100% of the cases... and that is unsatisfactory!" Thanks guys, hope this post clearly conveyed my troubles. |
Well I guess my first question back to you would be why the need to be done in C seeing that you are constantly making calls to shell based programs??
It does appear this would be better in a script of some kind. Assuming you still wish to go ahead with C, maybe you can show us your code that works for the 20% and outline where you get stuck trying to get to 100%? |
Quote:
|
I can't help you with C, but I agree with the others that a script is enough for that kind of task.
When I deal with a problem like that, I also try to simplify things first. In your case that would be:
|
Well, this program is only going to be a few hundred lines long, so I don't care how un-intelligent the language considerations may be (at least at this point)
I will agree, however, that a BASH script or such would be the right choice for this task, but I just want to write a C program to handle as many of the lower-level issues as I can think of! I don't have a completed version yet, and there is a bug in my source as of right now, however it is coming along fairly well. Code:
#include <stdio.h> |
I agree that C is not an optimal language for this.
However, to get the extension, you would use pointers: Code:
char * buf, *ptr1; Then, you find the end of that string. You can do this a couple of different ways, but I'll use sizeof(): Code:
ptr1 = buf + sizeof(buf) Now we need the extension. To do this, scan backward through the string until we encounter a period: Code:
while(*ptr1 != '.' && ptr1 > buf) { So, in this case, at the end, *ptr1 will point to the string ".par2", which is what you want to find. As for the question about using wildcards, the system() command should work with that. |
All times are GMT -5. The time now is 07:52 AM. |