Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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
Windows 如何逃避回声;用于存储在文件中?_Windows_Batch File_Console_Cmd - Fatal编程技术网

Windows 如何逃避回声;用于存储在文件中?

Windows 如何逃避回声;用于存储在文件中?,windows,batch-file,console,cmd,Windows,Batch File,Console,Cmd,我知道:echo“废话”>file.txt。 而且echo”“>file.txt也可以工作 但是,如果我只想回显文件中的一个“(双引号),该怎么办 echo>file.txt不起作用,是否可以用一行命令执行此操作?Windows shell的转义字符是^,因此: echo ^" > file.txt 引号不需要转义来回显它,但是在第一个引号之后出现的字符将被视为引号,即使没有结束引号,因此除非引号转义,否则尾部重定向将不起作用 将单引号的回显重定向到文件而不转义很简单-只需将重定向移到前

我知道:
echo“废话”>file.txt
。 而且
echo”“>file.txt
也可以工作

但是,如果我只想回显文件中的一个
(双引号),该怎么办


echo>file.txt不起作用,是否可以用一行命令执行此操作?

Windows shell的转义字符是
^
,因此:

echo ^" > file.txt

引号不需要转义来回显它,但是在第一个引号之后出现的字符将被视为引号,即使没有结束引号,因此除非引号转义,否则尾部重定向将不起作用

将单引号的回显重定向到文件而不转义很简单-只需将重定向移到前面即可

>file.txt echo "
完整的答案有点复杂,因为报价系统是一个状态机。如果当前处于“关闭”状态,则下一个报价将“打开”,除非报价以
^“
的形式转义。一旦报价机处于“打开”状态,则下一个报价将始终将其关闭-无法转义关闭的报价

这里有一个小示范

@echo off
:: everything after 1st quote is quoted
echo 1) "this & echo that & echo the other thing
echo(

:: the 2nd & is not quoted
echo 2) "this & echo that" & echo the other thing
echo(

:: the first quote is escaped, so the 1st & is not quoted.
:: the 2nd & is quoted
echo 3) ^"this & echo that" & echo the other thing
echo(

:: the caret is quoted so it does not escape the 2nd quote
echo 4) "this & echo that^" & echo the other thing
echo(

:: nothing is quoted
echo 5) ^"this & echo that^" & echo the other thing
echo(
下面是结果

1) "this & echo that & echo the other thing

2) "this & echo that"
the other thing

3) "this
that" & echo the other thing

4) "this & echo that^"
the other thing

5) "this
that"
the other thing
附录

虽然无法逃逸结束报价,但可以使用延迟扩展来隐藏结束报价,或者使用幻影重新开始报价来抵消结束报价

@echo off
setlocal enableDelayedExpansion

:: Define a quote variable named Q. The closing quote is hidden from the
:: quoting state machine, so everything is quoted.
set Q="
echo 6) "this & echo that!Q! & echo the other thing
echo(

:: The !"! variable does not exist, so it is stripped after all quoting
:: has been determined. It functions as a phantom quote to counteract
:: the closing quote, so everything is quoted.
echo 7) "this & echo that"!"! & echo the other thing
结果

6) "this & echo that" & echo the other thing

7) "this & echo that" & echo the other thing

不,我知道,但是^character只用于^\^&^^ ^^^^^>^ ^ ^ ^ ^,因此该方法无法工作,请测试它,您会看到的。无论如何,我找到了自己的答案。:D Setlocal EnableDelayedExpansion Set quote=“echo!报价!>用这个file.txt,我只需要这个:echo!报价!>每次我需要它的时候。哦,好的,很有趣。。。早些时候,在提出这个问题之前,它没有起作用。也许我打错了P无论如何,谢谢!:DIt使用起来更安全
!“=!
相反,因为
可能是一个有效变量
set”“=这是一个引号“
”,但是
“=
永远不能是一个变量