How to replace c:\Program Files with Program Files (x86) in allscripts?

C

clearguy02

Hi experts,

I have a parent folder, C:\Test and it has a few sub-folders (and its
files) and files and I am now forced to do the following change in all
files starting from its parent folder, c:\Test

To replace a string, "c:\\program files\" or "c:\PROGRA~1" with "C:\
\Program Files (x86)" in all files.

Here is some thing I am trying with:

C:\>perl -i.bak -pe "s/Progra~1/Program Files (x86)/g" `find "C:\
\Test" -type f -name "*.pl" `

Error is:
Can't open `find: No such file or directory.
Can't do inplace edit: C:\\Test is not a regular file.
Can't open -type: No such file or directory.
Can't open f: No such file or directory.
Can't open -name: No such file or directory.
Can't open *.pl`: Invalid argument.

When I run the find command (find C:\Test -type f -name "*.pl" ), it
is returning the correct set of files, but it not working with the
perl commandline.

Where am I doing wrong?

--J
 
J

Jürgen Exner

I have a parent folder, C:\Test and it has a few sub-folders (and its
files) and files and I am now forced to do the following change in all
files starting from its parent folder, c:\Test

To replace a string, "c:\\program files\" or "c:\PROGRA~1" with "C:\
\Program Files (x86)" in all files.

Use File::Find to recursively find all files, the test -f will probably
come in handy to determine if the file is a regular file, then open()
each file, and do the replace.
If the files are small you can possibly slurp them in and do the replace
globally before writing the content back in one go. If the files are
large you may need to loop through then line by line and write each
modified line back into a temporary file before renaming the new file
into the old.
Here is some thing I am trying with:

C:\>perl -i.bak -pe "s/Progra~1/Program Files (x86)/g" `find "C:\
\Test" -type f -name "*.pl" `

Error is:
Can't open `find: No such file or directory.
Can't do inplace edit: C:\\Test is not a regular file.
Can't open -type: No such file or directory.
Can't open f: No such file or directory.
Can't open -name: No such file or directory.
Can't open *.pl`: Invalid argument.

When I run the find command (find C:\Test -type f -name "*.pl" ), it
is returning the correct set of files, but it not working with the
perl commandline.

Where am I doing wrong?

It appears as if your shell(!) doesn't execute the command `find ....`
but passes it on to perl as just another command line argument. This
isn't a perl problem, but you need to check the documentation of your
shell if and how to execute parts of the command line as a separate
command first.

Having said that, I wouldn't go about it that way anyway. What if your
find returns 2000 results? I don't know of any CLI, that could handle
such a long command line.
Either write a little Perl script using File::Find as I explained above.
Or use the -exec option of the find program to run a simpler Perl script
for each file found, which does the replace on exactly this one file.
Or use SED, it's the perfect tool for such a simple job.

jue
 
C

clearguy02

Assuming windows.  Try the following on a single line.

for %F in (*.pl) do perl -i.bak -pe "s/Progra~1/Program Files (x86)/g" "%F"

It looks goods Len.. thanks a bunch. But what if I have to address a
few sub-folders within c:\TEST folder?
 
G

Gunnar Hjalmarsson

I have a parent folder, C:\Test and it has a few sub-folders (and its
files) and files and I am now forced to do the following change in all
files starting from its parent folder, c:\Test

To replace a string, "c:\\program files\" or "c:\PROGRA~1" with "C:\
\Program Files (x86)" in all files.

Why would you need to do that? I thought that Windows was not case
sensitive.
 
A

A. Sinan Unur

Why would you need to do that? I thought that Windows was not case
sensitive.

I am not sure but I don't think this about case sensitivity. I think he
hard coded the location of Program Files and now the program needs to be
run on 64 bit Windows and he wants to continue with hard-coding this
kind of information.

Well, good luck if the OPs application ever needs to be run on a
non-English version of Windows.

Sinan


--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
W

Wolf Behrenhoff

Ben said:
For reference, the correct way to get the path of the Program Files
directory is to call Win32::GetFolderPath with CSIDL_PROGRAM_FILES. To
get the path of the x86 Program Files directory on 64bit windows, you
need

use constant CSIDL_PROGRAM_FILESX86 => 0x002a;

since Win32 doesn't currently supply this constant.

Right. And in case some old programs use the hard coded string, one
could create a symlink:

mklink /d linkdirectory targetdirectory

This is what Vista also does with old XP folders, for example
"C:\Documents and Settings" is a now symlink to "C:\Users".

- Wolf
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top