如何正确转义命令行参数?

时间:2019-02-01 12:20:14

标签: batch-file arguments

我正在开发一个简单的应用程序,该应用程序应加密命令行给出的字符串,然后将其写入注册表。不幸的是,如果密码中包含诸如" & < > |之类的特殊字符,我会遇到麻烦。例如:

Encrypt.exe /Password:Password /reg       --> works fine
Encrypt.exe /Password:Password"&<>| /reg  --> the password variable contains: Password&<>| /reg which is wrong
Encrypt.exe /Password:Password&<>|        --> this outputs: "> was unexpected at this time." but no idea where this text is coming from
Encrypt.exe /Password:"Password&<>|"      --> this works, but note that the password can't contains a " char and the encrypted/decrypted string will contain a " at the beginning and at the end

所以我的问题是,我怎么把这个字符串Password&<>|正确应用?

1 个答案:

答案 0 :(得分:2)

要在.bat文件中转义某些字符:

select p 
from Person p
where not exists (
   select b from p.books b where b.name = 'Effective Java'
)

供参考: https://www.robvanderwoude.com/escapechars.php