Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理文件-忽略子批处理文件中的CLS_Batch File - Fatal编程技术网

Batch file 批处理文件-忽略子批处理文件中的CLS

Batch file 批处理文件-忽略子批处理文件中的CLS,batch-file,Batch File,我正在创建一个批处理文件,它将调用其他几个批处理文件。要调用的一个批处理文件中有一个“CLS”。当直接从命令行调用子批处理文件时,是否有办法让包装批处理文件忽略子“CLS”调用,同时仍让CLS工作?您可以创建一个临时批处理文件,其中所有CLS命令都已被忽略: setlocal enabledelayedexpansion set target=targetname.bat for /f "usebackq delims=" %%a in ("%target%") do ( set line=%%

我正在创建一个批处理文件,它将调用其他几个批处理文件。要调用的一个批处理文件中有一个“CLS”。当直接从命令行调用子批处理文件时,是否有办法让包装批处理文件忽略子“CLS”调用,同时仍让CLS工作?

您可以创建一个临时批处理文件,其中所有
CLS
命令都已被忽略:

setlocal enabledelayedexpansion
set target=targetname.bat
for /f "usebackq delims=" %%a in ("%target%") do (
set line=%%a
Echo !line:cls=Rem Cls! >> temp_%target%
Echo !line:CLS=Rem Cls! >> temp_%target%
Echo !line:Cls=Rem Cls! >> temp_%target%
Rem Include CLs ClS cLS, etc. if the person who wrote the file is not case consistent
Rem  : ( 
set line=
)
call temp_%target%
set target=
Endlocal
这应该完全符合你的要求


莫娜

嗯-一根绳子有多长

通常,您可以在
调用
ed批处理中尝试此操作:

@echo off
setlocal
set skipcls=%1
if defined skipcls (if "%skipcls%"=="::" (shift) else (set "skipcls="))
...
%SKIPCLS% cls
...
然后使用额外的第一个参数调用批处理

如果第一个参数是
,它将被移出,
skipcls
将被设置为
;否则
skipcls
将为空,因此将跳过或执行
%skipcls%CLS


修正案:

或许

@echo off
setlocal
if "%~1"=="::" (shift&set "skipcls=REM ") else (set "skipcls=")
...
%SKIPCLS% cls
...
这样会更简单。将
skipcls
的值设置为REMSPACE,而不是
将允许在括号内的构造中更可靠地使用该技术

当然,另一种方法是在调用者例程中适当地设置
skipcls

或者将
skipcls
设置为您喜欢并使用的任何非空值

if [not] defined skipcls ...
它支持这样的构造:如果设置了,则执行此操作,如果不执行此操作

当然,它并不局限于
CLS
,因此可能应该使用不同的变量名

如果你真的想冒险,你可以在调用者中设置
重定向到
>“a_filename”
,然后

%redirect% ECHO whatever
将输出导向外部控制的目标


似乎有很多方法可以应用一个简单的概念。

这里有一个真正丑陋的黑客:-)

如果
stdout
被重定向到
con:
,则CLS屏幕命令不起作用-它将表单馈送字符(0x0C)打印到屏幕上。在我的机器上,它看起来像女性符号

@echo off

:: Normal call, CLS in called batch clears the screen
call batch2

:: Redirected call, CLS in called batch prints <FF> character to screen (without newline)
call batch2 >con:
@echo关闭
::正常呼叫,被呼叫批次中的CLS清除屏幕
呼叫batch2
::重定向调用,被调用的批处理中的CLS将字符打印到屏幕(无换行符)
调用batch2>con:
优点-无需修改您的子脚本


缺点-输出中出现不需要的
字符。

用户想知道如何避免不手动忽略
cls
:如果他想这样做,他可以在记事本中执行
Ctrl+H
,并用
Rem cls
::cls
替换
cls
。此外,他为什么要费心这么做,因为与
rem
相比,打字
%SKIPCLS%
要长得多,也很平淡。我的
-1
。@Monacraft-至少Magoo的解决方案被参数化了。可以在启用或禁用CLS的情况下调用相同的脚本,而无需更改代码;只是电话需要改变。但可以选择将变量CLS定义为CLS或undefined更简单。然后在脚本正文中使用%CLS%。