我想知道如何以编程方式\在文件路径中出现的空格前添加.我fs.readdir用来获取目录的内容,如果我没有从路径中删除空格,那么我得到一个ENOENT错误,因为未转义的路径在UNIX中无效.我想得到以下结果:
/path/with\ spaces/in/them/
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了NODE REPL中REGEX的问题.当我做 :
var path = '/path/with spaces/in/them/';
var escapedPath = path.replace(/(\s)/, "\\ ");
Run Code Online (Sandbox Code Playgroud)
我得到的结果是:
'/path/with\\ spaces/in/them/'
Run Code Online (Sandbox Code Playgroud)
当我尝试在Chrome控制台内部运行相同的代码时,我得到:
"/path/with\ spaces/in/them/"
Run Code Online (Sandbox Code Playgroud)
这是期望的效果.
我不确定我是否遗漏了某些东西,或者这是否是一个错误.我很困惑,因为Node运行在与Chrome相同的Javascript引擎上,所以我认为这些表达式将被解释为相同.
如果有人知道如何解决这个问题,请告诉我.也许我错过了什么.我只需要能够在传递它们之前逃脱路径,fs.readdir以便我可以避免这些错误.
谢谢!
我一直在运行密码过期脚本 6 个月没有任何问题。该脚本将读取静态 html 文件并更改内存中的一些内容,然后将向所有密码过期的用户发送 html 电子邮件。
脚本似乎在过去一周左右的时间里坏了。经过进一步调查,我将错误范围缩小到 Powershell 应该创建新 ComObject 并将该 HTML 文件写入 ComObject 的部分。
我现在收到错误:
No coercion operator is defined between types 'System.Array' and 'System.String'
At line:1 char:1
+ $html.write($source);
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException
Run Code Online (Sandbox Code Playgroud)
当我在代码行下方运行时会发生上述错误:
$html = New-Object -ComObject "HTMLFile"
$src = Get-Content -path "./passwordreminder.html" -Raw
$html.write($src)
Run Code Online (Sandbox Code Playgroud)
当我调用该write()方法时,出现错误。
由于它在过去 6 个月中一直运行良好,我能想到的唯一改变是我的 powershell 版本。我相信当我开始运行这个脚本时,我使用的是 Powershell v4.0,但在 Windows 更新之后,我猜 Powershell 现在是 v5.0。见下文 :
Name Value
---- -----
PSVersion 5.0.10105.0 …Run Code Online (Sandbox Code Playgroud)