Reading/Deleteing/Writting to txt file

October 5, 2009 at 06:12:24
Specs: Windows Vista
I know I have seen many different threads about reading over text files with a batch file, but I for some reason am having a hard time making those work with my problem.
This is what I am trying to do. I have a batch that when run will call :

REGEDIT /E C:\filetest.txt "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"

And it will create the registry listing for 'Interfaces' in filetest.txt. And what I am trying to do next is to have the batch file read over the text file and remove every key and value in the text file. Then I would like it (for every index) to add a new registry key and value to the text file.

After that I would like it to rename the file and open it to add the new key and value to the registry. (this part I know how to do).

Any help is greatly appreciated and if you need further explanation just let me know.
Thanks.


See More: Reading/Deleteing/Writting to txt file

Report •


#1
October 5, 2009 at 07:10:29
It may help to post a few lines of the file, with before and after, so we can better understand what's needed.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
October 5, 2009 at 08:07:46
Before:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{GUID}]
"UseZeroBroadcast"=dword:00000000
"EnableDHCP"=dword:00000000
.
.
.
"EnableDeadGWDetect"=dword:00000001
"DontAddDefaultGateway"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{GUID}]
"UseZeroBroadcast"=dword:00000000
"EnableDHCP"=dword:00000001
.
.
.
"DefaultGatewayMetric"=hex(7):00,00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{GUID}]
"UseZeroBroadcast"=dword:00000000
"EnableDHCP"=dword:00000000
.
.
.
"EnableDeadGWDetect"=dword:00000001
"DontAddDefaultGateway"=dword:00000000


After:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{GUID}]
"DhcpConnEnableBcastFlagToggle"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{GUID}]
"DhcpConnEnableBcastFlagToggle"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{GUID}]
"DhcpConnEnableBcastFlagToggle"=dword:00000001

Basically I need it to remove any line that has " at the beginning and for every [HKEY line to add my DHCPConn... line after them (skipping the first [HKEY that is)

Does that Help


Report •

#3
October 7, 2009 at 06:38:44
So I finally figured it out. It's probably not the best way of doing it but it works.
Let me know if there is anything I can do to make it more efficient

setlocal ENABLEDELAYEDEXPANSION

@echo off

REGEDIT /E C:\filetest.txt "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"

type C:\filetest.txt | findstr /v = | findstr /v , > C:\z.txt

set n=0

for /F "delims=" %%j in (c:\z.txt) do (
set /a n+=1
if !n! gtr 2 (
echo.%%j>> c:\MyFile.reg
echo."DhcpConnEnableBcastFlagToggle"=dword:00000001>> C:\MyFile.reg
echo.>> C:\MyFile.reg
) else (
echo.%%j>> c:\MyFile.reg
echo.>> C:\MyFile.reg
)
)

REGEDIT /S C:\MyFile.reg

DEL C:\filetest.txt
DEL C:\z.txt
DEL C:\MyFile.reg


Report •

Related Solutions

#4
October 8, 2009 at 08:43:58
Looks pretty solid to me.


=====================================
Helping others achieve escape felicity

M2


Report •


Ask Question