批处理文件或VBS编辑一个字符

时间:2021-04-13 23:31:56

I need to use "something", maybe a *.bat or *.vbs or a combination of the two. Scenario: I have an environment with aprox 200 users. They have a production environment for a specific application and a test environment for this application also.

我需要使用“某些东西”,可能是* .bat或* .vbs或两者的组合。场景:我有一个aprox 200用户的环境。它们还具有针对特定应用程序的生产环境以及此应用程序的测试环境。

There is one file (an *.ini) file that has pulled a "Device ID" from the production environment into the test environment.

有一个文件(* .ini)文件已将“设备ID”从生产环境中提取到测试环境中。

That *.ini file in the test environment needs to have one character changed in it for it to work properly in the test environment. But each of the two hundred pc's will have a different Device ID.

测试环境中的* .ini文件需要更改一个字符才能在测试环境中正常工作。但是这两百台电脑中的每一台都有不同的设备ID。

So I need something that will go out to this test environment *.ini and compare it to the production environment *.ini, and replace the Device ID in the test *.ini with a new ID.

所以我需要一些能够用于测试环境* .ini的内容并将其与生产环境* .ini进行比较,并将测试* .ini中的设备ID替换为新ID。

Basically all that is changing in the device id, is the second character in the ID will be a "6".

基本上所有在设备ID中发生变化的是,ID中的第二个字符将是“6”。

D63001000A (within the TEST environment)

D33001000A (production environment)

So looks like I need it to change the the first "3" in the ID to a "6" (for the test environment).

所以看起来我需要它将ID中的第一个“3”更改为“6”(对于测试环境)。

1 个解决方案

#1


There are numerous ways how you could approach this, but I wouldn't recommend fiddling around with replacing single characters in an ID. If you want to go the VBScript route, you could use a dictionary for storing a mapping of computernames to IDs and use that in the replacement.

有很多方法可以解决这个问题,但我不建议在ID中替换单个字符。如果您想要使用VBScript路由,可以使用字典存储计算机名到ID的映射,并在替换中使用它。

Set deviceIDs = CreateObject("Scripting.Dictionary")
deviceIDs.Add "computer A", "ID A"
deviceIDs.Add "computer B", "ID B"
deviceIDs.Add "computer C", "ID C"
...

Set fso = CreateObject("Scripting.FileSystemObject")
ini = fso.OpenTextFile("C:\path\to\your.ini").ReadAll

Set re = New RegExp
re.Pattern = "(device id = ).*"

Set net = CreateObject("WScript.Network")
ini = re.Replace(ini, "$1" & deviceIDs(net.ComputerName))

fso.OpenTextFile("C:\path\to\your.ini").Write ini

If you have the mapping of computer names to device IDs in some other file (e.g. a CSV) you could also use that file for populating the dictionary:

如果您将计算机名称映射到某些其他文件(例如CSV)中的设备ID,则还可以使用该文件填充字典:

Set deviceIDs = CreateObject("Scripting.Dictionary")

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile("C:\path\to\device_id.csv")
Do Until f.AtEndOfStream
  id = Split(f.ReadLine, ",")
  deviceIDs.Add id(0), id(1)
Loop
f.Close

Edit: For replacing a specific character in a specific line you could do something like this:

编辑:要替换特定行中的特定字符,您可以执行以下操作:

lineNumber = 23

Set fso = CreateObject("Scripting.FileSystemObject")
ini = Split(fso.OpenTextFile("C:\path\to\your.ini").ReadAll, vbNewLine)

Set re = New RegExp
re.Pattern = "^(.{16})3"

ini(lineNumber) = re.Replace(ini(lineNumber), "$16")

fso.OpenTextFile("C:\path\to\your.ini").Write Join(ini, vbNewLine)

#1


There are numerous ways how you could approach this, but I wouldn't recommend fiddling around with replacing single characters in an ID. If you want to go the VBScript route, you could use a dictionary for storing a mapping of computernames to IDs and use that in the replacement.

有很多方法可以解决这个问题,但我不建议在ID中替换单个字符。如果您想要使用VBScript路由,可以使用字典存储计算机名到ID的映射,并在替换中使用它。

Set deviceIDs = CreateObject("Scripting.Dictionary")
deviceIDs.Add "computer A", "ID A"
deviceIDs.Add "computer B", "ID B"
deviceIDs.Add "computer C", "ID C"
...

Set fso = CreateObject("Scripting.FileSystemObject")
ini = fso.OpenTextFile("C:\path\to\your.ini").ReadAll

Set re = New RegExp
re.Pattern = "(device id = ).*"

Set net = CreateObject("WScript.Network")
ini = re.Replace(ini, "$1" & deviceIDs(net.ComputerName))

fso.OpenTextFile("C:\path\to\your.ini").Write ini

If you have the mapping of computer names to device IDs in some other file (e.g. a CSV) you could also use that file for populating the dictionary:

如果您将计算机名称映射到某些其他文件(例如CSV)中的设备ID,则还可以使用该文件填充字典:

Set deviceIDs = CreateObject("Scripting.Dictionary")

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile("C:\path\to\device_id.csv")
Do Until f.AtEndOfStream
  id = Split(f.ReadLine, ",")
  deviceIDs.Add id(0), id(1)
Loop
f.Close

Edit: For replacing a specific character in a specific line you could do something like this:

编辑:要替换特定行中的特定字符,您可以执行以下操作:

lineNumber = 23

Set fso = CreateObject("Scripting.FileSystemObject")
ini = Split(fso.OpenTextFile("C:\path\to\your.ini").ReadAll, vbNewLine)

Set re = New RegExp
re.Pattern = "^(.{16})3"

ini(lineNumber) = re.Replace(ini(lineNumber), "$16")

fso.OpenTextFile("C:\path\to\your.ini").Write Join(ini, vbNewLine)