Re: [問題] 請問關於在windows 下的* 字元
※ 引述《n0tme (who did this?)》之銘言:
: 如果我下 perl -pie '...' abc.txt 可以單處理一個檔
: 但是後面想接萬用字元 * 處理所有的.txt檔,windows會顯示錯誤
: 之前在unix 或 windows 下的cygwin可以使用*
: 想請問windows下要怎麼一次處理多個檔案呢 ?
http://www.dunwich.org/soft/wildcardext.html
Command-line Wildcard Expansion
The default command shells on DOS descendant operating systems (such as they
are) usually do not expand wildcard arguments supplied to programs. They
consider it the application's job to handle that. This is commonly achieved
by linking the application (in our case, perl) with startup code that the C
runtime libraries usually provide. However, doing that results in
incompatible perl versions (since the behavior of the argv expansion code
differs depending on the compiler, and it is even buggy on some compilers).
Besides, it may be a source of frustration if you use such a perl binary with
an alternate shell that *does* expand wildcards.
Instead, the following solution works rather well. The nice things about it
are 1) you can start using it right away; 2) it is more powerful, because it
will do the right thing with a pattern like */*/*.c; 3) you can decide
whether you do/don't want to use it; and 4) you can extend the method to add
any customizations (or even entirely different kinds of wildcard expansion).
C:\> copy con c:\perl\lib\Wild.pm
# Wild.pm - emulate shell @ARGV expansion on shells that don't
use File::DosGlob;
@ARGV = map {
my @g = File::DosGlob::glob($_) if /[*?]/;
@g ? @g : $_;
} @ARGV;
1;
^Z
C:\> set PERL5OPT=-MWild
C:\> perl -le "for (@ARGV) { print }" */*/perl*.c
p4view/perl/perl.c
p4view/perl/perlio.c
p4view/perl/perly.c
perl5.005/win32/perlglob.c
perl5.005/win32/perllib.c
perl5.005/win32/perlglob.c
perl5.005/win32/perllib.c
perl5.005/win32/perlglob.c
perl5.005/win32/perllib.c
Note there are two distinct steps there: 1) You'll have to create Wild.pm and
put it in your perl lib directory. 2) You'll need to set the PERL5OPT
environment variable. If you want argv expansion to be the default, just set
PERL5OPT in your default startup environment.
If you are using the Visual C compiler, you can get the C runtime's command
line wildcard expansion built into perl binary. The resulting binary will
always expand unquoted command lines, which may not be what you want if you
use a shell that does that for you. The expansion done is also somewhat less
powerful than the approach suggested above.
==
今天遇到這問題 (真不敢相信今天才遇到...)
查到這個解法,
另外還查到說,Unix-like 下可行,是因為 shell 幫我們傳的關係,
換句話說,並不是 Unix-like 下可以,而是 shell dependent 的。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.122.230.41
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
Perl 近期熱門文章
PTT數位生活區 即時熱門文章