读取和批处理文件写REG_DWORD [英] Read and write REG_DWORD from batch file

查看:220
本文介绍了读取和批处理文件写REG_DWORD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是从注册表中读取一个REG_DWORD,并将其写入到另一个位置。我已经成功地从注册表位置看书,但不知道怎么写。

My requirement is to read a REG_DWORD from the registry and write it to another location. I have succeeded in reading from a registry location, but don't know how to write.

我的code:

@echo off
REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\NetCache /v "OfflineDirRenameDelete"
SET previous=OfflineDirRenameDelete
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MySoftware /v "CacheDelete" /t REG_DWORD /d previous /f

这code失败,因为REG ADD不理解previous变量

This code fails, as "reg add" doesn't understand the "previous" variable

我能硬code的值,并将其设置到注册表中。例如,这个作品:

I am able to hard-code a value and set it to the registry. For example, this works:

REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MySoftware /v "CacheDelete" /t REG_DWORD /d 5454 /f

但我基本上不知道该如何动态的REG_DWORD值到注册表中。

But I basically don't know how to dynamic REG_DWORD value to the registry.

推荐答案

问题是,REG QUERY指令返航一些注册表项,它的类型和值的名称在一起。例如,它会是这样的:

The problem was that the REG QUERY instruction was returning something the name of the registry key, the type of it, and the value together. For example, it would be something like:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\CacheDelete
    LogLevel    REG_DWORD    0x29

和我试图将该值设置为在注册表中,这显然失败了,因为这是不是一个DWORD更另一个REG_DWORD。而我可能已经在设法太一些语法错误之类的东西,所以也难怪它失败了。

And I was trying to set this value to another REG_DWORD in the registry, which obviously failed since this is more than a DWORD. And I may have had some syntax error and stuff in trying that too, so there's no wonder it failed.

以下code为我工作:

The following code worked for me:

    @ECHO OFF

set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\NetCache"
set VALUE_NAME="OfflineDirRenameDelete"

FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (
 `REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set "ValueName=%%A"
    set "ValueType=%%B"
    set Val="%%C"
)

reg add %KEY_NAME% /v "CacheDelete" /t REG_DWORD /d %Val% /f

希望这可以帮助别人!

Hope this helps someone!

这篇关于读取和批处理文件写REG_DWORD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