LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-26-2010, 09:04 AM   #16
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111

Code:
fixed_name=$( echo "${songname,,}" | sed -e 's/^./\U&/' -e 's/- ./\U&/' )
Use here-strings (cmd <<< "string") instead of echo.

Code:
start_name="${lower_name%% -*}"
end_name="${lower_name##*- }"
This will not properly handle names with multiple '-' and splice out the middle.

Quote:
Have a look at this
http://tldp.org/LDP/abs/html/loops.html
ABS has a lot of problems and teaches bad habits, http://mywiki.wooledge.org/BashGuide...til_and_for.29 is much better.

Quote:
I think that needs to be double quotes because of the spaces
Incorrect, since there are no expansions inside the quotes, there is no difference between '' and "". The quoting is there to prevent the shell from globbing the *. Regardless, that way of using find does not work for the problem at all, and does not properly handle newlines in filenames.

It's important to use [:upper:] and [:lower:] in tr due to locale oddities.
Code:
newfile="$(tr '[:upper:]' '[:lower:]' <<< "$file"  | sed -e 's/^ *./\U&/' -e 's/- *./\U&/')"
For the loop, use the basic while-read-find that I must have posted a dozen times already.
 
2 members found this post helpful.
Old 02-26-2010, 09:13 AM   #17
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
@tuxdev:
It seems that you are an expert on the subject of scripting (like grid?)!
Couldn't you provide the final code?

Thanks
 
Old 02-26-2010, 09:28 AM   #18
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by whizje View Post
If you learn python you will have little trouble with bash scripting.
But if you looked at man find as mentioned your problem would already be solved
a little help
Code:
find . -name '*.mp3' | sed ...
I think that needs to be double quotes because of the spaces
Code:
find . -name "*.mp3" | sed ...
Actually, you must use single quotes because you want the wildcard characters to be passed on to find, instead of being processed by bash.
 
1 members found this post helpful.
Old 02-26-2010, 10:01 AM   #19
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks MTK358.
 
Old 03-08-2010, 09:24 AM   #20
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
Thumbs up Alternative method

Well, here I bring you an alternative.
My friend T. Hlaing, wrote me:

