LinuxQuestions.org
Help answer threads with 0 replies.
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 10-26-2011, 05:21 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191

In that case would you show us the entire script?
 
Old 10-28-2011, 09:09 AM   #17
giantanakim
LQ Newbie
 
Registered: Oct 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
I have not included it in any other script at the moment, it is completely stand alone. The code that i copied into my post the other day includes the complete contents of the file.
 
Old 10-28-2011, 12:42 PM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
In that case, how are you executing it as it will not run as is?
 
Old 10-28-2011, 01:21 PM   #19
giantanakim
LQ Newbie
 
Registered: Oct 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
The script does run as is. It is in a file called renamefiles.sh which I marked as executable. I run the command line, type the command ./renamefiles.sh which causes it to run. When I run it, it runs, but for each line says [[ cannot be found. This is the code that I am using exactly:

for file in *.wav;
do cnt=1;
newfile="${file%% *}";
while [[ -f "${newfile}.wav" ]];
do newfile="${file%% *}_$((cnt++))";
done;
mv "${file}" "${newfile}.wav";
done

It has been doing what it is supposed to do, but kicking up that error for every line. In addition, it adds an extra .wav on the end of files that have already been renamed by it, leaving the filename 1112223333.wav.wav.
 
Old 10-29-2011, 02:38 AM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What happens if you place the following as the first line in the file:
Code:
#!/bin/bash
 
Old 10-29-2011, 04:18 AM   #21
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
I've had similar requirements some days ago. The thread is here: http://www.linuxquestions.org/questi...rogram-908658/

I've done it with perl because the normal rename-tool cannot handle regexp.

Additionally I've written a clean.pl script:
Code:
#!/usr/bin/perl

use strict ;
use warnings ;
use File::Copy ;

opendir THISDIR, "." ;
my @files = readdir THISDIR ;
my ( $old, $new ) ;
foreach $old ( @files ) {
       $new = $old ;
       $new =~ s/ - /-/g ;
       $new =~ s/ /_/g ;
       $new =~ s/ä/ae/g ;
       $new =~ s/Ä/Ae/g ;
       $new =~ s/ö/oe/g ;
       $new =~ s/Ö/Oe/g ;
       $new =~ s/ü/ue/g ;
       $new =~ s/Ü/Ue/g ;
       $new =~ s/ß/ss/g ;
       $new =~ s/,/./g ;
       $new =~ s/'/./g ;
       $new =~ s/\(//g ;
       $new =~ s/\)//g ;
       $new =~ s/&/und/g ;
       next if $old eq $new ;
       move ( $old, $new ) ;
}
I've used this script to substitute german Umlaute with more common characters. The script substitutes " " with "_". I used it for mp3 files. If you want to use it you'll have to add code which uses another filename if $new already exists in the directory. You'll find the solution in my rename.pl script in the other thread mentioned above.

Hope that helps.

Markus
 
Old 10-31-2011, 09:43 AM   #22
giantanakim
LQ Newbie
 
Registered: Oct 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
Grail:

When I added the #!/bin/bash into the file, it stopped giving the error of [[ cannot be found. It does still add the unnecessary .wav at the end of the files.

Markush:

I will have to look into your script. I am not familiar with PERL at all, but I have a couple of books (Llama and camel) on the way to start picking it up. I have been doing so much programming lately at my job that I need to pick it up as BASH is great, but PERL seems to be a bit more powerful.
 
Old 10-31-2011, 10:59 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
When I added the #!/bin/bash into the file, it stopped giving the error of [[ cannot be found.
This tells us that your default shell is not bash and hence the previous errors.
Quote:
It does still add the unnecessary .wav at the end of the files.
I am not sure I understand what you mean? Assuming the format you have previously supplied for a naming convention is correct
then the following two line:
Code:
newfile="${file%% *}";
newfile="${file%% *}_$((cnt++))";
Both of these will remove any current extensions and so the following is required to replace the .wav extension:
Code:
mv "${file}" "${newfile}.wav";
The only way this will add and additional .wav to the end is if any of the files you are parsing are not of the correct format you previously informed us of.
 
Old 10-31-2011, 02:22 PM   #24
giantanakim
LQ Newbie
 
Registered: Oct 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
Grail:

What I mean by the unnecessary .wav is that when any file which has already been through the scripted process once is still in the folder when the script is run again has a .wav appended to the end.
 
Old 11-01-2011, 02:37 AM   #25
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
What I mean by the unnecessary .wav is that when any file which has already been through the scripted process once is still in the folder when the script is run again has a .wav appended to the end.
100% correct The script is not designed to run over the same data. This is to do with my explanation of the fact that if the data is not in the original format (which it is not once the script
has been run once) it will not produce the desired results.

Original format (as you described it):
Code:
2033028167 by xxxxxx@xxxxxx @ 4_32_36 PM.wav
Once the script has run, and we will assume this to be the only file with the initial string '2033028167', this will look like:
Code:
2033028167.wav
Now if you run the script again and this file has already been changed:
Code:
for file in *.wav; # Assume we are up to our changed file, then file = 2033028167.wav
do
    cnt=1;
    newfile="${file%% *}"; # This says remove everything after a space in $file, but we have none so newfile = 2033028167.wav
    while [[ -f "${newfile}.wav" ]]; # Does 2033028167.wav.wav exist ... No so do not enter while
    do
        newfile="${file%% *}_$((cnt++))";
    done;
    mv "${file}" "${newfile}.wav"; # This line now says ... mv "2033028167.wav" "2033028167.wav.wav"
done
 
1 members found this post helpful.
Old 11-01-2011, 12:21 PM   #26
giantanakim
LQ Newbie
 
Registered: Oct 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thank you for the clarification, I really appreciate it. Given the situation, it is probably easiest just to move the file into a different folder as it is renamed, so that there are never any that have been renamed already.

Thanks again grail! You have been a lot of help!
 
  


Reply


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
Bash script for sorting and renaming multiple mp3 files by id3 tags simonloach Linux - General 8 02-16-2013 09:07 AM
Bash script for renaming files (all odd) snaggletooth1134 Programming 10 05-01-2012 12:26 AM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration