每次尝试批量创建vbs文件时,vbs文件中的某些行都会丢失
@echo off
echo Function RunAsAdmin() >> 2.vbs
echo If WScript.Arguments.length = 0 Then >> 2.vbs
echo CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & _ >> 2.vbs
echo WScript.ScriptFullName & """" & "RunAsAdministrator""",,"runas", 1 >> 2.vbs
echo WScript.Quit >> 2.vbs
echo End If >> test.vbs批量制作vbs文件时,这两行需要保留什么?
echo CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & _ >> 2.vbs
echo WScript.ScriptFullName & """" & "RunAsAdministrator""",,"runas", 1 >> 2.vbs`发布于 2020-06-03 19:07:49
这里有一个替代方法,可以避免转义的必要性:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_VBS_FILE=%~dp02.vbs"
rem // Write to given output file:
> "%_VBS_FILE%" (
rem // Read all lines from this batch script that begin with `::::`:
for /F "delims=" %%V in ('findstr "^::::" "%~f0"') do (
rem // Store current line string:
set "VBS_LINE=%%V"
rem // Toggle delayed expansion to avoid loss of `!`:
setlocal EnableDelayedExpansion
rem // Return current line string with preceding `::::` removed:
echo(!VBS_LINE:*::::=!
endlocal
)
)
endlocal
exit /B
::This is the embedded VBS code section with each line preceded by `::::`:
::::Function RunAsAdmin()
::::If WScript.Arguments.length = 0 Then
:::: CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & _
:::: WScript.ScriptFullName & """" & "RunAsAdministrator""", , "runas", 1
:::: WScript.Quit
::::End If发布于 2020-06-03 16:24:08
参考How to Escape Characters in batch file。
@echo off
Title Generate VBS File from batch script
Set "VBSFile=%~dpn0.vbs"
Call :Generate_VBS_File
Pause & Exit
REM -----------------------------------------------------------------------------
:Generate_VBS_File
>"%VBSFile%" (
echo Function RunAsAdmin(^)
echo If WScript.Arguments.length = 0 Then
echo CreateObject("Shell.Application"^).ShellExecute "wscript.exe", """" ^& _
echo WScript.ScriptFullName ^& """" ^& "RunAsAdministrator""",,"runas", 1
echo WScript.Quit
echo End If
echo End Function
)
Exit /B
REM -----------------------------------------------------------------------------发布于 2020-06-04 20:21:54
在回显到文件时,您需要转义一些特殊字符。下面的代码将为您完成此操作。
echo CreateObject^("Shell.Application"^).ShellExecute "wscript.exe", """" ^& ^_ >>2.vbs
echo WScript.ScriptFullName ^& """" ^& "RunAsAdministrator""",,"runas",1 >>2.vbs例如,使用^打印特殊字符,如_,&。(备注: ^&->将打印&)
查看下面的链接以了解有关批处理转义字符的更多信息。
https://stackoverflow.com/questions/62165309
复制相似问题