Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

File Globbing in Windows One-Liner

by roho (Bishop)
on Jul 21, 2009 at 02:14 UTC ( [id://781801]=perlquestion: print w/replies, xml ) Need Help??

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

On Unix I can do the following to perform mass changes:
perl -p -i -e 's/old/new' *.txt

Windows does not work with the equivalent command line:
perl -p -i -e "s/old/new/" *.txt

I have tried using -MFile::Glob and -MFile::DosGlob, with and without
<>'s surrounding the file pattern, and I tried glob(*.txt), but nothing works.
I keep getting the error message: The syntax of the command is incorrect.
Has anyone been down this road before? What am I missing?

"Its not how hard you work, its how much you get done."

Replies are listed 'Best First'.
Re: File Globbing in Windows One-Liner
by toolic (Bishop) on Jul 21, 2009 at 02:44 UTC
      Thank you ikegami for the constructive criticism regarding my post and the heads up about -i in windows, and thank you toolic for the solution you found that works great! I appreciate your time and efforts.

      "Its not how hard you work, its how much you get done."

Re: File Globbing in Windows One-Liner
by ikegami (Patriarch) on Jul 21, 2009 at 03:16 UTC

    Windows does not work with the equivalent command line:

    Technically, it's a question of which shell you are using.

    What am I missing

    Hard to tell, you didn't show what's giving the error.

      I am using ActiveState Perl 5.10 on Windows Vista in a command window initiated by cmd.exe.
      The error is produced by the Perl one-liner:

      perl -p -i -e "s/old/new/" *.txt

      "Its not how hard you work, its how much you get done."

        There's no syntax error in that. You said you tried stuff that gave you "The syntax of the command is incorrect."

        By the way, -i with no extension doesn't work in Windows.

Re: File Globbing in Windows One-Liner
by QM (Parson) on Jul 21, 2009 at 19:57 UTC
    I hesitated quite a while before sharing this, but it seems that it can only be improved. (And I did write it some time ago.)

    I hacked up File::DosGlob to suit me a while back. I vaguely recall some problem with globs that didn't match anything, and whether they were silently dropped, or passed through as a glob. Either or both of these may cause needless errors or incomplete data, so I added options and checking for the caller to control this behavior, and was rather paranoid about accepting options that weren't recognized.

    I should go back and clean this up nice with POD, etc., but I'm lazy.

    I also had unsophisticated users running my scripts, and sometimes running them on *nix. They would not need this module, and wouldn't have installed it, but of course it couldn't just be used, so I used this code instead:

    # This BEGIN block avoids including File::DosGlob::Param for non-windo +ws systems BEGIN { if ( $^O =~ /win/i ) { require File::DosGlob::Param; import File::DosGlob::Param qw( dosglob ); } }

    Now, this didn't avoid the issue of the user not having it installed on Windows, but at least it didn't blow up unnecessarily in *nix.

    And finally, an example invocation:

    # convert filename wildcards to actual filenames if ( $^O =~ /win/i ) # only if DOS { if ( exists( $INC{'File/DosGlob/Param.pm'} ) ) # only if loaded { dosglob( "Array_Ref" => \@ARGV, "Remove_Empty_Matches" => 1, "Warn_On_Empty_Matches" => 1 ); } END { if ( ( $^O =~ /win/i ) and not exists( $INC{'File/DosGlob/Param.pm'} ) ) { warn "Consider installing module File::DosGlob::Param...\n +"; } } }

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://781801]
Approved by toolic
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: (3)
As of 2024-03-19 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found