Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Check if file exists in multiple directories

by Bama_Perl (Acolyte)
on May 21, 2015 at 18:01 UTC ( [id://1127372]=note: print w/replies, xml ) Need Help??


in reply to Re: Check if file exists in multiple directories
in thread Check if file exists in multiple directories

Yes if there is a .txt file, move the file called outFile to the new directory. Correct.
  • Comment on Re^2: Check if file exists in multiple directories

Replies are listed 'Best First'.
Re^3: Check if file exists in multiple directories
by 2teez (Vicar) on May 21, 2015 at 18:23 UTC

    Hi Bama_Perl,

    What I don't understand is do you want the file "outFile" moved or copied? Because, if you move it to say the first directory that has '*.txt', what happens to another say "fourth" directory that also has '*.txt'?

    Secondly, I think you should be using a perl core module like File::Copy to achieve your aim instead of using mv command in system like you are doing.

    Third, you may use File::Find module to do the searching of directories for you.
    Lastly, use three arguments open function and a lexical scoped file-handler (this is an old wisdom that still holds true in a way today)

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Hi 2teez, I want to copy the $outFile to a new directory. I don't want to move it to a directory that has *.txt. In other words, as I loop through each directory, if that directory has a *.txt file, then I want to move the $outfile within that directory to a new directory. If that directory does not have a *.txt file, then nothing will be moved to a new directory. I'll look into File::Copy to see how that can help me. Thanks!
Re^3: Check if file exists in multiple directories
by hdb (Monsignor) on May 21, 2015 at 18:19 UTC

    Your $dir probably has an extra newline at the end, try chomp @tablea; after reading the dirlist file.

      #!/usr/bin/perl use warnings; use Cwd; $out = "outFile"; $newdir = $newdirectory; open(TABLEA, "dirlist"); @tablea = <TABLEA>; foreach $dir (@tablea) { chomp @tablea; chdir $dir; print(cwd); print "\n"; if (glob("*.txt")) { system("mv $out $newdir"); } }
      If I add the chomp and I print the current working directory, the only directory that gets printed is the first directory, multiple times(as many times there are files in the main directory). Any other thoughts? Thanks.

        Can you add a use strict; at the beginning of your code? And where do you assign a value to $newdirectory? As the code stands it is undefined and therefore your target directory is not specified.

        The chomp should be placed before the loop.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1127372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found