Click to See Complete Forum and Search --> : A script to change the extension of files?
Wallex
07-18-2002, 05:47 PM
Well, I was told to look in this threa for the solution of my problem, and I couldn't find it (and it's just two pages of a thread, so I don't think I just 'missed it'). Anyway... I am new to Linux, but not new to C, I understand the language but none of the ways to compile/execute, nor the reserved words I can use to do what needs to be done. I don't think I have the time to learn that right now (as there's just too much I have to do), so could anyone help with this?
The problem: Renaming 1200 or around files that have an extension .sp7 and need to be converted to the same filenames with the extension spc. They are all in the Spc folder. And in advance.. if someone's gonna post the script, how do I run it? I think I read it was something like ./scriptfilename?
froggy3132000
07-18-2002, 06:13 PM
Try this link http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_271.html
I have done this before, with a while loop, I had to parse each name and bring it together with the new file extension. Haven't used C in a while, that should work.
Wallex
07-18-2002, 09:53 PM
Thanks... altough this is just the tip of the iceberg. Now that I know how to rename a file, I need to find out how to make a loop ... um... actually, what I probably need to know is how to get a list of all filenames inside a certain directory... or something like that. Hmm.. I'll go search a little more on that link. Thanks, hope I can manage to get this done before tomorrow.
TacKat
07-18-2002, 10:34 PM
I wouldn't bother trying to do this in C except for fun. It's simpler to just make a BASH script:
#!/bin/bash
for file in *.sp7; do
name=`basename $file`
rename=`echo $name | sed -e "s/sp7/spc/"`
mv $name $rename
done
Put that in a textfile then use chmod +x on the file to make it executable. Put it in the directory where these files reside and use ./scriptname to run it.
I didn't really test it so use at your own peril. It should be noted that only the first instance of "sp7" in the file name will be changed, so hopefully none of your files have an "sp7" before the extension.
Wallex
07-18-2002, 11:15 PM
Nice try... but...
It won't work on files with names that include spaces. I don't know anything about BASH scripts, but my guess is: Since the filenames have spaces, basename receives more than one parameter, and that causes the problems. Um... any idea on how to fix this? This bash method looks a lot quicker than doing a c file... I was trying that.. and well, it's gonna be longer than.. say... longer than 30 lines, is not a lot to type, but I have to look up all the functions and types I need... lots of learning... but if anyone can help solve this issue quicker, even better.
EDIT: What have I done? I tried adding "" nextt to the filenames so that they would be treated as a single argument, but now when I try to run it the console tells me: 'bad interpreter: Permission Denied'? what? but I am root? Err... did I mess up badly?
saithan
07-19-2002, 12:25 AM
the error you are reporting sounds allot like the chmod didn't take. if you are a gui junkie then just right click the flie and choose properties and make sure it is set as executable to the user/owner of the file.
if all else fails use cammand line:
chmod 755 file_name
* replace file_name with the actual name of file.
root will get permision denied error when the file is not chmod to execute for any user.
Wallex
07-19-2002, 12:46 AM
Yikes... these permissions things are driving me crazy! I finally made this work by adding "" everywhere, but it seems everytime I run it some files get protected against overwrite? Weird... but I'll get this working! Thanks.
furrycat
07-19-2002, 01:01 AM
Yes, you should always put quotes around string variables in shell scripts 'cos you never know when one is going to end up containing whitespace.
If you want to do it in C you can just use standard library functions. struct dirent dir;
DIR *directory;
directory = opendir(".");
while (dir = readdir(directory)) {
/* filesystem object's name is in dir->d_name */
}
closedir(directory);
Inside the loop you have to check that the object is a file with stat(), construct your new name and then call rename(old, new).
saithan
07-19-2002, 01:05 AM
that is good that you got it working.
Again if the files are not set with write permisions for any user (including root). the file will report write access denied.
a good way to fix this is to run the terminal and cd to the directory where the files all are and use:
chmod 664 *.sp7
this will place read permisions to all users
write permisions to owner and owners group for all files in the directory with .sp7 extentions.
then run the script again and those files that were write protected should then be writable.
Wallex
07-19-2002, 09:30 AM
Well thanks. I did got it all working finally. it seems there was a minor conflict of permissions over the script since I had it openened and at the same time I was changing it's permissions from Konqueror (in other ways, It used to sort of auto-change its permissions without my permission, if that made sense). I did got all working at last, altough I wonder if it would have been easier if I finished writing that c code I was working in... oh well, good thing it's done now. Thanks again for the help.
saithan
07-19-2002, 12:05 PM
for such small automated tasks shell scripting is best suited.
To go through the trouble in c would be a time consuming proccess in compairison.
I think one of the earlier posts said it best.....
"I wouldn't bother with C unless it was for fun".
shell scripting is designed for automating many system tasks, it is light fast and easy to build scripts for virtually any task.
You are new to Linux (I assume) so it probably seems akward right now but you will develope a style of working with your systems.
A while back I first started running Mozilla but with the release of 1.0 I was annoyed with profile manager becuase I use gaim and any time some one sent me a link and i click it the profile manager would popup. I could have used c, c++ to build a solution but that would take time and i wanted a fix "now"
within a few minutes i built a script to solve my problem.
here is an example of how i fixed the prob using a cheesy script I made:
http://eclug.saithnet.dyndns.org/article.php?sid=32
here is another script I used to solve a simulare prob to the one you had:
#!/bin/sh
#
ls * | while read f
do
if [ -f $f ]; then
if [ "$f" != "`echo \"$f\" | tr A-Z a-z`" ]; then
#Note that 'This' will overwrite 'this'!
mv -iv "$f" "`echo \"$f\" | tr A-Z a-z`"
fi
fi
done
I used that to remove all caps from 2000 fonts I was adding to my system.
Look at it this way with shell scripting I was able to
1. realize the problem to be solved.
2. develope a script to conquor the issue. (consisting of automating common commands)
3. impliment and move on.
total time was a matter of typing the script.
Wallex
07-19-2002, 12:43 PM
Sounds great.
I guess C is for making programs and all that... but for quick 'fixes' scripting seems to do the job right (unless you can actually make a program with a script.. but somehow I doubt it), yeah I am a newbie as far as I can tell (I haven't found yet how to change the mouse icons! That's the only thing I have left to configure to personalize the distro I am using), and well... I do plan to become a computer programmer someday, so Linux is great for learning and using... well, anything's better than Windows anyway.
saithan
07-19-2002, 01:02 PM
lol, I myself consider Linux better than windows, anyways.
shell scripts are programs. and yes you can make full programs in shell scripting ranging from simple task automations to rather complex proccessing.
there are so many tools to work with in scripting as well as so many shells to fit what ever you want.
for instance I had a script (can't find the damn thing now that I want it) that i ran in cron to retrieve headline news articles by keyword from my favorite news sites. for the command aurguments i used the sites and a single keyword:
./scriptname.sh www.slashdot.com www.hackinthebox.org www.linuxnewbie.org/forum -k kernel
this would pull the front page of each listed site and look for the value "kernel" if found would print the article to a predetermined directory for me to read at my leasure.
this is just to give you an idea as to the simple things *nix provides as a standard and can be taken advantage of any time.