Announcement

Collapse
No announcement yet.

Finding and replacing text in registry file with quotes and paths and spaces.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Finding and replacing text in registry file with quotes and paths and spaces.

    Hi Everyone, I'm new here - please be nice.

    I want to only use a Batch File for the purpose of replacing something like:

    [HKEY_LOCAL_MACHINE\SOFTWARE\MYPROGRAM]
    "PathToProgram"="C:\\Program Files\\MyDirectory\\MyProgram"

    with this:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MYPROGRAM]
    "PathToProgram"="C:\\Program Files (x86)\\MyDirectory\\MyProgram"

    Some lines are also set out as:
    "PathToProgram"="C:\Program Files (x86)\MyDirectory\MyProgram"
    so it needs to be able to change these also.

    So here I am looking for "\Program Files\" to be replaced (if found) with "\Program Files (x86)\" and "HKEY_LOCAL_MACHINE\SOFTWARE\MYPROGRAM" with "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MYPROGRAM "

    Doing this is allowing the exported registry file to be changed so it can be imported into the registry on a x64 system (and still work).

    Because it is a registry file, it must be able to work with quotes ("), spaces in paths, and blank lines kept intact. Pretty tricky, I know.

    Another post on this forum has come close, but doesn't work with quotes and spaces (I can't post the link until I post 5 or more times..?)

    Batch Files are my only option for doing this please - I don't know any other programming language, and relying on third party programs of vbs scripts etc to do this for me are not an option. Batch File solutions only please would be most welcome.

    Oh, it would be great if the reverse would work also:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MYPROGRAM]
    "PathToProgram"="C:\\Program Files (x86)\\MyDirectory\\MyProgram"

    with this:

    [HKEY_LOCAL_MACHINE\SOFTWARE\MYPROGRAM]
    "PathToProgram"="C:\\Program Files\\MyDirectory\\MyProgram"

    Thanks so much in advance if anybody can help me.

  • #2
    Re: Finding and replacing text in registry file with quotes and paths and spaces.

    Here is what I have so far...

    for /f "skip=2 delims=] tokens=1*" %%A in ('find /v /n "" "%CD%\REG\MYREGFILE.REG"') do (
    SET LINE=%%B
    SET LINE=!LINE:\Program Files\=\Program Files (x86)\!
    SET LINE=!LINE:HKEY_LOCAL_MACHINE\SOFTWARE\MYPROGRAM\= HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MYPROGRAM\ !
    ECHO.!LINE!>>RESULT.REG
    )

    But "SET LINE=!LINE:\Program Files\=\Program Files (x86)\!" etc. doesn't work... If I could somehow change the !LINE! to %LINE%, then it would work - but how can I do this ??? Echoing %LINE% on it's own echoes nothing.

    How do I change !LINE! to %LINE% ???

    Comment


    • #3
      Re: Finding and replacing text in registry file with quotes and paths and spaces.

      Originally posted by HelpWantI View Post
      SET LINE=!LINE:\Program Files\=\Program Files (x86)\!
      SET LINE=!LINE:HKEY_LOCAL_MACHINE\SOFTWARE\MYPROGRAM\= HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MYPROGRAM\ !
      Is the whitespace before the closing ! on the second line intentional or was it added by the forum to prevent wrapping?

      % is a special character in batch files for referring to variables and command line parameters so probably isn't a good choice. Try %% to escape it.

      Comment


      • #4
        Re: Finding and replacing text in registry file with quotes and paths and spaces.

        Originally posted by HelpWantI View Post
        Another post on this forum has come close, but doesn't work with quotes and spaces (I can't post the link until I post 5 or more times..?)
        You can use the Thread or Post TAGs to post the links (sample http://forums.petri.com/showpost.php...09&postcount=5)


        Originally posted by HelpWantI View Post
        Because it is a registry file, it must be able to work with quotes ("), spaces in paths, and blank lines kept intact. Pretty tricky, I know.

        Batch Files are my only option for doing this please - I don't know any other programming language, and relying on third party programs of vbs scripts etc to do this for me are not an option. Batch File solutions only please would be most welcome.
        To do advanced String replacement a Batch files is not the best choice. Not without using a 3rd party command line utility anyway (a good command line utility for text processing is the free gnu sed for Windows).

        Here is a batch sample that is not using a 3rd party tool,
        Code:
        @echo off & SETLOCAL ENABLEEXTENSIONS
        
        Set "InFile=%~dp0\REG\MYREGFILE.REG"
        Set "OutFile=%~dp0\REG\MYREGFILE2.REG"
        
        color 6A & echo please wait.. & title parsing file, "%InFile"
        
        call:BatchSubstitude "%InFile%" > "%OutFile%"
        
        
        goto:EOF
        :BatchSubstitude *** source http://www.dostips.com/?t=Batch.FindAndReplace
           SETLOCAL DISABLEDELAYEDEXPANSION
           for /f "tokens=1,* delims=]" %%A in ('"type "%~1"|find /n /v """') do (
             set "line=%%B"
             if defined line (     
               1>nul 2>&1 call:replace
               for /f "delims=" %%* in ('"echo."%%line%%""') do (%%~*)
             ) ELSE (echo.))
           ENDLOCAL
           exit /b 0
           :replace
           call set line |Findstr.exe /RIC:"\<\[HKEY_.*\\.*\]\>" &&(
             set "onkey="
        
             call set line |Findstr.exe /RIC:"\<\[HKEY_LOCAL_MACHINE\\SOFTWARE\\MYPROGRAM\>" &&(
                 call set "line=%%line:\SOFTWARE\MYPROGRAM=\SOFTWARE\Wow6432Node\MYPROGRAM%%")
        
             call set line |Findstr.exe /RIC:"\<\[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MYPROGRAM\>" &&(
                 set "onkey=ON")
           )
        
           if defined onkey (
             call set line |Findstr.exe /RIC:"\<"PathToProgram"\>="" &&(
                 call set "line=%%line:C:\Program files (x86)\MyDirectory\MyProgram=C:\\Program Files (x86)\\MyDirectory\\MyProgram%%"
                 call set "line=%%line:C:\\Program files\\MyDirectory\\MyProgram=C:\\Program Files (x86)\\MyDirectory\\MyProgram%%")
           )
           call set "line=echo.%%line%%"
        exit /b 0


        Originally posted by HelpWantI View Post
        Oh, it would be great if the reverse would work also:

        [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MYPROGRAM]
        "PathToProgram"="C:\\Program Files (x86)\\MyDirectory\\MyProgram"

        with this:

        [HKEY_LOCAL_MACHINE\SOFTWARE\MYPROGRAM]
        "PathToProgram"="C:\\Program Files\\MyDirectory\\MyProgram"
        Make the necessary adjustments to the blue colored text.


        /Rems
        Last edited by Rems; 3rd May 2011, 09:38.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Finding and replacing text in registry file with quotes and paths and spaces.

          Originally posted by beddo View Post
          Is the whitespace before the closing ! on the second line intentional or was it added by the forum to prevent wrapping?

          % is a special character in batch files for referring to variables and command line parameters so probably isn't a good choice. Try %% to escape it.
          Thanks for your reply, Beddo. The whitespace was put in by the forum.

          Comment


          • #6
            Re: Finding and replacing text in registry file with quotes and paths and spaces.

            Thaks so much for your reply Rems, I will try your code and see how it works out.

            I managed to make my own solution for this, but my code can't deal with single % symbols in a line (only doubles) and some other symbol at the moment, but I am working around these.

            I'll try your code when I can tomorrow and see what it does. Thanks again!

            Comment

            Working...
            X