develooper Front page | perl.beginners | Postings from February 2002

RE: change all files in directory

Thread Previous | Thread Next
From:
Hanson, Robert
Date:
February 1, 2002 08:02
Subject:
RE: change all files in directory
Message ID:
11A304418ECBAB498FBF10ABCEA5E499D1CDD6@hq2kax1.nj.aptegrity.com
Oh, you're on Windows, it may not work because of the shell ("cmd").

In Windows the single quotes around the code usually need to be double
quotes, and it doesn't seem to be expanding *.cpp when on Unix (types) it
will expand that to all of the individual file names.

I guess you need to write a script then like you were originally planning to
do.  ...Or if you want to be bold, install Cygwin (http://www.cygwin.com/)
which includes tons of Unix utilities that will run on Win95/98/ME/NT/2K.
Among the utilities is "bash", and command shell which you can use instead
of "cmd" on NT/2K and "command" on Win95/98/ME.  After using bash for a
while you will wonder why you ever wanted to use "cmd" to begin with (I know
I do).

Rob


-----Original Message-----
From: Booher Timothy B 1stLt AFRL/MNAC
[mailto:timothy.booher@eglin.af.mil]
Sent: Friday, February 01, 2002 10:46 AM
To: Brett W. McCoy; Booher Timothy B 1stLt AFRL/MNAC
Cc: beginners@perl.org
Subject: RE: change all files in directory


When I try to run the one-liners I get:

Can't open *.cpp: Invalid argument.
Can't open *.hpp: Invalid argument.

But when I do a dir command I get:
target_functions.cpp    target_modules.cpp      global_constants.hpp
global_header.hpp       class_functions.cpp     S2b_4.opt

Clearly these files are there . . . or am I just doing something silly . . .

tim

-----Original Message-----
From: Brett W. McCoy [mailto:bmccoy@chapelperilous.net]
Sent: Friday, February 01, 2002 9:47 AM
To: Booher Timothy B 1stLt AFRL/MNAC
Cc: beginners@perl.org
Subject: Re: change all files in directory

On Fri, 1 Feb 2002, Booher Timothy B 1stLt AFRL/MNAC wrote:

> Hello, I am trying to change all files in a directory that contain the
> strings Satellite satellite and SATELLITE that I need to change to target,
> Target and TARGET. Because many of these are C++ source files I need to
> preserve the case. I was thinking of the following script:
>
> #!/usr/bin/perl -w
> #UNTESTED
>
> @FilesInDirectory = <*.cpp *.hpp *.asc>;
> foreach $FileName (@FilesInDirectory) {
>             open(IN, $FileName);
>             while<IN> {
> $_ =~ s/satellite/target/;
> $_ =~ s/Satellite/Target/;
> $_ =~ s/SATELLITE/TARGET/;}
> }
>
> but this just doesn't seem as efficient as it can be. I was trying to
think
> of regex that could do it all in one line but it seemed so much simpler to
> do it in three.

This can actually be done on the command-line:

perl -pi.bak -e 's/satellite/target/g' *.cpp *.hpp. *.asc

There's nothing wrong with doing it in three steps.

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
"Trust me":
        Translation of the Latin "caveat emptor."

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About