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

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

  1. 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?

    --J
    , May 14, 2009
    #1
    1. Advertising

  2. Re: How to replace c:\Program Files with Program Files (x86) in all scripts?

    wrote:
    >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
    Jürgen Exner, May 14, 2009
    #2
    1. Advertising

  3. Guest

    On May 13, 7:36 pm, l v <> wrote:
    > wrote:
    > > 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

    >
    > 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"
    >
    > --
    >
    > Len


    It looks goods Len.. thanks a bunch. But what if I have to address a
    few sub-folders within c:\TEST folder?
    , May 14, 2009
    #3
  4. wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 14, 2009
    #4
  5. Re: How to replace c:\Program Files with Program Files (x86) in all scripts?

    Gunnar Hjalmarsson <> wrote in
    news::

    > wrote:
    >> 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.


    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 <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, May 15, 2009
    #5
  6. Ben Morrow schrieb:
    > 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
    Wolf Behrenhoff, May 15, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Chau
    Replies:
    11
    Views:
    21,983
    Anoop M
    Jan 2, 2004
  2. gswork
    Replies:
    6
    Views:
    663
    Will Hartung
    Oct 7, 2004
  3. Allen

    x86 and C++

    Allen, Nov 16, 2003, in forum: C++
    Replies:
    8
    Views:
    491
    Joe Estock
    Nov 17, 2003
  4. Al C
    Replies:
    3
    Views:
    326
    Jon Bell
    Sep 17, 2004
  5. jason

    Python On WinCE.Net (x86)

    jason, Nov 29, 2003, in forum: Python
    Replies:
    0
    Views:
    376
    jason
    Nov 29, 2003
Loading...

Share This Page