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

Hi All, I wanted to run just "ls" command in windows for one of my application. I installed cygwin and activeperl. Now i have written perl script which will process some files. I have a syntax foreach my $dir (`ls`) which will list all the directories under current directory. How i can i make this perl to execute under cygwin so that 'ls' command will work. If anyother way is there then fine... Please help...
  • Comment on Help me to run linux command in windows

Replies are listed 'Best First'.
Re: Help me to run linux command in windows
by polettix (Vicar) on Aug 22, 2007 at 10:22 UTC
    If another way is there then fine...
    Another way for doing what? If you're only looking for a way to obtain the list of files in a directory, you could go low-level with opendir/readdir/closedir, or try to take a look to the more structured approaches offered by File::Find and its colleagues on CPAN.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Help me to run linux command in windows
by bart (Canon) on Aug 22, 2007 at 10:34 UTC
    If your aim is to write portable code, then don't depend on external programs for such simple tasks.
Re: Help me to run linux command in windows
by moritz (Cardinal) on Aug 22, 2007 at 10:29 UTC
    Instead of `ls` you can use glob('*').
Re: Help me to run linux command in windows
by Anonymous Monk on Aug 22, 2007 at 11:13 UTC
    Make a file called ls.bat and put it in a system directory (like system32 or Windows). Put

    dir %1

    in the file. Reading your post again it seems like you want to call it from within an application - if it's a windows app, then why not just use 'dir'?
Re: Help me to run linux command in windows
by technojosh (Priest) on Aug 22, 2007 at 14:00 UTC
    This should work on windows just fine:

    my $fileDir = someawesomepath; opendir(DIR, "$fileDir") || die $!; while (defined(my $file = readdir(DIR))) { next if $file =~ /^\.\.?$/; # Skip the DIR #do stuff with $file } closedir(DIR);
Re: Execute Perl Under Cygwin in Windows!!! Help!!!
by Tux (Canon) on Aug 22, 2007 at 10:45 UTC

    WTF use external commands for something perl implements with native calls?

    opendir DIR, "." or die "$!"; foreach my $dir (grep { -d $_ } readdir DIR) { # Do something with $dir } closedir DIR;

    Using File::Find might be an option too


    Enjoy, Have FUN! H.Merijn
Re: Help me to run linux command in windows
by syphilis (Archbishop) on Aug 23, 2007 at 08:34 UTC
    I installed cygwin and activeperl

    Since you have cygwin, you could just run 'ls' in cygwin's bash shell ... and use cygwin's perl (in the same shell) instead of ActivePerl.

    But if you want to use ActivePerl, and still be able to run 'ls', then just add C:\cygwin\bin to the end of the path environment variable. (That's assuming you've installed Cygwin into C:\cygwin - otherwise amend accordingly.)

    Cheers,
    Rob
      Hi All, Many thanks!! What i did is adding the cygwin bin to the path variable so ls started working and my coding has succedded.........Thanks!!!
Re: Help me to run linux command in windows
by Anonymous Monk on Aug 22, 2007 at 18:05 UTC
    If your question is how to make the "ls" command run under windows, then one "simple" way would be to create a batch file in the windows/system32/ (or other directory sitting in the execution path) that will execute the cygwin'd ls perl script for you.
Re: Help me to run linux command in windows
by nega (Scribe) on Aug 22, 2007 at 20:34 UTC
    Perl can provide you all the information that ls would. You should stop and think about what you really want to accomplish.

    But if you don't want to do that...
    http://unxutils.sourceforge.net/

Re: Execute Perl Under Cygwin in Windows!!! Help!!!
by Razvanica (Novice) on Aug 22, 2007 at 10:14 UTC
    I don't really understand what you are trying to do, but Windows has the 'dir' command which does almost the same thing as 'ls' on Linux.
      I wanted the output format of ls command from windows but dir gives out junk data... Because my application has to login to each of the folders and process files and update DB.