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

Discussion in 'Perl Misc' started by clearguy02, May 14, 2009.

  1. clearguy02

    clearguy02 Guest

    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?

    clearguy02, May 14, 2009
    1. Advertisements

  2. 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.
    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.

    Jürgen Exner, May 14, 2009
    1. Advertisements

  3. clearguy02

    clearguy02 Guest

    It looks goods Len.. thanks a bunch. But what if I have to address a
    few sub-folders within c:\TEST folder?
    clearguy02, May 14, 2009
  4. Why would you need to do that? I thought that Windows was not case
    Gunnar Hjalmarsson, May 14, 2009
  5. 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.


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

    comp.lang.perl.misc guidelines on the WWW:
    A. Sinan Unur, May 15, 2009
  6. 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
    Wolf Behrenhoff, May 15, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.