globbing mess

Discussion in 'Perl Misc' started by MSG, Jan 14, 2006.

  1. MSG

    MSG Guest

    I can't seem to get a grasp on globbing when a directory has a white
    space in it.
    Here is my code snippet on Win32:

    my $dir = 'c:/program files';
    my @files = glob "$dir/*";
    foreach (@files) { print $_, "\n" };

    the output is just:
    c:/program
    instead of a list of files/directories.

    I found two ways to get around the problem:
    one is to add a backslash in front of the white space like this
    my $dir = 'c:/program\ files';
    the other is to double on quotes like this:
    my $dir = '"c:/program files"';

    But I am wondering what is the escaping/quoting rule with globbing.

    One other thing seems odd: It doesn't matter if there is a trailing
    slash or not:
    both $dir='c:/program\ files' and $dir='c:/program\ files/' work. I
    would think that
    c:/program files// would mess up the code...
     
    MSG, Jan 14, 2006
    #1
    1. Advertising

  2. MSG

    Matt Garrish Guest

    "MSG" <> wrote in message
    news:...
    >I can't seem to get a grasp on globbing when a directory has a white
    > space in it.
    > Here is my code snippet on Win32:
    >
    > my $dir = 'c:/program files';
    > my @files = glob "$dir/*";


    my @files = glob "'$dir/*'";

    Should do the trick. Be aware that you're not just going to get files, but
    directories as well. If you just want files try *.* instead.

    Matt
     
    Matt Garrish, Jan 14, 2006
    #2
    1. Advertising

  3. Matt Garrish wrote:
    > "MSG" <> wrote in message
    > news:...
    > >I can't seem to get a grasp on globbing when a directory has a white
    > > space in it.
    > > Here is my code snippet on Win32:
    > >
    > > my $dir = 'c:/program files';
    > > my @files = glob "$dir/*";

    >
    > my @files = glob "'$dir/*'";
    >
    > Should do the trick. Be aware that you're not just going to get files, but
    > directories as well. If you just want files try *.* instead.


    what about extensionless files?
     
    it_says_BALLS_on_your forehead, Jan 15, 2006
    #3
  4. MSG

    Dr.Ruud Guest

    Matt Garrish:

    > my @files = glob "'$dir/*'";
    >
    > Should do the trick. Be aware that you're not just going to get
    > files, but directories as well. If you just want files try *.*
    > instead.


    The presence or absence of a dot in the name, is not the distiguishing
    feature between files and directories.

    #!/usr/bin/perl

    use strict; use warnings;
    use File::Glob ':glob';

    { local $\ = $/;
    for (bsd_glob '*', GLOB_MARK | GLOB_NOSORT | GLOB_ERR) {
    print if m(/$);
    }
    }


    I don't know yet why

    while (glob '*') { print }

    works and

    while (bsd_glob '*') { print }

    doesn't.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 15, 2006
    #4
  5. MSG

    Matt Garrish Guest

    "it_says_BALLS_on_your forehead" <> wrote in message
    news:...
    >
    > Matt Garrish wrote:
    >> "MSG" <> wrote in message
    >> news:...
    >> >I can't seem to get a grasp on globbing when a directory has a white
    >> > space in it.
    >> > Here is my code snippet on Win32:
    >> >
    >> > my $dir = 'c:/program files';
    >> > my @files = glob "$dir/*";

    >>
    >> my @files = glob "'$dir/*'";
    >>
    >> Should do the trick. Be aware that you're not just going to get files,
    >> but
    >> directories as well. If you just want files try *.* instead.

    >
    > what about extensionless files?
    >


    You're right, even if they are incredibly rare on Windows...

    Matt
     
    Matt Garrish, Jan 15, 2006
    #5
  6. MSG

    Matt Garrish Guest

    "Dr.Ruud" <> wrote in message
    news:...
    > Matt Garrish:
    >
    >> my @files = glob "'$dir/*'";
    >>
    >> Should do the trick. Be aware that you're not just going to get
    >> files, but directories as well. If you just want files try *.*
    >> instead.

    >
    > The presence or absence of a dot in the name, is not the distiguishing
    > feature between files and directories.
    >


    I know, but I was going to eat dinner and not really thinking things
    though... : )

    Matt
     
    Matt Garrish, Jan 15, 2006
    #6
  7. MSG

    Dr.Ruud Guest

    Matt Garrish:
    > it_says_BALLS_on_your forehead:
    >> Matt Garrish:


    >>> my @files = glob "'$dir/*'";
    >>>
    >>> Should do the trick. Be aware that you're not just going to get
    >>> files, but
    >>> directories as well. If you just want files try *.* instead.

    >>
    >> what about extensionless files?

    >
    > You're right, even if they are incredibly rare on Windows...


    That must be a local thing. I have a Windows-2000 system here with many
    files that have no dot in the name.

    dir c:\ /s/a-d/b | find /v "."

    (skips directory names with a "." too, but already returns plenty)


    And many directory names with an embedded dot too, even as the first
    character.

    dir c:\ /s/ad/b | find "."

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 15, 2006
    #7
  8. MSG <> wrote:

    > I can't seem to get a grasp on globbing when a directory has a white
    > space in it.



    Using readdir/grep is an alternative to globbing.


    > my @files = glob "$dir/*";


    # untested
    opendir DIR, $dir or die "could not open '$dir' $!";
    my @files = grep /^[^.]/, readdir DIR;
    closedir DIR;


    (but you may need to remember to paste the $dir part onto the
    front of each array element.
    )


    > But I am wondering what is the escaping/quoting rule with globbing.



    The docs say glob() uses csh rules.


    > One other thing seems odd: It doesn't matter if there is a trailing
    > slash or not:
    > both $dir='c:/program\ files' and $dir='c:/program\ files/' work. I
    > would think that
    > c:/program files// would mess up the code...



    If you tell us why you think that would mess up the code, then
    we might be able to clear up whatever misunderstanding you have
    that led to that conclusion.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 15, 2006
    #8
  9. MSG

    Matt Garrish Guest

    "Dr.Ruud" <> wrote in message
    news:...
    > Matt Garrish:
    >> it_says_BALLS_on_your forehead:
    >>> Matt Garrish:

    >
    >>>> my @files = glob "'$dir/*'";
    >>>>
    >>>> Should do the trick. Be aware that you're not just going to get
    >>>> files, but
    >>>> directories as well. If you just want files try *.* instead.
    >>>
    >>> what about extensionless files?

    >>
    >> You're right, even if they are incredibly rare on Windows...

    >
    > That must be a local thing. I have a Windows-2000 system here with many
    > files that have no dot in the name.
    >


    Less than 2% of all files is not common (see File-Find question I posted),
    especially when removing uninstall and some application data files from the
    list reduces that number to a fraction of 1%. I'm curious what Windows files
    you have that don't have extensions?

    Matt
     
    Matt Garrish, Jan 15, 2006
    #9
  10. MSG

    Dr.Ruud Guest

    Matt Garrish:

    > [extension-less files on a Windows-system]
    > Less than 2% of all files is not common (see File-Find question I
    > posted), especially when removing uninstall and some application data
    > files from the list reduces that number to a fraction of 1%. I'm
    > curious what Windows files you have that don't have extensions?



    s/common/uncommon/ ?

    On that Win2000-system I see all sort of files without a dot, like:
    - WordPerfect-5 documents, from an age that it was the only application
    in the world (for certain users)
    - data files of an MSDOS bookkeeping program
    - many system files like c:/CFGSAFE/QCINIT/NT*/hlm*
    - c:/Documents and Settings/Administrator/Application
    Data/Microsoft/CryptnetUrlCache/Content/*
    - c:/Perl/*/* :)
    - c:/Program Files/Common Files/InstallShield/Driver/8/Intel 32/ID
    - c:/Program Files/WinSCP3/licence
    - c:/RECYCLER/S-1-5-21-839522115-1383384898-1343024091-500/INFO2
    - c:/WINNT/ShellIconCache
    - c:/WINNT/*/reg*
    - c:/WINNT/repair/*
    - c:/WINNT/system32/drivers/etc/*
    so mostly system files.

    On this system (with 201500 files) there are 4200 such files, of which
    almost 3000 belonging to cygwin.
    So without the cygwin files it is about 0.5% netto.

    There are 6320 directories, of which 1920 with a dot in the name, of
    which about 850 of cygwin or with version numbers, so netto about 17%.
    Most system or application directories, but also many that are
    user-made, for example from website names like "www.example.com/".

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 15, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Philips

    Case-insensitive globbing

    Thomas Philips, Jun 3, 2004, in forum: Python
    Replies:
    4
    Views:
    584
    Derek Chen-Becker
    Jun 3, 2004
  2. Replies:
    5
    Views:
    415
  3. Replies:
    3
    Views:
    293
  4. Daz
    Replies:
    3
    Views:
    340
  5. Owner

    globbing source for C

    Owner, May 12, 2011, in forum: C Programming
    Replies:
    7
    Views:
    312
    Roberto Waltman
    May 16, 2011
Loading...

Share This Page