为什么 PowerShell 脚本找不到字符?
我有一个每天运行的 PowerShell 脚本,用于过滤文件夹中的所有文件(因为有 2000 多个文件),并查找和替换字符并用换行符替换。字符在记事本中显示为向上箭头字符,在记事本++中显示为 FF 字符
我在下面也有图像
$filename = Get-ChildItem "C:\Scripts\*filename*.*"
$filename | % {
(gc $_) -replace "","`n`f" | Set-Content $_.fullname
}
如所见,在代码块中它不显示箭头,但作为文本显示。我可以手动查找和替换,但是当它从任务计划运行 PowerShell 脚本时,它似乎不会选择任何内容来替换它。有没有不同的方法来解决这个问题?
任何帮助表示赞赏!
I have a PowerShell script that runs daily and is used to filter through all the files in a folder as there are 2000+ and do a find and replace of a character and replace with a linebreak. Character shows as, up arrow character in notepad, an FF character in notepad++
I have images below as well
$filename = Get-ChildItem "C:\Scripts\*filename*.*"
$filename | % {
(gc $_) -replace "","`n`f" | Set-Content $_.fullname
}
As seen, in the code block it doesn't show the arrow, but as text it does. I can do a manual find and replace but when it runs the PowerShell script from the task schedule it doesn't pick anything up to replace it seems. Is there a different way of going about this?
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我对正在发生的事情的猜测。将换页符 0C“`f”替换为回车符,换行符 0D 0A“`r`n”(Windows 文本)。
Here's my guess as to what's going on. Replacing formfeed 0C "`f" with carriage return, linefeed 0D 0A "`r`n" (windows text).
从我在测试中看到的情况来看,默认情况下 Get-Content 并不默认获取 Unicode 格式的内容,因此我猜测它默认为 ASCII(但不确定)。
因此,在您的脚本中,您需要指定编码以强制它使用该编码。我还建议通过 UNICODE 数字而不是符号来引用特定字符,这样您就不必担心脚本文件的格式或您正在使用的编辑器。以下应该可以满足您的需要(或者至少在我的机器上)。
From what I can see in tests, by default Get-Content doesn't default to getting content in Unicode format, so I'd guess it's defaulting to ASCII (but don't know for sure).
So in your script you'll want to specify the encoding to force it to use that. I'd also suggest referencing the specific character via it's UNICODE number rather than the symbol, that way you don't need to worry about the format of the script file, or the editor you're using. The following should do what you need (or at least does on my machine).
好吧,我想我已经知道你遇到问题的角色是什么了。看起来是0x0C。我使用 HxD 编辑一个文本文件,并将从 0x00 到 0x1F 的所有字符放入其中,发现 0x0C 是粗向上箭头。
我已将这段代码重写为应该有效的代码。但如果不行,请尝试 Keith 的答案,并将“\u2191”替换为“\u000C”,或者如果这不起作用,也许可以在他的代码中尝试“\u0C”。不确定 RegEx 如何与 unicode 配合使用,但应该是这样的。
编辑:
在下面的评论中 mklement0 指出这应该是换页字符。既然如此,那么这个版本应该可以工作。
但如果这些都不起作用,那么不要忘记尝试 Keith 的版本 - 并用他的代码尝试“`f”和 Unicode“\u000C”变体。
Okay, I think I figured out what character you are having problems with. It appears to be 0x0C. I used HxD to edit a text file and placed all the characters from 0x00 to 0x1F in there and found 0x0C was the fat up arrow.
I've rewrote this code to what should work. But if it doesn't then try Keith's answer and replace the "\u2191" with "\u000C", or if that doesn't work, maybe try "\u0C" in his code. Not sure how RegEx works with unicode, but it should be something like this.
EDIT:
In the comments below mklement0 pointed out that this should be the Form Feed character. That being the case, then this version should work.
But if none these work, then don't forget to try Keith's version - and try both the "`f" and the Unicode "\u000C" variations with his code.