Windows 批处理文件 - 需要从参数中删除换行符
我有一个 powershell 脚本,它将运行查询并将结果导出到 Excel。我想将其连接到 SQL Studio Management Studio (2008) 的外部工具中。 SSMS 不允许 powershell 脚本,因此我使用批处理文件来启动它。
在外部工具部分,您可以指定一些预定义的参数。我想要的参数称为“当前文本”。该参数将突出显示的任何内容传递给工具(批处理文件)。然后,我的批处理文件将该参数传递给 powershell 脚本。
问题是,如果用户的查询跨越多行突出显示,则 powershell 脚本会因换行符而失败。似乎将它们删除相对容易,或者更好的是,用空格替换它们?
这是我的批处理文件:
echo %~1
call powershell ^& 'c:\temp\ExportSQL.ps1' -query "%~1"
我的问题是,在将 %1 传递给 powershell 脚本之前,如何用空格替换换行符和回车符? TIA。
I have a powershell script that will run a query and export the results to Excel. I want to hook this into SQL Studio Management Studio (2008)'s external tools. SSMS will not allow powershell scripts so I am using a batch file to get it started.
In the external tools section you can specify some predefined arguments. The argument that I want is called "current text". This argument passes whatever is highlighted to the tool (batch file). My batch file then passes that argument on to a powershell script.
The problem is that if the user has a query that spans multiple lines highlighted then the powershell script fails because of the linebreaks. It seems it would be relatively easy to strip them out, or better yet, replace them with a space?
Here is my batch file:
echo %~1
call powershell ^& 'c:\temp\ExportSQL.ps1' -query "%~1"
My question is how can I replace newlines and carriage returns from %1 with a space before passing it to the powershell script? TIA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我不敢相信您的参数
%1
中真的有换行符,因为这是可能的,但实现这一点有点复杂。要控制这一点,您可以使用
要处理 CR 和换行符,您必须使用延迟扩展,因为此类内容的百分比扩展是不可能的。
由于百分比扩展 CR 总是被删除,换行符会删除行的其余部分,或者在块上下文中附加一个新的命令行。
因此,无法在
%1
..%9
这样的参数中处理带有换行符的内容。您需要将其存储在变量中。
假设变量中包含换行符内容,您可以将其替换为空格。
此示例中的空行对于结果很重要。
- - 输出 - -
First, I can't believe that you have really linefeeds in your parameter
%1
, as it is possible, but a bit complex to achieve this.To control this you could use
To handle CR and linefeed characters you have to use the delayed expansion, as percent expansion of such a content is not possible.
With percent expansion CR's are always removed, linefeeds removes the rest of the line or in a block context they append a new command line.
So it's not possible to handle content with linefeeds in a parameter like
%1
..%9
.You need it stored in a variable.
Assuming you have content with newlines in a variable, you could replace it with spaces.
The empty lines in this example are important for the result.
--- Output ---