【问题标题】:Conditional Compilation Error in Batch Working For One Script But Not Another批处理一个脚本但不是另一个脚本的条件编译错误
【发布时间】:2018-06-20 07:40:09
【问题描述】:

语言:批处理 + 一点 JScript。

主要思想: JScript 适用于一个程序,但不适用于另一个程序。

错误信息: Microsoft JScript 编译错误:条件编译已关闭。

我的问题是我试图从我的 pastebin 中获取一些信息到批处理变量中。我希望我的代码能够检查并执行网站上的代码。我已经让代码独立工作,但是当我将它添加到主程序时,我得到一个Microsoft JScript compilation error: Conditional compilation is turned off 错误。如果我自己运行相同的脚本,它可以完美运行,但是当我将它添加到主脚本时它会中断。

我很确定我添加的彩色文本脚本会破坏它,但我不能 100% 确定。 所以基本上,为什么网站数据代码自己工作,但不能与其他任何东西一起工作。

我该如何解决这个问题?

[不工作部分]菜单+彩色文字+网站信息

@echo off
color 0a
setlocal
call :initColorPrint
call :title

rem ---- Menu Stuff ----

:menu
call :title
echo.
echo (Type 'version' to see error)
echo.
call :colorPrint 09 "%time%"
call :colorPrint 06 "@"
call :colorPrint 0c "%username%"
set /p call=^> 
if "%call%" =="version" goto :version
%call%
goto menu

:title
title CMD ^| User: %username%
exit /b

rem ---- This part doesn't work... ----

:version
@if (@a==@b) @end /*
@echo off
setlocal
call :stuff
timeout 1 
echo.
echo %code%
set "code=%code%"
timeout 3
exit

:stuff
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "https://pastebin.com/raw/wAtBQEeZ"') do (
set "code=%%I"
echo(%%I
)
exit /b

JScript */
var x=new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET",WSH.Arguments(0),true);
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState!=4) {WSH.Sleep(50)};
WSH.Echo(x.responseText);

rem ----- Color Stuff -----

:colorPrint Color  Str  [/n]
setlocal
set "str=%~2"
call :colorPrintVar %1 str %3
exit /b

:colorPrintVar  Color  StrVar  [/n]
if not defined %~2 exit /b 
setlocal enableDelayedExpansion
set "str=a%DEL%!%~2:\=a%DEL%\..\%DEL%%DEL%%DEL%!"
set "str=!str:/=a%DEL%/..\%DEL%%DEL%%DEL%!"
set "str=!str:"=\"!"
pushd "%temp%"
findstr /p /A:%1 "." "!str!\..\x" nul
if /i "%~3"=="/n" echo(
exit /b

:initColorPrint
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
<nul >"%temp%\x" set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%.%DEL%"
exit /b

:cleanupColorPrint
del "%temp%\x"
exit /b

图片:

[工作中]网站信息代码

@if (@a==@b) @end /*
@echo off
setlocal
call :stuff
timeout 1 
echo.
echo %code%
set "code=%code%"
timeout 3
exit

:stuff
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "https://pastebin.com/raw/wAtBQEeZ"') do (
set "code=%%I"
echo(%%I
)
exit /b

JScript */
var x=new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET",WSH.Arguments(0),true);
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState!=4) {WSH.Sleep(50)};
WSH.Echo(x.responseText);

工作图片:

【问题讨论】:

    标签: batch-file jscript


    【解决方案1】:

    您正在尝试编写一个混合脚本,该脚本一部分是 cmd.exe 批处理,一部分是 JScript(通过 CSCRIPT 执行)

    您必须确保批处理永远不会尝试执行 JScript 代码,相反,JSCRIPT 必须永远不会尝试执行批处理代码。

    通过 EXIT /B(和/或可能带有 :labels 的 GOTO/CALL)可以很容易地防止 JScript 被批处理看到。但 CSCRIPT 引擎将始终尝试将整个脚本解析为 JSCRIPT。

    成功的混合脚本的诀窍是在开头有一行是有效的批处理和 JScript。第一行在批处理中不应该做任何事情,在 JScript 中,除了开始 JScript 多行注释之外什么也不做。所有批处理代码都在后面,然后以 EXIT /B 结束。然后有一行关闭 JScript 多行注释,然后是 JScript 代码。

    您似乎试图将一个有效的混合脚本粘贴到另一个脚本中,但不了解它的工作原理。您只需要重新构建代码以确保所有批处理代码都对 JScript 隐藏(作为注释的一部分)

    @if (@a==@b) @end /* This is a harmless hybrid batch/JScript line that begins a JScript comment.
    
    @rem ================== Batch code ==========================
    @echo off
    color 0a
    setlocal
    call :initColorPrint
    call :title
    
    rem ---- Menu Stuff ----
    
    :menu
    call :title
    echo.
    echo (Type 'version' to see error)
    echo.
    call :colorPrint 09 "%time%"
    call :colorPrint 06 "@"
    call :colorPrint 0c "%username%"
    set /p call=^>
    if "%call%" =="version" goto :version
    %call%
    goto menu
    
    :title
    title CMD ^| User: %username%
    exit /b
    
    :version
    @echo off
    setlocal
    call :stuff
    timeout 1 
    echo.
    echo %code%
    set "code=%code%"
    timeout 3
    exit
    
    :stuff
    for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "https://pastebin.com/raw/wAtBQEeZ"') do (
    set "code=%%I"
    echo(%%I
    )
    exit /b
    
    rem ----- Color Stuff -----
    
    :colorPrint Color  Str  [/n]
    setlocal
    set "str=%~2"
    call :colorPrintVar %1 str %3
    exit /b
    
    :colorPrintVar  Color  StrVar  [/n]
    if not defined %~2 exit /b
    setlocal enableDelayedExpansion
    set "str=a%DEL%!%~2:\=a%DEL%\..\%DEL%%DEL%%DEL%!"
    set "str=!str:/=a%DEL%/..\%DEL%%DEL%%DEL%!"
    set "str=!str:"=\"!"
    pushd "%temp%"
    findstr /p /A:%1 "." "!str!\..\x" nul
    if /i "%~3"=="/n" echo(
    exit /b
    
    :initColorPrint
    for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
    <nul >"%temp%\x" set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%.%DEL%"
    exit /b
    
    :cleanupColorPrint
    del "%temp%\x"
    exit /b
    
    ====================== JScript Code ======================== */
    var x=new ActiveXObject("Microsoft.XMLHTTP");
    x.open("GET",WSH.Arguments(0),true);
    x.setRequestHeader('User-Agent','XMLHTTP/1.0');
    x.send('');
    while (x.readyState!=4) {WSH.Sleep(50)};
    WSH.Echo(x.responseText);
    

    我没有努力寻找您代码中的其他问题。它可能会也可能不会起作用。但这应该可以解决 JScript 编译错误。

    【讨论】:

    • 这个解决方案帮了很多忙。我从来没有在批处理文件中使用过 JScript,也找不到任何好的教程。我虽然你的解释很好,现在意识到出了什么问题。感谢您不仅提供了正确的代码,还解释了您添加的内容及其工作原理,感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 2014-12-03
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2017-04-22
    • 2021-01-09
    相关资源
    最近更新 更多