0
C:\>Powershell -NoProfile "(Get-Content -Raw .\allacts.txt) -replace '\u0020\u0020\u0020\u0020\u007D\u002C\u000d\u000A\u0020\u0020\u0020\u0020\u0020\u0020\u0022\u0073\u0074\u0075\u0064\u0065\u006E\u0074\u0054\u006F\u0074\u0061\u006C\u0022\u003A\u0020', \"      `\"studentTotal`\": \" | Out-File -FilePath allacts.txt -Force -Encoding ASCII -nonewline"

This works correctly and doesn't return any errors. As you can see, I need to replace a string with a double quote.

C:\>Powershell -NoProfile "(Get-Content -Raw .\allacts.txt) -replace '\u0022\u000d\u000A\u0020\u0020\u0020\u0020\u0020\u0020', \"    `\"   \" | Out-File -FilePath allacts.txt -Force -Encoding ASCII -nonewline"
'Out-File' is not recognized as an internal or external command,
operable program or batch file.

The above does not work. I don't understand why - it's escaped in exactly the same way as the first command.

I have to use these commands from within a batch file as I can't run Powershell scripts on this system. I've searched through a lot of questions on SO about escaping quotes. ^ does not work inside double quotes. If the presence of | is the problem, as suggested in this answer., then it shouldn't work for either of these examples.

You can see that I used unicode for the "find" portion of the command. I would use it in the "replace" portion but it is interpreted literally, whether I use single or double quotes. That is it will not insert the " character but the six characters /u0022 - if you know how to make that work the way I need it to, please tell.

Edit:

Powershell -NoProfile "(Get-Content -Raw .\allacts.txt) -replace '\u0022\u000d\u000A\u0020\u0020\u0020\u0020\u0020\u0020', \"    `\"   `\"   \" | Out-File -FilePath allacts.txt -Force -Encoding ASCII -nonewline"

works but this means I get two double quotes when I only want one.

Ne Mo
  • 198
  • 4
  • 18

1 Answers1

1

The second line has an unbalanced single quote and unbalanced double quote in the replace-with part. Correct this and the script will work the same as the first line.

OJBakker
  • 602
  • 1
  • 5
  • 6
  • Thanks for your answer. By unbalanced I assumed you mean there is no corresponding closing quotes. There's no single quote in the second -replace with, but there is a backtick. I put a second double quote in and the command doesn't return an error, but I'm still v. confused. This means I'm replacing a string with *two* double quotes. I can work around this in my particular file but changing the find portion so I get the end result I want, but can't I just replace one double quote somehow? :/ – Ne Mo Sep 16 '21 at 12:18
  • Sorry for the confusion regarding the backtick / single quote. Use '""' as replacement string to get 1 double quote as replacement. I am no expert on powershell so I can not fully explain why you need 2 double quotes to get 1 as result but it is probably necessary because the whole command is already enclosed with double quotes. – OJBakker Sep 16 '21 at 15:04