Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

system(find) unable to find a file even if the file exists

by SitAllDay (Initiate)
on Feb 14, 2018 at 08:48 UTC ( [id://1209115]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys I am new to Perl. Recently I received a task to find a file using system(file...), however the output keeps displaying File Not Found...

use strict; use warnings; my $destination = 'C:\Users\Documents'; if (-d ${destination}) { print "Info: Detected folder exists. \n"; } else { print "Folder not found!\n"; } print "Unzip destination folder\n"; system("find $destination -name '*.txt'");

And here is what shown in my terminal:

C:\Users\Documents>perl test3.pl

Info: Detected folder exists.

Unzip destination folder

File not found - '*.txt'

Hopefully someone can help me with this. Thanks a lot!

Replies are listed 'Best First'.
Re: system(find) unable to find a file even if the file exists
by BrowserUk (Patriarch) on Feb 14, 2018 at 09:22 UTC

    On windows, the find, command searches for text within a file, or set of files described by a wildcard expression:

    C:\>find /? Searches for a text string in a file or files. FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename +[ ...]] /V Displays all lines NOT containing the specified string. /C Displays only the count of lines containing the string. /N Displays line numbers with the displayed lines. /I Ignores the case of characters when searching for the str +ing. /OFF[LINE] Do not skip files with offline attribute set. "string" Specifies the text string to find. [drive:][path]filename Specifies a file or files to search. If a path is not specified, FIND searches the text typed at the prompt or piped from another command.

    Think simple grep-like tool.

    Your use of -name suggests you are trying to use a *nix-style find command; which is available from various sources, but from the error you are getting, is not the one that is being run.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Re: system(find) unable to find a file even if the file exists
by ikegami (Patriarch) on Feb 14, 2018 at 09:51 UTC

    You are providing a (badly-constructed) Bourne shell command that expects to invoke the unix find tool, but you are invoking the Windows command shell which finds the very different Windows find tool.

    Perhaps you should be using File::Find::Rule?

Re: system(find) unable to find a file even if the file exists
by 1nickt (Canon) on Feb 14, 2018 at 14:06 UTC

    Try Path::Iterator::Rule.

    use strict; use warnings; use Path::Iterator::Rule; my $destination = 'C:/Users/Documents'; my $rule = Path::Iterator::Rule->new->file->name('*.txt'); print "$_\n" for $rule->all( $destination ); __END__

    Hope this helps!


    The way forward always starts with a minimal test.
Re: system(find) unable to find a file even if the file exists
by choroba (Cardinal) on Feb 14, 2018 at 09:03 UTC
    I'm not sure what find does on MSWin, but the first thing to do would be to remove the single quotes around *.txt: single quotes aren't special in Windows' shell, so they are understood as part of the file name.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: system(find) unable to find a file even if the file exists
by Anonymous Monk on Feb 14, 2018 at 09:04 UTC

    try: C:/ iso C:\.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found