Results 1 to 3 of 3

Thread: Searching CSV

  1. #1
    Join Date
    Jun 2006
    Posts
    2

    Unanswered: 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!

  2. #2
    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.

  3. #3
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •