Script to add the contents clipboard on a particular line of ini file

Page 2 of 2 FirstFirst 12

  1. Posts : 948
    windows 10 professional 64-bit, 22H2
    Thread Starter
       #11

    ddelo said:
    Copy and paste the following three lines of code into a text file.
    In the 1st line replace within the quotes the path of your .ini file.
    Save the text file with an extension ps1, for example Test.ps1

    Code:
    $INIFile = "C:\Users\<UserName>\Desktop\OOreg.ini"
    $LastAddressFromClipboard = Get-Clipboard -Format Text -TextFormatType UnicodeText
    (Get-Content $INIFile | ForEach-Object { $_ -replace "LastAddress=.+", "LastAddress=$LastAddressFromClipboard" }) | Set-Content $INIFile

    Copy to the clipboard the address you want to save in your .ini file
    Open File Explorer and go to the folder where you saved the Test.ps1 file.
    In File Explorer select File > Open Windows PowerShell (either as user or Administrator, I prefer Administrator)
    In the PowerShell console window type .\Test.ps1

    That's it!
    Thank you for this. I will try it out. In the meantime, I was able to learn how to do this with Autohotkey. My script is:

    IniWrite, %Clipboard%, C:\Utilities\RegEditor\OORegEdt.INI, Settings, LastAddress
    Run, C:\Utilities\RegEditor\OORegEdt.exe


    - - - Updated - - -

    KeithM said:
    PowerShell:
    Code:
    $IniFile = 'c:\Path\To\file.ini'
    $NewText = gcb
    (gc $IniFile -Raw) -replace '(?<=LastAddress=).+(?=\n)', $NewText
    Thank you, Keith. I appreciate your advice & will experiment with it.
      My Computers


  2. Posts : 2,450
    Windows 10 Pro x64
       #12

    phrab said:
    Thank you for this. I will try it out. In the meantime, I was able to learn how to do this with Autohotkey.
    Most welcome Phil. I hope you get it working the way you want.
      My Computer


  3. Posts : 989
    Microsoft Windows 10 Home
       #13

    phrab said:
    Thank you, Keith. I appreciate your advice & will experiment with it.
    I forgot Set-Content (sc)​ after replace. I've edited my reply.
      My Computer


  4. Posts : 7,607
    Windows 10 Home 20H2
       #14

    KeithM said:
    PowerShell:
    Code:
    $IniFile = 'c:\Path\To\file.ini'
    $NewText = gcb
    (gc $IniFile -Raw) -replace '(?<=LastAddress=).+(?=\n)', $NewText | sc $iniFile
    It works with an .ini file. How can I make it work with other files, e.g. a .txt file?
      My Computer


  5. Posts : 2,450
    Windows 10 Pro x64
       #15

    Matthew Wai said:
    It works with an .ini file. How can I make it work with other files, e.g. a .txt file?
    Change the filename in the first line to $IniFile = 'c:\Path\To\file.txt'
      My Computer


  6. Posts : 7,607
    Windows 10 Home 20H2
       #16

    It works now. Thank you!
      My Computer


  7. Posts : 2,450
    Windows 10 Pro x64
       #17

    Try this then:

    Code:
    $MyFile = "C:\Users\<UserName>\Desktop\somefile.txt"
    $LastAddressFromClipboard = Get-Clipboard -Format Text -TextFormatType UnicodeText
    (Get-Content $MyFile | ForEach-Object { $_ -replace "LastAddress=.+", "LastAddress=$LastAddressFromClipboard" }) | Set-Content $MyFile
      My Computer


  8. Posts : 7,607
    Windows 10 Home 20H2
       #18

    @ddelo,
    Code:
    $MyFile = "E:\Testing\somefile.txt"
    $LastAddressFromClipboard = Get-Clipboard -Format Text -TextFormatType UnicodeText
    (Get-Content $MyFile | ForEach-Object { $_ -replace "ABCD=.+", "ABCD=$LastAddressFromClipboard" }) | Set-Content $MyFile

    I put the above code into a .ps1 file, copied some text into the clipboard and then right-clicked on the file-->"Run with PowerShell". The following "E:\Testing\somefile.txt" remained unchanged.

    Code:
    Microsoft Windows [Version 10.0.18362.30]
    (c) 2019 Microsoft Corporation. All rights reserved.
    
    C:\Windows\system32>More "E:\Testing\somefile.txt"
    ABCD=
    
    C:\Windows\system32>
    Do you know what is wrong?
      My Computer


  9. Posts : 989
    Microsoft Windows 10 Home
       #19

    Matthew Wai said:
    @ddelo,
    Code:
    $MyFile = "E:\Testing\somefile.txt"
    $LastAddressFromClipboard = Get-Clipboard -Format Text -TextFormatType UnicodeText
    (Get-Content $MyFile | ForEach-Object { $_ -replace "ABCD=.+", "ABCD=$LastAddressFromClipboard" }) | Set-Content $MyFile

    I put the above code into a .ps1 file, copied some text into the clipboard and then right-clicked on the file-->"Run with PowerShell". The following "E:\Testing\somefile.txt" remained unchanged.

    Code:
    Microsoft Windows [Version 10.0.18362.30]
    (c) 2019 Microsoft Corporation. All rights reserved.
    
    C:\Windows\system32>More "E:\Testing\somefile.txt"
    ABCD=
    
    C:\Windows\system32>
    Do you know what is wrong?
    Did you have at least one character aftter the = character in your target file? That's what the .+ matches in a regex. To allow for no characters after the =, use: -replace "ABCD=.*"
      My Computer


  10. Posts : 7,607
    Windows 10 Home 20H2
       #20

    Thank you! You are right.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:03.
Find Us




Windows 10 Forums