LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 12-12-2009, 08:10 PM   #1
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Rep: Reputation: 15
Move files into a folder created by Perl


Hey I want to do some stuff to some files then create a folder based on the file name, then put the file inside of the folder that was created for it...then go to the next file and do the same thing. My code actually works but only for the last file...Can you please help?!

Code:
#!/usr/bin/perl 


# Use MP3::Tag Module
use MP3::Tag;

@file = <*.mp3>; # read every mp3 in current directory into array (one by one)
foreach (@file) { 
$file = $_; # Anchor the file to a variable

#Parse the file name into variables
@array = /(.*)_([0-9]{2})_([0-9]{4}).{0,1}_(.*)_(.*)(_side)s*_s*([12])(.*)/i;

#print "scalar(@array) \n";
$nums = @array;


#Remove underscores
foreach (@array) {
tr/_/ /d;
}

# Make directories

mkdir "@array[4] - @array[2]-@array[0]-@array[1]- @array[3]";

#Do some stuff


`mv $file ./"@array[4] - @array[2]-@array[0]-@array[1]- @array[3]"/`;
 
Old 12-12-2009, 08:16 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Suggestion:
Quote:
1. Print out the text of your "mv" statement when it executes.

2. Cut-and-paste it, run the same statement by hand (from the command line).

3. You'll exclaim "Whoops", and understand why you're only seeing the LAST file.

4. Fix it: change your "mv" syntax to do what you want.

5. Once you've got it working correctly in the command line, do it in Perl.
Sound like a plan?
 
Old 12-12-2009, 08:25 PM   #3
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Original Poster
Rep: Reputation: 15
Quote:
1. Print out the text of your "mv" statement when it executes.

2. Cut-and-paste it, run the same statement by hand (from the command line).

3. You'll exclaim "Whoops", and understand why you're only seeing the LAST file.

4. Fix it: change your "mv" syntax to do what you want.

5. Once you've got it working correctly in the command line, do it in Perl.
Hmm I still don't see the logic in why its not working I have assigned the "current file $_" to $file variable and given it the correct directory. It seems it should mv each file into its own directory. I need more assistance...btw how can I possibly run this command from the shell if I haven't defined the variables contained in it??
 
Old 12-12-2009, 09:09 PM   #4
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Original Poster
Rep: Reputation: 15
I found the error...I needed to expand the scope of "foreach" thanks.
 
Old 12-12-2009, 10:41 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Q: how can I possibly run this command from the shell?
A: "print" is a simple - but invaluable - way to debug your Perl programs.
Quote:
EXAMPLE:
printf "file=" . $file;
printf "array[4]=" . @array[4];
...
`mv $file ./"@array[4] - @array[2]-@array[0]-@array[1]- @array[3]"/`;
'Glad you found the problem
 
  


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
Shell script to monitor files created on a folder cmadiam82 Programming 9 05-11-2013 01:59 AM
Sort files under a folder by created date Kunsheng Programming 3 05-11-2009 05:58 AM
Automatically set permissions of new files created within a specific folder Lorian Linux - Desktop 2 03-03-2007 04:17 PM
How to automatically move files from one folder into another folder? xmrkite Linux - Software 6 11-05-2006 11:39 AM
I have 8 character file name, no extension files being created in my dbaseIII+ folder bonzo Linux - General 2 01-22-2004 07:49 AM


All times are GMT -5. The time now is 09:36 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