2016-08-30 73 views
1

我试图从一个.bat文件写入不同的东西到一个文本文件在每一个尝试插入一个报价"它导致避免这个命令,它看起来像,因为它缺少"在输出文件中的符号 这里就是我要做的。丢失从Windows命令行或批处理引号“符号到文件

echo ' " '>> file.txt 

,它甚至没有工作,如果我们简单地从CLI

echo ' " '> file.txt 
试试吧

我有同样的我来自MinGW的printfecho。 这里有什么问题?

回答

2

你需要escape some special characters字面上对待它们。特别是,"double quote toggles the quote flag, if the quote flag is active, the following special characters are no longer special: ^&|<>()

==> echo ' " '>> file.txt 
' " '>> file.txt 

==> type file.txt 
The system cannot find the file specified. 

==> echo ' ^" '>> file.txt 

==> type file.txt 
' " ' 

下一个脚本显示了ECHO命令的一些转义规则;注意不同的输出(和规则插入符感叹号)如果Delayed Expansion被禁用或启用:

@cls 
@setlocal disabledelayedexpansion 
@Call :ouputOnly 
@endlocal 
@Echo . 
@setlocal enabledelayedexpansion 
@Call :ouputOnly 
@endlocal 
@GOTO :eof 
:ouputOnly 
@Echo ^@ - At Symbol: be less verbose 
@Echo ^~ - Tilde: Parameter Expansion as in Call subroutines, FOR loops etc. 
@Echo ^& - Single Ampersand: used as a command separator 
@Echo ^&^& - Double Ampersand: conditional command separator (if errorlevel 0) 
@Echo ^|^| - Double Pipe: conditional command separator (if errorlevel ^> 0) 
@Echo ^:^: - Double Colon: alternative to "rem" for comments outside of code blocks 
@Echo ^^ - Caret: general escape character in batch 
@Echo ^" - Double Quote: surrounding a string in double quotes 
@Echo  escapes all of the characters contained within it 
@Echo ^() - Parentheses: used to make "code blocks" of grouped commands 
@Echo %% - Percentage Sign: are used to mark three of the four variable types 
@Echo ^^! - Exclamation Mark: to mark delayed expansion environment variables ^^!var^^! 
@Echo ^* - Asterisk: wildcard matches any number or any characters 
@Echo ^? - Question Mark: matches any single character 
@Echo ^. - Single dot: represents the current directory 
@Echo ^.. - Double dot: represents the parent directory of the current directory 
@Echo ^\ - Backslash: represent the root directory of a drive dir ^\ 
@Echo ^| - Single Pipe: redirects the std.output of one command 
@Echo  into the std.input of another 
@Echo ^NUL (File like device): is like a bottomless pit 
@Echo ^CON (File like device): is a file like device that represents the console 
@Echo ^> - Single Greater Than: redirects output to either a file or file like device 
@Echo ^>^> - Double Greater than: output will be added to the very end of the file 
@Echo ^< - Less Than: redirect the contents of a file to the std.input of a command 
@Echo  Stream redirection: regarding the less and greater than symbols 
@echo caret^^  "caret^" 
@echo caret^^^^ bang^^! "caret^^ bang^!" 
@exit /B 
@rem based on (dead link nowadays) http://judago.webs.com/batchoperators.htm 
相关问题