CMD:剪辑命令问题
CMD : Clip command Issue
在呼叫中心工作,我有很多重复的日志要为每个呼叫记录下来,我正在尝试构建一个简单的 HTA,能够通过单击将日志模板(和电子邮件)复制到剪贴板。
很遗憾,无法复制 .vbs 格式的文本文件 (?)。
因此,HTA 在单击文本名称参数 (%1) 时打开 .bat。从它在服务器会话 (%~dp0) 上的位置,.batch 打开文件夹 "Assets".
中的文本文件 (%1)
cls
@pushd %~dp0
clip < %~dp0Assets\%1
@popd
我知道,我必须利用我所拥有的。但一切顺利!
直到我粘贴剪贴板的内容:
- "d'avoir" 变为 "dÆavoir"
- “é”-->“Ú”
- “à”-->“Ó”
- 等...
我尝试了 Unicode、Ansi 和 UTF-8,同样的问题。
有人有想法吗?
这里是使用 IE 剪贴板的 vbscript。因为您在 HTA 中,所以您已经可以访问 IE window 对象。此示例粘贴但 SetData
复制到剪贴板。忽略安全性,因为 HTA 会绕过 IE 安全性。
Sub Clip
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
'Have to navigate to a local file to put IE into Intranet Zone, else this will generate security dialog asking permission
ie.Navigate2 FilterPath & "Filter.html"
Do
wscript.sleep 100
Loop until ie.document.readystate = "complete"
txt=ie.document.parentwindow.clipboardData.GetData("TEXT")
ie.quit
If IsNull(txt) = true then
outp.writeline "No text on clipboard"
else
outp.writeline txt
End If
End Sub
主题太宽泛了。它的重点是展示如何使用您的主机环境。关键是组件对象模型 (COM) 和可用的库。您可以使用 Word,Excel 访问剪贴板而不是 IE。
将其粘贴到 HTA 中。
<html>
<body>
<script language=vbscript>
document.write document.parentwindow.clipboardData.GetData("TEXT")
</script>
</body>
</html>
问题是您正在执行 clip
,而控制台的活动代码页与您复制到剪贴板的文本的编码不匹配。您还应确保包含扩展字符的文本文件以 UTF8 格式保存。换句话说,问题出在复制上,而不是粘贴上;一切并不像你断言的那样顺利。
试试这个:
@echo off & setlocal
rem // capture current console codepage to a variable
for /f "tokens=2 delims=:" %%I in ('chcp') do set /a cp=%%I
rem // change console codepage to Unicode
chcp 65001 >NUL
rem // copy asset to clipboard
clip < "%~dp0\Assets\%~1" && 2>&1 echo Successfully copied.
rem // revert console to original codepage
chcp %cp% >NUL
我找到了另一个神奇的组合,涉及 PowerShell 和 .NET 方法,用于处理编码和设置剪贴板内容。试试这个 .bat 脚本:
<# : batch portion
@echo off & setlocal
set "infile=%~dp0Assets\%~1"
if not exist "%infile%" (
2>&1 echo Usage: %~dp0 assetfile
2>&1 echo ... where assetfile is a file in %~dp0Assets\
goto :EOF
)
powershell -STA "iex (${%~f0} | out-string)"
goto :EOF
# end batch / begin PowerShell hybrid code #>
add-type -As System.Windows.Forms
$txt = gc $env:infile -encoding UTF8 | out-string
[Windows.Forms.Clipboard]::SetText($txt)
另一种可能的解决方案是将您的文本片段存储在脚本本身中。这是一个在批处理 + PowerShell 混合脚本中演示 heredocs 的示例(使用 .bat 扩展名保存):
<# : batch portion
@echo off & setlocal
set "str=%~1"
powershell -STA "iex (${%~f0} | out-string)"
goto :EOF
# end batch / begin PowerShell #>
$strings = @{
foo = @"
Until i paste the content of the clipboard :
"d'avoir" becomes "dÆavoir"
"@
bar = @"
"é" --> "Ú"
"à" --> "Ó"
etc ...
"@
} # end $strings collection of heredocs
if (-not $strings[$env:str]) { "$env:str not defined."; exit 1 }
add-type -As System.Windows.Forms
[Windows.Forms.Clipboard]::Clear()
[Windows.Forms.Clipboard]::SetText($strings[$env:str])
您会注意到 heredocs 包含在 @"
和 "@
中。用法:script.bat foo
或 script.bat bar
在呼叫中心工作,我有很多重复的日志要为每个呼叫记录下来,我正在尝试构建一个简单的 HTA,能够通过单击将日志模板(和电子邮件)复制到剪贴板。
很遗憾,无法复制 .vbs 格式的文本文件 (?)。
因此,HTA 在单击文本名称参数 (%1) 时打开 .bat。从它在服务器会话 (%~dp0) 上的位置,.batch 打开文件夹 "Assets".
中的文本文件 (%1)cls
@pushd %~dp0
clip < %~dp0Assets\%1
@popd
我知道,我必须利用我所拥有的。但一切顺利!
直到我粘贴剪贴板的内容:
- "d'avoir" 变为 "dÆavoir"
- “é”-->“Ú”
- “à”-->“Ó”
- 等...
我尝试了 Unicode、Ansi 和 UTF-8,同样的问题。
有人有想法吗?
这里是使用 IE 剪贴板的 vbscript。因为您在 HTA 中,所以您已经可以访问 IE window 对象。此示例粘贴但 SetData
复制到剪贴板。忽略安全性,因为 HTA 会绕过 IE 安全性。
Sub Clip
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
'Have to navigate to a local file to put IE into Intranet Zone, else this will generate security dialog asking permission
ie.Navigate2 FilterPath & "Filter.html"
Do
wscript.sleep 100
Loop until ie.document.readystate = "complete"
txt=ie.document.parentwindow.clipboardData.GetData("TEXT")
ie.quit
If IsNull(txt) = true then
outp.writeline "No text on clipboard"
else
outp.writeline txt
End If
End Sub
主题太宽泛了。它的重点是展示如何使用您的主机环境。关键是组件对象模型 (COM) 和可用的库。您可以使用 Word,Excel 访问剪贴板而不是 IE。
将其粘贴到 HTA 中。
<html>
<body>
<script language=vbscript>
document.write document.parentwindow.clipboardData.GetData("TEXT")
</script>
</body>
</html>
问题是您正在执行 clip
,而控制台的活动代码页与您复制到剪贴板的文本的编码不匹配。您还应确保包含扩展字符的文本文件以 UTF8 格式保存。换句话说,问题出在复制上,而不是粘贴上;一切并不像你断言的那样顺利。
试试这个:
@echo off & setlocal
rem // capture current console codepage to a variable
for /f "tokens=2 delims=:" %%I in ('chcp') do set /a cp=%%I
rem // change console codepage to Unicode
chcp 65001 >NUL
rem // copy asset to clipboard
clip < "%~dp0\Assets\%~1" && 2>&1 echo Successfully copied.
rem // revert console to original codepage
chcp %cp% >NUL
我找到了另一个神奇的组合,涉及 PowerShell 和 .NET 方法,用于处理编码和设置剪贴板内容。试试这个 .bat 脚本:
<# : batch portion
@echo off & setlocal
set "infile=%~dp0Assets\%~1"
if not exist "%infile%" (
2>&1 echo Usage: %~dp0 assetfile
2>&1 echo ... where assetfile is a file in %~dp0Assets\
goto :EOF
)
powershell -STA "iex (${%~f0} | out-string)"
goto :EOF
# end batch / begin PowerShell hybrid code #>
add-type -As System.Windows.Forms
$txt = gc $env:infile -encoding UTF8 | out-string
[Windows.Forms.Clipboard]::SetText($txt)
另一种可能的解决方案是将您的文本片段存储在脚本本身中。这是一个在批处理 + PowerShell 混合脚本中演示 heredocs 的示例(使用 .bat 扩展名保存):
<# : batch portion
@echo off & setlocal
set "str=%~1"
powershell -STA "iex (${%~f0} | out-string)"
goto :EOF
# end batch / begin PowerShell #>
$strings = @{
foo = @"
Until i paste the content of the clipboard :
"d'avoir" becomes "dÆavoir"
"@
bar = @"
"é" --> "Ú"
"à" --> "Ó"
etc ...
"@
} # end $strings collection of heredocs
if (-not $strings[$env:str]) { "$env:str not defined."; exit 1 }
add-type -As System.Windows.Forms
[Windows.Forms.Clipboard]::Clear()
[Windows.Forms.Clipboard]::SetText($strings[$env:str])
您会注意到 heredocs 包含在 @"
和 "@
中。用法:script.bat foo
或 script.bat bar