• Login
Community
  • Login

Using Replace function in PShell to modify Workspace file

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 3 Posters 292 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C
    Cariboudjan
    last edited by Jun 28, 2023, 3:29 PM

    I’m trying to use the below PS1 script to replace the username in each line of the Workspace file to a different username. The script works, and the text is replaced correctly, but afterwards the Workspace file no longer works in Notepad++. Is there a specific filetype or encoding I need to set in order for the formatted file to be readable/usable in Notepad++ after the script is run?

    Thanks.

    $path = "c:\Users\$($env:username)\Documents\Workspace File" 
    (Get-Content $path) -replace "username1","username2" | out-file $path 
    
    E M 2 Replies Last reply Jun 28, 2023, 4:01 PM Reply Quote 0
    • E
      Ekopalypse @Cariboudjan
      last edited by Jun 28, 2023, 4:01 PM

      @Cariboudjan

      What do you mean by “workspace file” and “no longer works”?
      You see a different content when you open the file with npp? If so, what is different?

      C 1 Reply Last reply Jun 28, 2023, 4:11 PM Reply Quote 1
      • C
        Cariboudjan @Ekopalypse
        last edited by Cariboudjan Jun 28, 2023, 4:12 PM Jun 28, 2023, 4:11 PM

        @Ekopalypse

        I receive this error when trying to load it in the Project Pane in Notepad++

        bf994030-7347-4604-8795-d98f368f7072-image.png

        06b3d631-684c-4f6b-ba30-a6c880828502-image.png

        1 Reply Last reply Reply Quote 0
        • M
          mkupper @Cariboudjan
          last edited by Jun 28, 2023, 4:15 PM

          @Cariboudjan PowerShell’s out-file command outputs utf8NoBOM encoded files by default. From within Notepad++ first look at the right side of the status line at the bottom. If it says UTF-8 then the file should be displayed correctly. If you see something other than UTF-8 then, using the Encoding drop down menu try picking UTF-8 and see if that works for you. I would not use the Convert to … options nor would I save the file from Notepad++ without first making a backup of the file.

          C 1 Reply Last reply Jun 28, 2023, 4:26 PM Reply Quote 2
          • C
            Cariboudjan @mkupper
            last edited by Cariboudjan Jun 28, 2023, 4:30 PM Jun 28, 2023, 4:26 PM

            @mkupper

            Alright. Changing the encoding to UTF8 fixed the issue. Below is the full solution:

            $path = "c:\Users\$($env:username)\Documents\Workspace File" 
            (Get-Content $path) -replace "username1","username2" | out-file $path 
            Set-Content -NoNewline -Encoding OEM $path -Value (Get-Content -Raw -Encoding utf8 $path)
            

            Or alternatively, with arguments:

            $replace1 = args[0]
            $replace2 = args[1]
            $path = $args[2]
            $encoding = $args[3]
            
            (Get-Content $path) -replace $replace1,$replace2 | out-file $path
            Set-Content -NoNewline -Encoding OEM $path -Value (Get-Content -Raw -Encoding $encoding $path)
            
            M 1 Reply Last reply Jun 29, 2023, 3:06 PM Reply Quote 3
            • M
              mkupper @Cariboudjan
              last edited by Jun 29, 2023, 3:06 PM

              @Cariboudjan said in Using Replace function in PShell to modify Workspace file:

              Alright. Changing the encoding to UTF8 fixed the issue.

              I am glad it seems to be working for you now.

              A couple of points:

              1. The encoding shown on Notepad++'s status line is an estimate based on inspecting the file. The inspection process is not perfect and so I would not load a file into Notepad++ and 100% rely on what it says the encoding of that file seems to be. I have had Notepad++ say “ANSI” for example even when the file has 3 or 4 byte long UTF-8 encoded sequences near the beginning. Figuring out the encoding of a file can be a long topic in itself.
              2. In your code I’m bothered by your use of -NoNewline -Encoding OEM. It just seems weird and is the sort of thing that will lead to unexpected results down the road. However, this forum is about Notepad++ and so I’ll leave it at that rather than topic-drifting into encoding and PowerShell.
              1 Reply Last reply Reply Quote 2
              5 out of 6
              • First post
                5/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors