在呼叫中心工作时,我要为每个呼叫写下相当重复的日志,我正在尝试构建一个简单的HTA,只需单击一下即可将日志模板(和电子邮件)复制到剪贴板。
不幸的是,无法在.vbs(?)中复制文本文件。
因此,HTA在单击时使用文本名称参数(%1)打开.bat。.batch从服务器会话(%〜dp0)上的位置打开“资产”文件夹中的文本文件(%1)。
cls
@pushd %~dp0
clip < %~dp0Assets\%1
@popd
我知道,我必须与自己所拥有的一起工作。但是一切都顺利进行!
直到我粘贴剪贴板的内容:
我尝试了Unicode,Ansi和UTF-8,同样的问题。
有人有主意吗?
问题是您正在执行,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混合脚本(保存为.bat扩展名)中演示heredocs:
<# : 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句