基本上,我做了一个批量的程序,创建了一个程序,允许自定义文件名和按键绑定来启动一个隐蔽的chrome窗口,然后关闭它,但是按键绑定使用%errorlevel%,但是当重写它变成0的时候,我知道一个解决方案来修复它一次,但我需要做两次。
我没有太多的编程技能。
我也不知道有没有可能专门关闭隐身窗口。有什么办法吗?
代码
除了错误级别的代码行之外,其他所有代码都工作得很好(但我想我会提供所有代码)
echo @echo off >> TempIncognitoCreator.bat
echo set Closer= 1 >> TempIncognitoCreator.bat
echo set CloseBatch= 10 >> TempIncognitoCreator.bat
echo Title Module Configuration >> TempIncognitoCreator.bat
echo color 80 >> TempIncognitoCreator.bat
echo echo Module configuration >> TempIncognitoCreator.bat
echo echo. >> TempIncognitoCreator.bat
echo set /p selectName= Select the name of the IncognitoModule: >> TempIncognitoCreator.bat
echo cls >> TempIncognitoCreator.bat
echo echo Module configuration >> TempIncognitoCreator.bat
echo echo. >> TempIncognitoCreator.bat
echo set /p keybindStart= Select the letter that will start incognito mode (CHROME): >> TempIncognitoCreator.bat
echo cls >> TempIncognitoCreator.bat
echo echo Module configuration >> TempIncognitoCreator.bat
echo echo. >> TempIncognitoCreator.bat
echo set /p keybindClose= Select the letter that will close CHROME: >> TempIncognitoCreator.bat
echo :Closerino >> TempIncognitoCreator.bat
echo cls >> TempIncognitoCreator.bat
echo echo Module configuration >> TempIncognitoCreator.bat
echo echo. >> TempIncognitoCreator.bat
echo set /p CloseBatch= Select do you want the module to close after the chrome termination (Y / N){default NO}: >> TempIncognitoCreator.bat
echo if %%CloseBatch%% == 10 goto Closerino >> TempIncognitoCreator.bat
echo if %%CloseBatch%% == Y goto YES >> TempIncognitoCreator.bat
echo if %%CloseBatch%% == N goto NO >> TempIncognitoCreator.bat
echo goto Closerino >> TempIncognitoCreator.bat
echo :YES >> TempIncognitoCreator.bat
echo set Closer= 2 >> TempIncognitoCreator.bat
echo :NO >> TempIncognitoCreator.bat
echo set Closer= 1 >> TempIncognitoCreator.bat
echo cls >> TempIncognitoCreator.bat
echo echo You selected the name %%selectName%% >> TempIncognitoCreator.bat
echo echo You selected the Keybind to start incognito mode %%keybindStart%% >> TempIncognitoCreator.bat
echo echo You selected the Keybind to close chrome %%keybindClose%% >> TempIncognitoCreator.bat
echo echo @echo off ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo :back ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo Title %%selectName%% ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo choice /c %%keybindStart%%%%keybindClose%% /n >> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo if %%errorlevel%%==1 goto Start ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo if %%errorlevel%%==2 goto Close ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo :Start ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo start chrome --new-window --incognito "www.google.com" ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo echo You started new Incognito window ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo ping -n 3 127.0.0.1^>nul ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo goto back ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo :Close ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo taskkill /f /im chrome.exe ^>^> %%selectName%%.bat >> TempIncognitoCreator.bat
echo echo if %%Closer%% == 1 goto back >> TempIncognitoCreator.bat
echo echo if %%Closer%% == 2 goto killeroni >> TempIncognitoCreator.bat
echo :killeroni >> TempIncognitoCreator.bat
echo echo taskkill cmd.exe >> TempIncognitoCreator.bat
echo echo exit >> TempIncognitoCreator.bat发布于 2021-01-15 09:28:25
因为你是双重嵌套的,所以你需要双重转义,所以对于你的特定问题,我建议你需要每边使用4%的字符,即%%%%errorlevel%%%%。从本质上讲,您应该用第一个嵌套的另一个% %%转义它,然后对下一个嵌套%%%%执行同样的操作。
作为对上面的回答的补充,以及对我的评论的补充,我建议你如何构建你的脚本。我发现这更容易阅读和维护,并且大大减少了你的代码。
@(
Echo @Echo Off
Echo Set "Closer=1"
Echo Set "CloseBatch=10"
Echo Title Module Configuration
Echo Color 80
Echo Echo Module configuration
Echo Echo(
Echo Set /P "selectName=Select the name of the IncognitoModule: "
Echo ClS
Echo Echo Module configuration
Echo Echo(
Echo Set /P "keybindStart=Select the letter that will start incognito mode (CHROME): "
Echo ClS
Echo Echo Module configuration
Echo Echo(
Echo Set /P "keybindClose=Select the letter that will close CHROME: "
Echo(
Echo :Closerino
Echo ClS
Echo Echo Module configuration
Echo Echo(
Echo Set /P "CloseBatch=Select do you want the module to close after the chrome termination (Y / N){default NO}: "
Echo If /I "%%CloseBatch%%" == "Y" GoTo YES
Echo If /I "%%CloseBatch%%" == "N" GoTo NO
Echo GoTo Closerino
Echo(
Echo :YES
Echo Set "Closer=2"
Echo(
Echo :NO
Echo Set "Closer=1"
Echo ClS
Echo Echo You selected the name %%selectName%%
Echo Echo You selected the Keybind to start incognito mode %%keybindStart%%
Echo Echo You selected the Keybind to close chrome %%keybindClose%%
Echo (
Echo Echo @Echo Off
Echo Echo(
Echo Echo :Back
Echo Echo Title %%selectName%%
Echo Echo %%%%SystemRoot%%%%\System32\choice.exe /C %%keybindStart%%%%keybindClose%% /N
Echo Echo If ErrorLevel 2 GoTo Close
Echo Echo Start "" chrome.exe --new-window --incognito "https://www.google.com"
Echo Echo Echo You started new Incognito window
Echo Echo %%%%SystemRoot%%%%\System32\timeout.exe /t 2 /NoBreak ^^^>NUL
Echo Echo GoTo Back
Echo Echo(
Echo Echo :Close
Echo Echo %%%%SystemRoot%%%%\System32\taskkill.exe /F /Im chrome.exe
Echo ^) ^> "%%selectName%%.bat"
Echo If /I Not "%%Closer%%" == "2" GoTo Back
Echo %%SystemRoot%%\System32\taskkill.exe /Fi "UserName Eq %%UserName%%" /Im cmd.exe
) 1> "TempIncognitoCreator.bat"还请注意,如果您没有实现某种确保最终用户输入正确数据的方法,那么您已经决定使用Set /P,这是潜在的危险。让我们以Set /P "selectName=Select the name of the IncognitoModule: "为例。如果您的最终用户使用的模块名称对于文件名无效,那么在创建"%selectName%.bat"时就会出现问题。因此,如果您想让它更健壮,我建议您合并一些验证技术。
发布于 2021-01-15 15:18:08
我不明白您为什么要以如此复杂的方式经历创建辅助文件的步骤。您可以在变量中定义默认配置选项,并在修改配置选项时将新的定义值附加到正在运行的脚本。调用配置标签时,将应用最近附加的定义:
@ECHO off
Setlocal Enableextensions EnableDelayedExpansion
:# Clear any existing definitions for '.' prefixed script variables
(For /F "Tokens=1,2 Delims==" %%G in ('Set "."')Do Set "%%G=") 2> nul
:# execute config label ; define default definitions ; overrides if settings modified
CALL :config
:menu
CLS
Echo/[!.Exit!] Exit
Echo/[!.Start!] Start Incognito Session
Echo/[!.Close!] Close Incognito Session
Echo/[!.Config!] Edit %~n0 Configuration
For /F "Delims=" %%C in ('Choice /N /C:!.Choicelist!')Do (
If "%%C" == "!.exit!" (
If defined config.mod >>"%~f0" (For /F "Tokens=1,2 Delims==" %%G in ('Set "." ^| Sort /R')Do Echo/Set "%%G=%%H")
Exit /B 0
)
If "%%C" == "!.start!" Start "" chrome.exe --new-window -incognito
If "%%C" == "!.close!" (
For /F "Skip=3 Delims=" %%G in ('Tasklist.exe /FI "imagename eq chrome.exe" /FI "sessionname eq console" /FO:table')Do (
For /F "tokens=2" %%H in ("%%G")Do Taskkill.exe /PID %%H 2> nul > nul
)
)
If "%%C" == "!.config!" (
Echo/Select Key to overide: !.Choicelist!
For /F "Delims=" %%D in ('Choice /N /C:!.Choicelist!')Do (
Echo/Select New Key: !.Keylist!
For /F "Delims=" %%E in ('Choice /N /C:!.Keylist!')Do (
For %%O in ("exit" "start" "close" "config")Do If "!.%%~O!" == "%%D" Set ".%%~O=%%E"
Set ".Keylist=!.Keylist:%%E=%%D!"
Set ".Choicelist=!.Choicelist:%%D=%%E!"
Set "config.mod=_"
)
)
)
)
Goto :menu
::: SCRIPT BREAK - end of script
:config
:# Default definitions for script variables prefixed with '.' to facilitate bulk output.
Set ".exit=Q"&Set ".Start=O"&Set ".Close=X"& Set ".Config=C"
Set ".Keylist=ABCDEFGHIJKLMNOPQRST0123456789"
For %%A in (!.Exit! !.Start! !.Close! !.Config!)Do (
Set ".Choicelist=!.Choicelist!%%A"
Set ".Keylist=!.Keylist:%%A=!"
)
:# appended CONFIG Options. Last Line must end with CRLFhttps://stackoverflow.com/questions/65727095
复制相似问题