in reply to problem running executable in subdirectories

Update: See ikegami's reply for a working solution :)

Here is one way to get just the subdirectories of the current directory (using File::Slurp, grep and -d), then run your command in each dir (using chdir and system):

use strict; use warnings; use File::Slurp qw(read_dir); my @dirs = grep { -d } read_dir('./'); for (@dirs) { chdir $_; system '/somepath/foo.exe'; }

If you want to run it in all directories recursively, then find or File::Find would be appropriate.

You should be specific about the problem you are having. Why doesn't it work for you?

You should choose a more meaningful title for your question, like: "Problem running command in subdirectories.".

Replies are listed 'Best First'.
Re^2: trouble with a simple script
by ikegami (Patriarch) on Jul 07, 2010 at 19:14 UTC

    You need to undo the chdir since you are using paths relative to the original directory.

    Here's a convenient way of doing that:

    use File::chdir qw( $CWD ); use File::Slurp qw( read_dir ); my @dirs = grep -d, read_dir('.'); for (@dirs) { local $CWD = $_; system '/somepath/foo.exe'; }

    Or since we know we're just going one level down,

    use File::Slurp qw( read_dir ); my @dirs = grep -d, read_dir('.'); for (@dirs) { chdir $_ or die; system '/somepath/foo.exe'; chdir '..' or die; }

    Another way is to make the paths absolute.

    Update: Added alternative.