Quote:
You can easily hack my script ( http://gtk-apps.org/content/show.php?content=87601 ) to suit your needs.
If you enable "Recursive" option, it will automatically include subfolders (except for patternize option).
For each file, to get a new name for it, get_new_name function will be called.
All you need to do is modify it to suit your needs.

For instance, if you want to modify "First Letter Upper case" to suit your case, you will need to change this code segment.

elif self.misc_opt == FIRST_CAP:
name = name.capitalize() /* Change this line */
Thanks a lot!
 
Old 03-12-2010, 01:34 AM   #21
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Just an example in bash for your reference. You need bash 4.0 for this

Code:
#!/bin/bash

function renamefiles {
	if [[ BASH_VERSINFO -lt 4 ]]; then
		echo "You need bash 4+ to make this script work."
		return 1
	fi

	while read PATH; do
		FILE=${PATH##*/}
		DIR=${PATH%/*}

		if [[ $FILE == *-* ]]; then
			LEFT=${FILE%%-*}
			LEFT_UPPER=${LEFT:0:1}
			LEFT_UPPER=${LEFT_UPPER^^}
			LEFT_LOWER=${LEFT:1}
			LEFT_LOWER=${LEFT_LOWER,,}
			LEFT=${LEFT_UPPER}${LEFT_LOWER}

			RIGHT=${FILE#*-}
			RIGHT_UPPER=${RIGHT:0:1}
			RIGHT_UPPER=${RIGHT_UPPER^^}
			RIGHT_LOWER=${RIGHT:1}
			RIGHT_LOWER=${RIGHT_LOWER,,}
			RIGHT=${RIGHT_UPPER}${RIGHT_LOWER}

			FILE=${LEFT}-${RIGHT}
		else
			FILE=${FILE%%-*}
			FILE_UPPER=${FILE:0:1}
			FILE_UPPER=${FILE_UPPER^^}
			FILE_LOWER=${FILE:1}
			FILE_LOWER=${FILE_LOWER,,}
			FILE=${FILE_UPPER}${FILE_LOWER}
		fi

		echo "renaming $PATH to $FILE..."

		mv "$PATH" "$DIR/$FILE" || {
			while read -t 0.2 -n 1; do continue; done  # flush keyboard buffer
			echo 'An error has occurred. Press ^C to cancel or any other key to continue.'
			read -n 1
		}
	done < <(exec find -type f -name '*.mp3')
}

renamefiles
edit: revised to work with filenames that doesn't contain '-'.

Last edited by konsolebox; 03-12-2010 at 01:41 AM.
 
1 members found this post helpful.
Old 03-12-2010, 07:06 AM   #22
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
GTK interface pending.

A thousand thanks, konsolebox!

I think now with your script, with that of GrapefruiTgirl, with that of grail, and that of D. Ciabrini, we have enough to start working on a GTK interface.
To be honest, what really would make me happy is if someone could implement all of these scripts (fused into one) in the existing program pyRenamer ( http://www.infinicode.org/code/pyrenamer/ ).
I want to clarify that before posting this thread, I had long conversations with the author (in Castilian), and he said he had left the project due to lack of time. Not even offering money, I could convince implement this feature ... Really a shame.

I want to thank all contributors for, just how small contributions have made so far.

Last edited by pkramerruiz; 03-12-2010 at 07:07 AM.
 
Old 03-12-2010, 08:26 AM   #23
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by pkramerruiz View Post
A thousand thanks, konsolebox!

I think now with your script, with that of GrapefruiTgirl, with that of grail, and that of D. Ciabrini, we have enough to start working on a GTK interface.
To be honest, what really would make me happy is if someone could implement all of these scripts (fused into one) in the existing program pyRenamer ( http://www.infinicode.org/code/pyrenamer/ ).
I want to clarify that before posting this thread, I had long conversations with the author (in Castilian), and he said he had left the project due to lack of time. Not even offering money, I could convince implement this feature ... Really a shame.

I want to thank all contributors for, just how small contributions have made so far.
Hey a thousand is way too much but 'course you're welcome. The script is even just a prototype. Just now I even found a bug.. RIGHT* will just hit a space and will not convert the first letter to uppercase.

Regarding pyRenamer I'm glad if I can help too but, too bad I don't do python and I also have no plans learning it yet. My target interpreter language is only Perl and Ruby. Just my taste and hope it's no offense to others.

Maybe you should try posting a request on a python forum?
 
1 members found this post helpful.
Old 03-12-2010, 10:27 AM   #24
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by konsolebox View Post
...too bad I don't do python...yet. My target interpreter language is only Perl and Ruby.
If you think its a good idea:
I thought that with the help of my friend D.Ciabrini, you (both) might do an interface? I think some of this forum will lay a hand ...
I have no idea whether it is possible to interface with Pearl, but he knows a lot of Pearl ... Of course, he does not have much free time ...
 
Old 03-13-2010, 01:00 AM   #25
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by pkramerruiz View Post
If you think its a good idea:
I thought that with the help of my friend D.Ciabrini, you (both) might do an interface? I think some of this forum will lay a hand ...
I have no idea whether it is possible to interface with Pearl, but he knows a lot of Pearl ... Of course, he does not have much free time ...
Sorry but I'm also not yet good with Perl and Ruby and I don't go online often so I don't think I'll be capable of doing that.
 
0 members found this post helpful.
Old 03-13-2010, 07:28 AM   #26
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 583

Rep: Reputation: 129Reputation: 129
I suggest that you try it yourself if you got a problem there always people who want to offer you a hand, but you want it it so get started.
 
0 members found this post helpful.
  


Reply

Tags
clean, music, rename, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to clean music. pkramerruiz Linux - Newbie 3 02-26-2010 06:18 AM
Need script to clean up file stevemcb Programming 26 01-18-2008 05:55 AM
clean music collection edgjerp Linux - Software 5 12-04-2007 02:38 AM
Help with clean-up script fiservguy Programming 5 01-27-2005 12:59 AM
Simple script to clean up old file rbeckett Red Hat 2 09-09-2004 02:38 PM


All times are GMT -5. The time now is 01:43 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration