简体   繁体   English

非交互写入注册表而不会覆盖

[英]Non-interactively write to the registry without overwriting

I'm trying to use the reg add command (reg.exe) to write to the registry, but if the value already exists I don't want to overwrite it. 我正在尝试使用reg add命令(reg.exe)写入注册表,但是如果该值已经存在,则我不想覆盖它。 Without the /f option it asks for confirmation, which I don't want. 如果没有/f选项,它将要求您确认,我不希望这样做。 With /f it overwrites the value, which I also don't want. 使用/f它将覆盖我也不想要的值。

Is there an easy way to add a registry value if it doesn't exist but leave it alone if it does, and not prompt the user? 如果注册表值不存在,是否有一种简单的方法添加注册表值,如果存在,则将其保留,并且提示用户?

As per my comment… 根据我的评论...

Because you have reported having to decline the overwrite prompt, I would suggest you pipe the expected N into your REG command: 因为您报告了必须拒绝覆盖提示,所以建议您将期望的N传递给REG命令:

Echo N|Reg Add "HK…… >Nul

As reg returns errorlevels it can be as simple as 当reg返回错误级别时,它可以很简单
(using conditional execution on && success or || fail ) (在&&成功或||失败时使用条件执行)

reg query "hkcu\console" /V ColorTable15 >NUL 2>&1  &&( echo Val exists) ||(echo Val not there)
Val exists

reg query "hkcu\console" /V ColorTable16 >NUL 2>&1  &&( echo Val exists) ||(echo Val not there)
Val not there

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM