powershell - .Replace 适用于 powershell 但不适用于批处理脚本
问题描述
我的批处理脚本如下所示:
@echo off
echo.
echo Replacing instances of type="module" in index.html with type="text/javascript"
cd www
(Get-Content index.html).Replace("module", "javascript") | Set-Content index.html
当我尝试在 powershell 中使用 .Replace 时,它工作正常,但是从批处理脚本运行它失败并出现错误.Replace("module" was unexpected at this time.
(Get-Content index.html) -replace 'module', 'javascript' | Set-Content index.html
也想出了同样的错误。提前感谢您的帮助。
解决方案
如果您真的想从 .bat 脚本调用 powershell,这里有一种方法。输出文件必须与输入文件不同。"> index2.html" 部分由 cmd 运行,因此它是用 ascii 编码的,而不是 unicode。
type index.html | powershell $input -replace 'module','javascript' > index2.html
或者将其完全保留在 powershell 中。文件编码将是“ansi”。
powershell "(get-content index.html) -replace 'module','javascript' | set-content index.html"
推荐阅读
- multithreading - Spring Batch 事务管理 - 多线程步骤
- input - 我可以使用来自不和谐消息的输入来更改命令吗?
- http - 导入 'dart:html' 失败
- java - 运行 dockerizing 映像时未找到主类
- python - 使用 matplotlib 绘制数据并使用相同颜色对组直方图进行着色
- android - 运行颤振命令时找不到 PowerShell 可执行文件
- vue.js - 调用其他函数时如何禁用函数
- java - Java 通过点击 android studio 获取 textView 或 Button 的 id
- javascript - Firebase Child_Added 在 Javascript 中不起作用
- reactjs - 属性未定义 - 在 React 中从类组件切换到函数挂钩组件