Connecting Tech Pros Worldwide Forums | Help | Site Map

File Directory

Member
 
Join Date: Sep 2007
Location: Virginia Beach, VA
Posts: 57
#1: Dec 5 '07
Okay I'm back again,
I have a list of files in a directory. i am starting at the top of the list and when i'm down with that first file, i am deleting it by using 'unlink'. and then i want to move on to the next file that is at the top of the list.

how do i select that first file (regardless of what the file name is) and save that file name into a variable?

Terra

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#2: Dec 5 '07

re: File Directory


Quote:

Originally Posted by Perl Beginner

Okay I'm back again,
I have a list of files in a directory. i am starting at the top of the list and when i'm down with that first file, i am deleting it by using 'unlink'. and then i want to move on to the next file that is at the top of the list.

how do i select that first file (regardless of what the file name is) and save that file name into a variable?

Terra

First, can you please post your code? We want to see how you are trying to do this.

Once we know what you are trying, we can then make the necessary suggestions.

Regards,

Jeff
Member
 
Join Date: Sep 2007
Location: Virginia Beach, VA
Posts: 57
#3: Dec 5 '07

re: File Directory


Well, there not much code to post. i have a directory that has more than one file in it and i would like to just pick the first one. My code saves files into the $destination_dir folder and from this directory i want to select the first one without using the file name.

For example, the directory itself would have the file names
test_results0001.txt
test_results0002.txt
test_results0003.txt

once i unlink the test_results0001.txt then the 0002 is now the first one.
test_results0002.txt
test_results0003.txt

when i'm done with that one, i will unlink it and move to the next one. but i don't know how to grab it to use it. so you can see why i can't go by the file name.
[HTML]
$destination_dir = C:\\Test Data\\Test_Results\\";
.
.
.
@result_files = <'$destination_dir\\ '>; #this is where i don't know how to call it

foreach $file (@result_files)
{open (RESULT, "<$file") or die "and error occurred: $!";}[/HTML]
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#4: Dec 5 '07

re: File Directory


Quote:

Originally Posted by Perl Beginner

Well, there not much code to post. i have a directory that has more than one file in it and i would like to just pick the first one. My code saves files into the $destination_dir folder and from this directory i want to select the first one without using the file name.

For example, the directory itself would have the file names
test_results0001.txt
test_results0002.txt
test_results0003.txt

once i unlink the test_results0001.txt then the 0002 is now the first one.
test_results0002.txt
test_results0003.txt

when i'm done with that one, i will unlink it and move to the next one. but i don't know how to grab it to use it. so you can see why i can't go by the file name.
[HTML]
$destination_dir = C:\\Test Data\\Test_Results\\";
.
.
.
@result_files = <'$destination_dir\\ '>; #this is where i don't know how to call it

foreach $file (@result_files)
{open (RESULT, "<$file") or die "and error occurred: $!";}[/HTML]

Ok, let me see if I understand this. You have a directory that has "x" number of files in it. You want to get a list of the files in that directory an delete them, regardless of the file name? Is that correct? It looks like you want to open each file, individually and do something with the contents and then delete it.

Can you just clarify for me?

As for the line above you "don't know how to call it", is that the line you are getting the list of files and plugging them into an array?

Regards,

Jeff
Member
 
Join Date: Sep 2007
Location: Virginia Beach, VA
Posts: 57
#5: Dec 5 '07

re: File Directory


I'm sorry, that foreach loop shouldn't be there. it should be:

Expand|Select|Wrap|Line Numbers
  1. $result_files = <'destination_dir\\...'>;
  2.  
  3. open (RESULT, "<$result_files");
And you just made me see what my error is. i think i have the solution!

Thanks!
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#6: Dec 5 '07

re: File Directory


Quote:

Originally Posted by Perl Beginner

I'm sorry, that foreach loop shouldn't be there. it should be:

Expand|Select|Wrap|Line Numbers
  1. $result_files = <'destination_dir\\...>;
  2.  
  3. open (RESULT, "<$result_files");
And you just made me see what my error is. i think i have the solution!

Thanks!

Ok, then I await your new code. :-)

Regards,

Jeff
Reply