Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Access given folder to parse a file

by ArifS (Beadle)
on Nov 03, 2014 at 14:23 UTC ( [id://1105900]=perlquestion: print w/replies, xml ) Need Help??

ArifS has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to provide a folder name and a file information for parsing. However, I am getting error in the following script-
my $directory = "folder(1)"; my $file = "my log file.log"; my $files = ($directory, "\", $file); print $files; # .\\$directory\\$file open INPUT, "<.\\$files" or die "Can't open 'INPUT': $!"; my @lines = <INPUT>; print "I have access to the folder\n"; close INPUT;
Please let me know.

Replies are listed 'Best First'.
Re: Access given folder to parse a file
by choroba (Cardinal) on Nov 03, 2014 at 14:29 UTC
    $file is a scalar variable, possibly a file path. Why do you assign a list to it?
    my $files = ($directory, "\", $file);

    It might be better to concatenate the strings:

    my $file_path = $directory . '\\' . $file; # OR my $file_path = "$directory\\$file";

    Note that \ is special, to include it literally, you have to backslash it.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Access given folder to parse a file
by toolic (Bishop) on Nov 03, 2014 at 14:32 UTC
    You should always post your exact error message.

    When I merely paste your code into my editor, I immediately see (due to syntax highlighting) this line is a problem:

    my $files = ($directory, "\", $file);

    It is a syntax error to escape the double quote like that. It needs to eventually be followed by another double quote.

    Also, do you use strict and warnings?

Re: Access given folder to parse a file
by Loops (Curate) on Nov 03, 2014 at 14:46 UTC

    Hi ArifS,

    There are a number of problems with your code that would have been shown to you had you started your program off using strict and warnings. There is a function provided by File::Spec::Functions named catfile, which will put the proper separator between your directory and file. I'd also suggest using the 3 argument version of open along with a lexical variable instead of a bareword "INPUT". Example changes shown below along with use of Win32 GetFolderPath to use the Desktop as the target directory:

    use strict; use warnings; use File::Spec::Functions; use Win32 qw(CSIDL_DESKTOP); my $directory = Win32::GetFolderPath(CSIDL_DESKTOP); my $file = "my log file.log"; my $path = catfile($directory,$file); open my $input, '<', $path or die "Can't open INPUT: $!"; my @lines = <$input>; print "I have access to the folder\n"; close $input;
      I tried your script replacing with the following and getting an error-
      use Win32 qw(folder(1)); my $directory = Win32::GetFolderPath(folder(1));
      Error: "folder(1)" is not exported by the Win32 module Can't continue after import errors at c:\temp\dir5B6B.tmp\AccessFolder +.pl line 8. BEGIN failed--compilation aborted at c:\temp\dir5B6B.tmp\AccessFolder +line 8.
      Please let me know.

        Please explain what the following line is supposed to do:

        use Win32 qw(folder(1));

        Where did you find this syntax documented in the documentation of Win32?

Re: Access given folder to parse a file
by Corion (Patriarch) on Nov 03, 2014 at 14:30 UTC

    What errors do you get?

    Maybe you want to explain what the following line in your program does:

    my $files = ($directory, "\", $file);

    What steps have you undertaken to verify that this line works?

Re: Access given folder to parse a file
by james28909 (Deacon) on Nov 04, 2014 at 08:37 UTC
    "I am trying to provide a folder name and a file information for parsing"
    use File::Slurp; my @dir = read_dir($ARGV[0]); my $file = $ARGV[1]; if ($file ~~ @dir){ print uc"$file FOUND"; } else{ print uc"$file not found"; } #usage - script_name.pl [directory] [file_name] #eg. - script_name.pl [folder(1)] [my_log_file.log]
    this simply will take your first arg and read the specified directories contents into the array "@dir". then it will take the second argument "$file" and chomp it so there is no trailing unwanted characters or spaces.
    THEN in the if statement, it simply looks for the filename in the array "@dir" (which contains all the filenames from the directory you specify.)

    you should be able to specify any directory in the script as in "folder(1)" OR you should be able to do something like "C:/path/to/folder(1)".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1105900]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found