If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Searching CSV

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-09-06, 12:29
techcoor techcoor is offline
Registered User
 
Join Date: Jun 2006
Posts: 2
Searching CSV

To start off, I am new to perl....

Here's what I'm trying to do. I want to verify a file exist.
Here are the files:
1file_15895_123.csv
2file_13232_342.csv
3file_13242_314.csv

I have the user type in what file they want to verify. Now, currently, in order for it to work, they have to type in the entire file name. What i'm wanting to do is have them just type in the first part of the file name; ex. 1, 2 ,3. I'm trying to figure out how to add a wild card that will automatically find everything after what the user has inputted. I keep putting a * but that doesn't work for me...

Here's my code for that portion:



Code:
sub findfile
{

	my $line = "";
	my @fields;

	$filetoscan = "C:/FilePath/" . $filename . ".csv";

	if (-e $filetoscan)
	{
		open(FILETOSCAN, $filetoscan);
		while ($line = readline(FILETOSCAN))
		{
			#print("$line\n");
			@fields = split(',', $line);

			if ($fields[0] =~ "Total")
			{
				if ($fields[1] =~ "0")
				{
					print ("\nThis is Okay!\n");
				}
				else
				{
					print ("\nThis is not Okay!\n");
				}
			}
		}
		close(FILETOSCAN);	
	}
	else
	{
		print ("\nFile does not exist!\n");
	}

}

Another problem if you have time....what happens if it files 2 of the files that start off with the same # or Letter?

Thanks for your help!
Reply With Quote
  #2 (permalink)  
Old 06-09-06, 14:10
KevinADC KevinADC is offline
Registered User
 
Join Date: Feb 2006
Posts: 56
assume the person has already entered "2" as the file to find and validate and we have that in $file. To find all the files that start with 2 and end with .csv:

Code:
my $file = 2;
my $dir = 'C:/FilePath/';
my $file_ext = '.csv';

my @files_found = <$dir$file*$file_ext>;
print "$_\n" for @files_found;
but it will find all files that start with 2 and end with .csv, for example:

1file_15895_123.csv
2file_13232_342.csv
3file_13242_314.csv
2file_13232_789.csv

the above code will find:

2file_13232_342.csv
2file_13232_789.csv

if you don't want multiple matches you will have to narrow down the search pattern or use other criteria, like last modified date or creation date of file, or whatever is appropriate. Or you can display the list of all files found and direct the user to now enter the exact one to check based on the list.
Reply With Quote
  #3 (permalink)  
Old 06-09-06, 15:18
techcoor techcoor is offline
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks!
I actually came up with another one but your code looks alot more efficient.

Thanks for the reply!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On